读书人

操作数据库请教 finally最后如何执行不

发布时间: 2012-04-11 17:42:33 作者: rapoo

操作数据库请问 finally最后怎么执行不了呢?
try{

String name= new String(request.getParameter("uid"));


String password= new String(request.getParameter("upwd"));

DataBase db= new DataBase(); //DataBase 是个数据库的类
String sql = "select * from XT_YHXX where YHMC='" + name + "' and YHMM='" + password + "' and YXBZ='Y'";


ResultSet rs=db.select(sql);


}

catch(Exception e)
{

String str = "登陆信息出错了!";


response.sendRedirect( "error.jsp?mess='" + str +"'");

}
finally
{
boolean t = db.close();
/*
if(db.close() == true)
{
if(rs!=null)
{
rs.close();
}
}
*/

}

DataBase类中的 close 方法
public boolean close(){
try{
if(rs!=null){
rs.close();
}
if(stmt!=null){
stmt.close();
}
if(conn!=null){
conn.close();
}
return true;
}catch(Exception e){
System.err.println("关闭失败!");
return false;
}
}

这个语句:boolean t = db.close();
放到try 最后可以执行,为什么放在finally执行不了?提示错误:
cannot resolve symbol
symbol : variable db
location: class org.apache.jsp.login_jsp
boolean t = db.close();


[解决办法]
DataBase db= new DataBase();
放try前面

我习惯,try前加一个标示变量=false,catch中=true, finally后面 if(变量){response.sendRedirect()}
[解决办法]
DataBase db= null;

try{

String name= new String(request.getParameter("uid"));


String password= new String(request.getParameter("upwd"));

db= new DataBase(); //DataBase 是个数据库的类


finally
{
if(db != null) {
db.close();
}
/*

读书人网 >Java Web开发

热点推荐