如何在JAVA中检查mysql update语句的返回值

如何在JAVA中检查mysql update语句的返回值,java,mysql,Java,Mysql,我是一名java程序员新手,下面是我根据记录的ID删除记录的代码片段 private void RemovebuttonActionPerformed(java.awt.event.ActionEvent evt) { BookIDvar=Integer.parseInt(BookID.getText()); System.out.println(BookIDvar); Connection conn = null; Statement stmt = null;

我是一名java程序员新手,下面是我根据记录的ID删除记录的代码片段

private void RemovebuttonActionPerformed(java.awt.event.ActionEvent evt) {   

   BookIDvar=Integer.parseInt(BookID.getText());
   System.out.println(BookIDvar);
   Connection conn = null;
   Statement stmt = null;
   try{
  //STEP 2: Register JDBC driver
   Class.forName("com.mysql.jdbc.Driver");

  //STEP 3: Open a connection
   System.out.println("Connecting to database...");
   conn = DriverManager.getConnection(DB_URL,USER,PASS);

  //STEP 4: Execute a query
  System.out.println("Creating statement...");
  stmt = conn.createStatement();
  String sql;
  sql="DELETE FROM book_details WHERE book_id="+BookIDvar+"";
  stmt.executeUpdate(sql);
  //STEP 6: Clean-up environment

  stmt.close();
  conn.close();
 }
代码运行良好。但我想检查是否存在具有该特定ID的记录。是否有方法使用当前代码本身检查它?Java是否提供了一些结构来检查
executeupdate
的返回值,类似于
executeQuery

答案 int a=stmt.executeUpdate(sql)

System.out.println(a);//-->查询在数据库中操作的列数

语法 int executeUpdate(字符串sql)抛出SQLException

执行给定的SQL语句,该语句可以是INSERT、UPDATE或DELETE语句,也可以是不返回任何内容的SQL语句,例如SQL DDL语句

注意执行更新();方法返回类型为“int”,即返回查询在数据库中操作的列数