日期:2014-05-17  浏览次数:20780 次

操作数据库请问 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();
}
/*