我的java程序中用到了mysql数据库,我想做的是一旦不能正常连接到数据库则抛出异常,使用程序能根据判断做其它的操作如:
public Student loadById(int id)throws Exception{ Student stu;
Session s = Hibernate.getSessionFactory.openSession();
Query q = s.createQuery("from Student where id ='"+id"'")
try{
stu = (Student)q.list().get(0);
}
catch(Exception e){
throw e;
}
s.close();
return stu;
} 当由于网络原因或其它原因无法正常连接到数据库时,标红的地方就会报错,但是,用try-catch是捕捉不到的
我该怎么处理,或者大家 有什么其它的好的方法,可以让我能够捕捉到MYSQL产生的异常
public Student loadById(int id)throws Exception{ Student stu;
Session s = Hibernate.getSessionFactory.openSession();
Query q = s.createQuery("from Student where id ='"+id"'")
try{
stu = (Student)q.list().get(0);
}
catch(Exception e){
throw e;
}
s.close();
return stu;
} 当由于网络原因或其它原因无法正常连接到数据库时,标红的地方就会报错,但是,用try-catch是捕捉不到的
我该怎么处理,或者大家 有什么其它的好的方法,可以让我能够捕捉到MYSQL产生的异常
不好意思,我不知道新启的线程该怎么来捕捉这个异常:还是在
try{
stu = (Student)q.list().get(0);
}
catch(Exception e){
throw e;
}这个地方加try catch吗?
这个也捕捉不到,而且还会提示,不有地方抛出SQLException