java - java中的更新语句

标签 java

大家好, 这是我的代码,

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/

相关文章:

java - 如何使用字符串输入访问现有对象(将字符串输入转换为对象名称)

java - Android AndEngine : java. lang.IllegalArgumentException: 未找到 EGLConfig

java - 无法解析配置 ':compileClasspath' 的所有文件

java - 通过反射从对象的字段访问数组

java - 尝试在 Spring 中加载默认页面时出现 404 错误

Java整数与字符串相加

java - 在计算机之间发送图像(从 Java 到 MATLAB)

java - 如何使用ConvertApi提供的excel转pdf REST api?

java - Maven "build path specifies execution environment J2SE-1.5",即使我将其更改为 1.7

java - 在 Windows 中使用 Java 网络摄像头界面进行人脸检测