How to use prepared statement for select query in Java?

前端 未结 2 1644
情歌与酒
情歌与酒 2020-12-01 21:02

I had tried several times using prepared statements but it returns SQL exception. here is my code:

public ArrayList name(String mobile, String          


        
相关标签:
2条回答
  • 2020-12-01 21:29

    For both parameter you use preparedStatement.setString(1, ..); so the first parameter is set two times. but you never set the value for second parameter.

    so change

    preparedStatement.setString(1, mobile);
                preparedStatement.setString(1, password);
    

    to

        preparedStatement.setString(1, mobile);
        preparedStatement.setString(2, password);
    
    0 讨论(0)
  • 2020-12-01 21:46

    You need to use:

    preparedStatement.executeQuery();
    

    instead of

    preparedStatement.executeQuery(login);
    

    when you pass in a string to executeQuery() that query is executed literally and thus the ? is send to the database which then creates the error. By passing query string you are not execution the "cached" prepared statement for which you passed the values.

    0 讨论(0)
提交回复
热议问题