问题描述
我从一个数据库中提取数据,执行两次检查,并希望更新另一个数据库中的表.
I extract data from one database, execute a couple of checks and want to update a table in another database.
我的update.executeQuery语句不起作用.但是,当我在SQL中复制语句字符串并执行它时,它确实起作用.这可能是什么问题? (甚至指导也会对我有帮助)
My update.executeQuery statement is not working. However when I copy the statement string in SQL and execute it, it does work. What could be wrong with it? (even directions would help me)
创建更新字符串:
public static void createUpdateString() throws SQLException {
try {
updateString = "UPDATE ORDER_HEADER SET "
+ "FRUIT=" + "'" + fruitName+ "',"
+ "CUSTOMER_NAME =" + "'" + customerName + "'"
+ " WHERE ORDER_NUMBER = TRIM(" + "'" + orderNumber + "')";
updateData();
}
catch(SQLException e) {
System.err.println("ERROR!: " + e.getMessage());
}
}
执行更新查询:
public static void updateData() throws SQLException{
try {
conn.setAutoCommit(false);
statementUpdate = conn.createStatement();
statementUpdate.executeQuery(OrderObject.updateString);
conn.commit();
conn.setAutoCommit(true);
statementUpdate.close();
}
catch(SQLException e) {
System.err.println("Could not process query" + e.getMessage());
}
}
推荐答案
此行
statementUpdate.executeQuery(OrderObject.updateString);
在我看来错了.试试
statementUpdate.executeUpdate(OrderObject.updateString);
相反.
UPDATE
语句不是查询,因此不要将它们与executeQuery()
一起使用. executeQuery()
返回ResultSet
,但是使用UPDATE
语句没有数据要返回.而是使用executeUpdate()
. (请注意,您还使用executeUpdate()
来运行INSERT
和DELETE
语句-Statement
对象上没有任何executeInsert()
或executeDelete()
方法.)
UPDATE
statements are not queries, so you don't use executeQuery()
with them. executeQuery()
returns a ResultSet
, but with an UPDATE
statement there's no data to return. Instead, you use executeUpdate()
. (Note that you also use executeUpdate()
to run INSERT
and DELETE
statements - there aren't any executeInsert()
nor executeDelete()
methods on Statement
objects.)
在这种情况下,数据库的行为因一个数据库而异.特别是,如果您尝试将executeQuery()
与UPDATE
语句一起使用,则MySQL JDBC驱动程序将引发带有以下消息的异常:
How databases behave in these situations varies from one database to another. In particular, the MySQL JDBC driver throws an exception with the following message if you attempt to use executeQuery()
with an UPDATE
statement:
这篇关于JDBC更新语句在Netbeans中不起作用,但在SQL中起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!