Passing parameters to a JDBC PreparedStatement

后端 未结 6 733
星月不相逢
星月不相逢 2020-11-28 11:45

I\'m trying to make my validation class for my program. I already establish the connection to the MySQL database and I already inserted rows into the table. The table consis

6条回答
  •  醉酒成梦
    2020-11-28 12:37

    You should use the setString() method to set the userID. This both ensures that the statement is formatted properly, and prevents SQL injection:

    statement =con.prepareStatement("SELECT * from employee WHERE  userID = ?");
    statement.setString(1, userID);
    

    There is a nice tutorial on how to use PreparedStatements properly in the Java Tutorials.

提交回复
热议问题