Database insertion

后端 未结 2 1020
-上瘾入骨i
-上瘾入骨i 2021-01-28 23:14
if(lines.size() >= 5){
    String Actor  = it.next();
    String Bio = it.next();
    String More_Bio = it.next();
    String Reason = it.next();
    String Fact = it         


        
2条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-29 00:02

    You should use PreparedStatement primarily because it prevents SQL injection attacks. @John Moses has posted a tutorial to use PreparedStatement from the Java official documentation, here is another good link: MySQL and Java JDBC - Tutorial.

    Moving your code to PreparedStatement, it should be like this:

    PreparedStatement ps = con.prepareStatement("INSERT INTO Tiffany(Actor, Bio, More_Bio, Reason, Fact) VALUES (?, ?, ?, ?, ?) ");
    ps.setString(1, Actor);
    ps.setString(2, Bio);
    ps.setString(3, More_Bio);
    ps.setString(4, Reason);
    ps.setString(5, Fact);
    ps.executeUpdate();
    

    Don't forget to close your resources after use them:

    ps.close();
    con.close();
    

提交回复
热议问题