尝试catch块不使用sql

时间:2018-06-13 20:04:22

标签: java sql try-catch

我有以下java代码:

ResultSet rs2 = stmt.executeQuery("select * from teacher");  

        try
        {rs2.getInt("fee");
          System.out.println("found");
          stmt.executeUpdate("alter table teacher drop fee  ");
        }catch(SQLException e){
            System.out.println("not found");
            System.err.print(e);
            stmt.executeUpdate("alter table teacher add fee int ");
        } 

尝试似乎永远不会被调用。

1 个答案:

答案 0 :(得分:1)

您需要执行rs2.next()以获取第一行(及后续行)。如:

ResultSet rs2 = stmt.executeQuery("select * from teacher");  

//rs2.next(); // reads one row!
  rs2.first();


try {
  rs2.getInt("fee");
  System.out.println("found");
  stmt.executeUpdate("alter table teacher drop fee  ");
} catch(SQLException e) {
  System.out.println("not found");
  System.err.print(e);
  stmt.executeUpdate("alter table teacher add fee int ");
}