问题描述
我正在尝试使用jdbc插入/更新/删除行.插入正常,并且我仅更改了查询字符串(或 insertTableSQL ).通过调试,我怀疑 executeUpdate()没有终止,并且控制台没有向我显示任何错误消息. (只是空白)
I am trying to insert/update/delete a row using 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. (JUST BLANK)
executeUpate()执行时程序被卡住,这意味着我什至看不到返回值
我也尝试使用 PreparedStatement ,但是没有运气:(
Instead of using query string, I also tried PreparedStatement but no luck :(
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)的状态更改为 unbound .我将执行环境更改为JavaSE-1.6(Oracle WebLogic Server 11gR1(10.3.6)JRE),此问题已解决..weird..
There was Java update notification, and I followed it. After update, the status of JRE System Library(JavaSE - 1.7) is changed to unbound. I changed Execution environment to JavaSE - 1.6 (Oracle WebLogic Server 11gR1 (10.3.6) JRE), and the issue was resolved..weird..
没有语法问题
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.
仅供参考.
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()无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!