简体   繁体   English

Java - JDBC executeUpdate()不起作用

[英]Java - JDBC executeUpdate() not working

I am trying to insert/update/delete a row using jdbc. 我试图使用jdbc插入/更新/删除行。 Inserting is working, and I only changed the query string (or insertTableSQL ) By debugging, I suspect that executeUpdate() is not terminated and console does not show me any error message. 插入工作正常,我只更改了查询字符串(或insertTableSQL )通过调试,我怀疑executeUpdate()没有终止,控制台也没有显示任何错误消息。 (JUST BLANK) (只是空白)

[EDIT] [编辑]
The program is stuck while executeUpate() is exeucting, meaning that I cannot even see the return value 执行executeUpate()时程序被卡住了,这意味着我甚至看不到返回值

Instead of using query string, I also tried PreparedStatement but no luck :( 我没有使用查询字符串,而是尝试了PreparedStatement,但没有运气:(

String deleteRecordSQL = "DELETE mytable WHERE id = ?";
PreparedStatement ps = dbConnection.prepareStatement(deleteRecordSQL);
ps.setInt(1, 6);
ps.executeUpdate();

Full Code: 完整代码:

import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;

public class DeleteRow {

private static final String DB_DRIVER = "oracle.jdbc.driver.OracleDriver";
private static final String DB_CONNECTION = "";
private static final String DB_USER = "";
private static final String DB_PASSWORD = "";

public static void main(String[] argv) {

    try {

        deleteRecordFromDbUserTable();

    } catch (SQLException e) {

        System.out.println(e.getMessage());

    }

}

private static void deleteRecordFromDbUserTable() throws SQLException {

    Connection dbConnection = null;
    Statement statement = null;

    try {
        dbConnection = getDBConnection();
        statement = dbConnection.createStatement();
        statement.executeUpdate("DELETE mytable WHERE id = 6");

        System.out.println("Record is deleted!");


    } catch (SQLException e) {

        System.out.println(e.getMessage());

    } finally {

        if (statement != null) {
            statement.close();
        }

        if (dbConnection != null) {
            dbConnection.close();
        }

    }

}

private static Connection getDBConnection() {

    Connection dbConnection = null;

    try {

        Class.forName(DB_DRIVER);

    } catch (ClassNotFoundException e) {

        System.out.println(e.getMessage());

    }

    try {

        dbConnection = DriverManager.getConnection(
                           DB_CONNECTION, DB_USER,DB_PASSWORD);
        return dbConnection;

    } catch (SQLException e) {

        System.out.println(e.getMessage());

    }

    return dbConnection;

}

} }

Any help? 有帮助吗?

THIS IS NOT THE OFFICIAL ANSWER, BUT THE PROBLEM IS RESOLVED SOMEHOW 这不是官方的答案,但问题是如何解决的

There was Java update notification, and I followed it. 有Java更新通知,我跟着它。 After update, the status of JRE System Library(JavaSE - 1.7) is changed to unbound . 更新后,JRE系统库(JavaSE - 1.7)的状态将更改为未绑定 I changed Execution environment to JavaSE - 1.6 (Oracle WebLogic Server 11gR1 (10.3.6) JRE), and the issue was resolved..weird.. 我将执行环境更改为JavaSE - 1.6(Oracle WebLogic Server 11gR1(10.3.6)JRE),问题解决了......很奇怪..

There was no syntax problem 没有语法问题

DELETE FROM mytable WHERE id = 6
DELETE mytable WHERE id = 6

If anyone can explain me how this issue could be resolved, your help would be much appreciated. 如果有人能解释我如何解决这个问题,我们将非常感谢您的帮助。

FYI. 仅供参考。

C:\>java -version
java version "1.7.0_67"
Java(TM) SE Runtime Environment (build 1.7.0_67-b01)
Java HotSpot(TM) Client VM (build 24.65-b04, mixed mode, sharing)

Since its stuck it must be waiting for some resource which might be locked. 由于卡住它必须等待一些可能被锁定的资源。 I had faced this issue for simple update query. 对于简单的更新查询,我遇到过这个问题。 Here the update query was run on sql developer tool and the commit was not done. 这里更新查询是在sql developer工具上运行的,并且没有完成提交。

Thanks to a comment by Prabhmanmeet Singh on this link 感谢Prabhmanmeet Singh对此链接的评论

If you didn't get an exception, which you don't state one way or the other, you need to examine the return value of executeUpdate() , which is the number of affected rows. 如果没有得到异常,而您没有说明这种情况,则需要检查executeUpdate()的返回值,即受影响行的数量。 If it was zero, no rows were deleted, probably because nothing matched the WHERE clause. 如果它为零,则不删除任何行,可能是因为没有与WHERE子句匹配。 If it was non-zero, that many rows were deleted. 如果它不为零,那么删除了很多行。 You certainly shouldn't be printing "Record is deleted!" 你当然不应该打印"Record is deleted!" when nothing may have happened at all. 当什么都没发生的时候。 Your program may be lying to the user. 您的程序可能对用户撒谎。

NB insertTableSQL is a stupid name for a variable containing an SQL DELETE statement. NB insertTableSQL是包含SQL DELETE语句的变量的愚蠢名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM