簡體   English   中英

無法使用jdbc preparestatement查詢mysql數據庫

[英]Unable to query mysql database using jdbc preparedstatement

我正在嘗試運行下面的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)

直接刪除占位符並設置該值即可。

我不確定語法在哪里錯誤,如果有人可以指出該錯誤,我將不勝感激。

我收到了您的錯誤,為什么還要將查詢再次分配給preparestatment。

您的代碼:

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();

這就是為什么您收到語法錯誤的原因。 希望這對您有幫助

刪除; 在您的SQL字符串,然后再試一次。

; 大多數數據庫工具中都使用sql字符串來分隔sql字符串,但這不是標准的sql。

代替

statement.executeQuery(stmtSelectObjKey);

采用

statement.executeQuery();

它將工作!

當您使用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) {}
    }

欲了解更多信息, 請點擊這里

替換以下查詢

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'";

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM