JDBC更新语句在Netbeans中不起作用,但在SQL中起作用 [英] JDBC Update Statement not working in Netbeans but working in SQL

查看:118
本文介绍了JDBC更新语句在Netbeans中不起作用,但在SQL中起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从一个数据库中提取数据,执行两次检查,并希望更新另一个数据库中的表.

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()来运行INSERTDELETE语句-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:

无法使用executeQuery()发出数据操作语句

Can not issue data manipulation statements with executeQuery()

这篇关于JDBC更新语句在Netbeans中不起作用,但在SQL中起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆