select count(*).. 在java中执行这段sql语句,要是查询结果不为0则停止执行,并报错。java程序该怎么写?

2024-12-03 13:46:06
推荐回答(2个)
回答1:

  public void sqlTest throws SQLException{
  Connection con=null;
  Statement st=null;
  ResultSet rs=null;
  int a=0;
  try {
   Class.forName("net.sourceforge.jtds.jdbc.Driver");
   con = DriverManager.getConnection("jdbc:jtds:sqlserver://localhost:1433/study","sa","0000");
   st = con.createStatement();
   String sqlStr="select count(*) from btoroom where AL_ID is null or len(al_id) = 0 ";
   rs = st.executeQuery(sqlStr);
   if(rs.next()){
    a = rs.getInt(0);
   if(a==0){
   throw new Exception("记录数为0!")
   }
   }
  } catch (ClassNotFoundException e) {
  e.printStackTrace();
  }finally{
  con.close();

  st.close();
  rs.close();
  }
}

回答2:

查询结果不为0则停止执行,是让后面的代码不再执行吗?还有,报错,需要报什么错误?NullPointerException?还是其他错误?