select count(*).. 在java中执行这段sql语句,要是查询结果不为0则停止执行,并报错。java程序该怎么写?
发布网友
发布时间:2022-04-10 18:38
我来回答
共2个回答
热心网友
时间:2022-04-10 20:08
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();
}
}
热心网友
时间:2022-04-10 21:26
查询结果不为0则停止执行,是让后面的代码不再执行吗?还有,报错,需要报什么错误?NullPointerException?还是其他错误?