我正在尝试使用 jdbc 插入/更新/删除一行。插入工作正常,我只更改了查询字符串(或 insertTableSQL) 通过调试,我怀疑 executeUpdate() 没有终止并且控制台没有向我显示任何错误消息. (只是空白)
[编辑]
程序在executeUpate()执行的时候卡住了,意思是我连返回值都看不到
我没有使用查询字符串,而是尝试了 PreparedStatement 但没有成功 :(
String deleteRecordSQL = "DELETE mytable WHERE id = ?";
PreparedStatement ps = dbConnection.prepareStatement(deleteRecordSQL);
ps.setInt(1, 6);
ps.executeUpdate();
完整代码:
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;
}
有什么帮助吗?
最佳答案
这不是官方答案,但问题已通过某种方式解决
有Java更新通知,我跟着。更新后,JRE 系统库(JavaSE - 1.7) 的状态更改为未绑定(bind)。我将执行环境更改为 JavaSE - 1.6 (Oracle WebLogic Server 11gR1 (10.3.6) JRE),问题得到解决..很奇怪..
没有语法问题
DELETE FROM mytable WHERE id = 6
DELETE mytable WHERE id = 6
如果有人能向我解释如何解决这个问题,我们将不胜感激。
仅供引用。
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)
关于Java - JDBC executeUpdate() 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27029049/