[英]Using Java's JDBC PreparedStatement on a SQLite Database to query for NOT NULL
[英]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.