PreparedStatement在oracle中成功执行但在Microsoft SQL中抛出异常 [英] PreparedStatement executing successfully in oracle but throwing exception in Microsoft SQL
问题描述
我有以下查询,我使用java PreparedStatement执行:
I have this below query that I execute using java PreparedStatement:
String dml=insert into users(name, addr, city, sex, dob) values(?,?,?,?,?);
PreparedStatement stmt = conn.prepareStatement(dml);
stmt.setString(1,"abcd");
stmt.setString(2,"def");
stmt.setString(3,"ghij");
stmt.setString(4,"m");
stmt.setString(5,"1-Jan-1987");
stmt.executeQuery();
当数据库是Oracle时它会成功执行,但是当数据库是Microsoft SQL时,它会抛出一个exception java.sql.SQLException:executeQuery方法必须返回结果集
。有人可以告诉我这里有什么问题。为什么同样的查询在oracle中成功执行但不在microsft sql中执行?
It executes successfully when the database is Oracle, but when the database is Microsoft SQL, then it throws an exception "java.sql.SQLException: The executeQuery method must return a result set"
. Could someone please tell what is the issue here. Why is the same query executing successfully in oracle but not in microsft sql?
推荐答案
答案在消息中 - ExecuteQuery需要一个结果集。请改用 executeUpdate 。
The answer is in the message - ExecuteQuery requires a result set. Use executeUpdate instead.
从以上链接:
-
布尔值执行()在此PreparedStatement对象中执行SQL语句,该对象可以是任何类型的SQL语句。
boolean execute() Executes the SQL statement in this PreparedStatement object, which may be any kind of SQL statement.
ResultSet executeQuery()执行此PreparedStatement对象中的SQL查询,并返回查询生成的ResultSet对象。
ResultSet executeQuery() Executes the SQL query in this PreparedStatement object and returns the ResultSet object generated by the query.
int executeUpdate()执行SQL此PreparedStatement对象中的语句,必须是SQL INSERT,UPDATE或DELETE语句;或者什么都不返回的SQL语句,例如DDL语句。
int executeUpdate() Executes the SQL statement in this PreparedStatement object, which must be an SQL INSERT, UPDATE or DELETE statement; or an SQL statement that returns nothing, such as a DDL statement.
它可能在oracle上工作的事实可能是只是你发现的副作用是不可靠的。
the fact that it works on oracle is probably just a side effect which you've discovered cannot be relied upon.
这篇关于PreparedStatement在oracle中成功执行但在Microsoft SQL中抛出异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!