无法使用jdbc preparedstatement查询mysql数据库

时间:2015-02-18 11:18:48

标签: java mysql jdbc

我正在尝试运行以下jdbc代码来获取对象密钥:

String sqlSelectObjKey = "select OBJ_KEY from obj where OBJ_NM=? and OBJ_TYP_CD='CONN';";


    PreparedStatement statement = null;
    ResultSet rs = null;
    try {           
        statement = connection.prepareStatement(stmtSelectObjKey);
        statement.setString(1, "Postal Connector");

        rs = statement.executeQuery(stmtSelectObjKey);
        while(rs.next()) {
            this.executionLog.setObjKey(rs.getInt("OBJ_KEY"));
        }

    } catch (SQLException e1) {
        e1.printStackTrace();

    } finally {
        if (rs != null) try { rs.close(); } catch (SQLException ignore) {}
        if (statement != null) try { statement.close(); } catch (SQLException ignore) {}
    }

但每次导致以下异常:

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 '? and OBJ_TYP_CD='CONN'' at line 1
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:377)
    at com.mysql.jdbc.Util.getInstance(Util.java:360)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:978)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3887)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3823)
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2435)
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2582)
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2526)
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2484)
    at com.mysql.jdbc.StatementImpl.executeQuery(StatementImpl.java:1446)
    at appMetadata.ConnectorMetadataLoad.insertExecutionLog(ConnectorMetadataLoad.java:67)
    at appCore.PostalCodeInterface.getPostalData(PostalCodeInterface.java:37)
    at appMain.PostalDataConnector.getPostalData(PostalDataConnector.java:19)
    at PostalDemo.main(PostalDemo.java:14)

移除占位符并直接设置值可以正常工作。

我不确定语法在哪里出错,如果有人能指出错误,我将不胜感激。

5 个答案:

答案 0 :(得分:2)

我收到了您的错误,为什么要再次将查询分配给preparedstatment。

您的密码:

statement = connection.prepareStatement(stmtSelectObjKey);
statement.setString(1, "Postal Connector");
rs = statement.executeQuery(stmtSelectObjKey);

代码应该是:

statement = connection.prepareStatement(stmtSelectObjKey);
statement.setString(1, "Postal Connector");
rs = statement.executeQuery();

这就是为什么你的语法错误?希望这可以帮助你

答案 1 :(得分:1)

删除sql字符串中的;并再次尝试。

大多数db-tools中使用;来分隔sql-strings,但它不是标准的sql。

答案 2 :(得分:1)

而不是

statement.executeQuery(stmtSelectObjKey);

使用

statement.executeQuery();

它会起作用!!

答案 3 :(得分:0)

当您使用prepareStatement时,执行语法如下prepareStatement.executeQuery()所以更改您的代码如下

String sqlSelectObjKey = "select OBJ_KEY from obj where OBJ_NM=? and OBJ_TYP_CD='CONN';";


    PreparedStatement statement = null;
    ResultSet rs = null;
    try {           
        statement = connection.prepareStatement(stmtSelectObjKey);
        statement.setString(1, "Postal Connector");

        rs = statement.executeQuery();//Change is here
        while(rs.next()) {
            this.executionLog.setObjKey(rs.getInt("OBJ_KEY"));
        }

    } catch (SQLException e1) {
        e1.printStackTrace();

    } finally {
        if (rs != null) try { rs.close(); } catch (SQLException ignore) {}
        if (statement != null) try { statement.close(); } catch (SQLException ignore) {}
    }

了解更多信息click here

答案 4 :(得分:-1)

替换以下查询

String sqlSelectObjKey = "select OBJ_KEY from obj where OBJ_NM=? and OBJ_TYP_CD='CONN';";

String sqlSelectObjKey = "select OBJ_KEY from obj where OBJ_NM=? and OBJ_TYP_CD='CONN'";