Passing parameters to a JDBC PreparedStatement

后端 未结 6 734
星月不相逢
星月不相逢 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:35

    There is a problem in your query..

       statement =con.prepareStatement("SELECT * from employee WHERE  userID = "+"''"+userID);
       ResultSet rs = statement.executeQuery();
    

    You are using Prepare Statement.. So you need to set your parameter using statement.setInt() or statement.setString() depending upon what is the type of your userId

    Replace it with: -

       statement =con.prepareStatement("SELECT * from employee WHERE  userID = :userId");
       statement.setString(userId, userID);
       ResultSet rs = statement.executeQuery();
    

    Or, you can use ? in place of named value - :userId..

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

提交回复
热议问题