大家好, 这是我的代码,
public static boolean updateDB() {
Connection cn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
boolean result = false;
int number;
String insetSQL ="update quan set ten_quan='lin' where id=1";
try {
cn = LocalDatabasePooling.getInstance().getConnection();
pstmt = cn.prepareStatement(insetSQL);
pstmt.executeUpdate();
result = true;
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (rs != null) {
try {
rs.close();
} catch (SQLException sqle) { }
}
if (pstmt != null) {
try {
pstmt.close();
}
catch (SQLException sqle) { }
}
if (cn1 != null) {
try {
cn1.close();
}
catch (SQLException sqle) { }
}
}
return false;
}
上面的代码正确吗?但是当我调试时,我看到了这一行
number = pstmt.executeUpdate();
没有执行,下面的代码也没有执行。
当我添加 watch 时
pstmt.executeUpdate();
我看到了这个信息
error(during the evaluation
出了什么问题?
最佳答案
显然抛出了一个异常,该异常被您的代码完全忽略了。像这样的东西:
try {
// Your code was here.
} catch (SQLException e) {
// But you didn't put anything here.
}
您至少应该记录/打印堆栈跟踪
try {
// Your code was here.
} catch (SQLException e) {
e.printStackTrace();
}
或者,更好的是,直接扔掉它。
public SomeObject someMethod() throws SQLException {
try {
// Your code was here.
} finally {
// Surely close resources here.
}
}
异常包含有关问题原因的大量信息,不应忽略它们,除非您真的知道自己在做什么。
关于java - java中的更新语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7975425/