【问题标题】:preparedStatement syntax error准备好的语句语法错误
【发布时间】:2014-06-18 15:50:02
【问题描述】:

我最近在使用 Java PreparedStatements 时遇到了这个问题。我有以下代码:

String selectSql1
        = "SELECT `value` FROM `sampling_numbers` WHERE `value` < (?)" ;
    ResultSet rs1 = con.select1(selectSql1,randNum);

select1 方法在哪里

    public ResultSet select1(String sql, int randNum) {
    try {
        this.stmt = con.prepareStatement(sql);
        stmt.setInt(1, randNum);
        return this.stmt.executeQuery(sql);
    } catch (SQLException e) {
        e.printStackTrace();
        return null;
    }
}

但是,它一直抛出这个错误:

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '?)' at line 1
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
    at com.mysql.jdbc.Util.getInstance(Util.java:386)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1054)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4237)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4169)
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2617)
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2778)
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2828)
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2777)
    at com.mysql.jdbc.StatementImpl.executeQuery(StatementImpl.java:1651)
    at util.P_DBCon.select1(P_DBCon.java:59)
    at app.RandomNumberGenerator.main(RandomNumberGenerator.java:60)

    Exception in thread "main" java.lang.NullPointerException
    at app.RandomNumberGenerator.main(RandomNumberGenerator.java:64)

当我以天真的方式执行“...value &lt; " + randNum”时不会发生此问题,但我想这样做。

非常感谢任何帮助。

更新

我尝试了社区的各种建议,例如

String selectSql1
        = "SELECT `value` FROM `sampling_numbers` WHERE value < ?" ;

String selectSql1
        = "SELECT value FROM sampling_numbers WHERE value < ?" ;

仍然出现错误消息。

【问题讨论】:

  • 你能把所有的`从你的sql字符串和括号里去掉吗?可能是这样的:selectSql1 = "SELECT value FROM sampling_numbers WHERE value
  • 为什么要用括号括起来?
  • @JunedAhsan 我试过你的变种,仍然报错。
  • @JunedAhsan:占位符 ? 周围的括号不是错误。看来 OP 正在从其他地方发布错误。
  • @bryansis2010 更新第二个 SQL 字符串应该可以工作,清理您的项目并重新构建。

标签: java mysql prepared-statement


【解决方案1】:

解决你的问题其实很简单,当你想打电话给PreparedStatement.executeQuery()时,你是在打电话给Statement.executeQuery(String)-

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

【讨论】:

  • 正是我需要的。谢谢!也谢谢你的解释
【解决方案2】:
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;
    }
}

参考如下代码

PreparedStatement statement =  con.prepareStatement("SELECT EMP_ID,EMP_PWD FROM employee " +
                    "WHERE EMP_ID = '"+param1+"'");
            ResultSet result = statement.executeQuery();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-06-11
    • 1970-01-01
    • 1970-01-01
    • 2019-11-04
    • 2018-04-10
    • 1970-01-01
    相关资源
    最近更新 更多