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

Java 无法使用jdbc preparedstatement查询mysql数据库,java,mysql,jdbc,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(

我正在尝试运行以下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)
移除占位符并直接设置值效果良好


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

删除
,然后重试


在大多数数据库工具中用于分隔sql字符串,但它不是标准sql。

删除
,然后重试


在大多数数据库工具中用于分隔sql字符串,但它不是标准sql。

删除
,然后重试


在大多数数据库工具中用于分隔sql字符串,但它不是标准sql。

删除
,然后重试


在大多数数据库工具中用于分隔sql字符串,但它不是标准sql。

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

您的代码:

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

这就是为什么您会得到?的语法错误?。希望这对您有所帮助

我知道了您的错误,为什么您要再次将查询分配给PreparedStation

您的代码:

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

这就是为什么您会得到?的语法错误?。希望这对您有所帮助

我知道了您的错误,为什么您要再次将查询分配给PreparedStation

您的代码:

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

这就是为什么您会得到?的语法错误?。希望这对您有所帮助

我知道了您的错误,为什么您要再次将查询分配给PreparedStation

您的代码:

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();
这就是为什么您会得到?的语法错误?。希望这对您有所帮助,而不是

statement.executeQuery(stmtSelectObjKey);
使用

它会起作用的

而不是

statement.executeQuery(stmtSelectObjKey);
使用

它会起作用的

而不是

statement.executeQuery(stmtSelectObjKey);
使用

它会起作用的

而不是

statement.executeQuery(stmtSelectObjKey);
使用


它会起作用的

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

有关详细信息

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

有关详细信息

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

有关详细信息

在使用
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';";

替换下面的查询

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



您确定
是正确的占位符吗?是的,直接在where子句中删除占位符和设置值也有效。错误告诉我们
有问题。看起来
没有被字符串参数替换。我调试了语句,它似乎在替换占位符,但即使这样,它也会导致相同的错误。您可能应该使用不带参数的
executeQuery()
。作为旁注,您还应该为第二个“CONN”使用占位符。您确定
是正确的占位符吗?是的,直接在where子句中删除占位符和设置值也有效。错误告诉我们
有问题。看起来
没有被字符串参数替换。我调试了语句,它似乎在替换占位符,但即使这样,它也会导致相同的错误。您可能应该使用不带参数的
executeQuery()
。作为旁注,您还应该为第二个“CONN”使用占位符。您确定
是正确的占位符吗?是的,直接在where子句中删除占位符和设置值也有效。错误告诉我们
有问题。看起来
没有被字符串参数替换。我调试了语句,它似乎在替换占位符,但即使这样,它也会导致相同的错误。您可能应该使用不带参数的
executeQuery()
。作为旁注,您还应该为第二个“CONN”使用占位符。您确定
是正确的占位符吗?是的,直接在where子句中删除占位符和设置值也有效。错误告诉我们
有问题。看起来
没有被字符串参数替换。我调试了语句,它似乎在替换占位符,但即使这样,它也会导致相同的错误。您可能应该使用不带参数的
executeQuery()
。作为旁注,您还应该为第二个“CONN”使用占位符。重复的重复的重复的重复的