日期:2014-05-18  浏览次数:20870 次

在class里建立连接,代码出问题了,请大家帮忙改正,立即给分
package   com.efsoft.cyfwpt.chen;
import   java.sql.*;


public   class   Conntion   {

private   String   url= "jdbc:oracle:thin:@127.0.0.1:1521:CHEN ";
private   String   user= "chen ";
private   String   passWord= "111111 ";
private   Connection   conn;
private   Statement   stmt;

public   Conntion(){

}

public     Connection   getConnect(){

try{
Class.forName( "oracle.jdbc.driver.OracleDriver ").newInstance();
  conn=   DriverManager.getConnection(url,user,passWord);
  stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
}catch(Exception   e){
System.out.print(e.toString());
}
return   conn;
}
        public   void   freeconn(){        
        stmt.close();
        conn.close();
        }

        public   ResultSet   resout   (String   sql){
        ResultSet   rs=stmt.executeQuery(sql);
        return   rs;
        }


}
代码如上.可好象   conn     和stmt对象的使用范围出错还是怎么的了.
大家帮忙改正谢谢.

------解决方案--------------------
最好加上判斷
public void freeconn(){
if (stmt!=null)
stmt.close();
if (conn!=null)
conn.close();
}