preparedStatement syntax error

前端 未结 2 1046
[愿得一人]
[愿得一人] 2020-11-28 15:35

I recently encountered this problem with Java PreparedStatements. I have the following code:

String selectSql1
        = \"SELECT `value` FROM `sampling_numb         


        
相关标签:
2条回答
  • 2020-11-28 15:40

    The solution to your problem is actually very easy, you are calling Statement.executeQuery(String) when you want to call PreparedStatement.executeQuery() -

    this.stmt = con.prepareStatement(sql); // Prepares the Statement.
    stmt.setInt(1, randNum);               // Binds the parameter.
    // return this.stmt.executeQuery(sql); // calls Statement#executeQuery
    return this.stmt.executeQuery();       // calls your set-up PreparedStatement
    
    0 讨论(0)
  • 2020-11-28 15:59
    public ResultSet select1(String sql, int randNum) {
        try {
            this.stmt = con.prepareStatement(sql);
            stmt.setInt(1, randNum);
            return this.stmt.executeQuery();
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }
    

    for reference see below code

    PreparedStatement statement =  con.prepareStatement("SELECT EMP_ID,EMP_PWD FROM employee " +
                        "WHERE EMP_ID = '"+param1+"'");
                ResultSet result = statement.executeQuery();
    
    0 讨论(0)
提交回复
热议问题