Topic: JDBC Object has been closed

  Print this page

1.JDBC Object has been closed Copy to clipboard
Posted by: huajiazhou
Posted on: 2005-05-21 23:55

JAVABEAN连接数据库的代码!
import java.sql.*;
import java.lang.*;
public class sqlBean{
  public Connection conn=null;
  public ResultSet rs=null;
  private String DatabaseDriver="com.microsoft.jdbc.sqlserver.SQLServerDriver";
  private String DbConnStr="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=mis";
  private String User="sa";
  private String Pswd="5696285";
  
  
  public void setDatabaseDriver(String dbDriver){
    this.DatabaseDriver=dbDriver;
  }
  public void setDatabaseConnStr(String ConnStr){
    this.DbConnStr=ConnStr;
  }
  public void setUser(String user){
    this.User=user;
  }
  public void setPassword(String pwd){
    this.Pswd=pwd;
  }
  public String getDatabaseDriver(){
    return (this.DatabaseDriver);
  }
  public String getDatabaseConnStr(){
    return (this.DbConnStr);
  }
  // constructor
  public sqlBean(){
    try{
      Class.forName(DatabaseDriver);//.newInstance();
    }catch(ClassNotFoundException e){
      System.out.println("Load DB Driver Error:"+e.getMessage());
    }
    try{
      conn = DriverManager.getConnection(DbConnStr,User,Pswd);
    }catch(Exception ex){
      System.out.println("Query Error:"+ex.getMessage());
    }
  }  
  public int executeInsert(String sql){
    int num=0;
    try{
      Statement stmt=conn.createStatement();
      num=stmt.executeUpdate(sql);
    }catch(SQLException ex){
      System.out.println("Insert Data Error:"+ex.getMessage());
    }
    CloseDataBase();
    return num;
  }   
  public ResultSet executeQuery(String sql){
    rs=null;
    try{
    Statement stmt=conn.createStatement();
    rs=stmt.executeQuery(sql);
    }catch(SQLException ex){
      System.out.println("Query Error:"+ex.getMessage());
    }
    CloseDataBase();
    return rs;
  }
  
  public int executeDelete(String sql){
    int num=0;
    try{
      //conn = DriverManager.getConnection(DbConnStr,User,Pswd);
      Statement stmt=conn.createStatement();
      num=stmt.executeUpdate(sql);
    }catch(SQLException ex){
      System.out.println("Delete Data Error:"+ex.getMessage());
    }
    CloseDataBase();
    return num;
  }
  // Close the DB
  public void CloseDataBase(){
    try{
     conn.close();
    }catch(Exception end){
      System.out.println("Can not close the DB:"+end.getMessage());
    }
  }
}
编译没错,但是实际运用在JSP里面后提示java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC]Object has been closed. 的错误!
请大家指教啊!这个问题搞的我想自杀,一直解决不了!

2.Re:JDBC Object has been closed [Re: huajiazhou] Copy to clipboard
Posted by: huajiazhou
Posted on: 2005-05-21 23:55

我想可能是数据库关闭的地方有错误啊 
请高手指教啊!!!

3.Re:JDBC Object has been closed [Re: huajiazhou] Copy to clipboard
Posted by: why
Posted on: 2005-05-22 03:06

How is it used?

You may want to set up a logger or whatever debugging tools and check when this Expection happens.

4.Re:JDBC Object has been closed [Re: huajiazhou] Copy to clipboard
Posted by: huajiazhou
Posted on: 2005-05-22 13:57

TOMCAT5.0+JCREATOR下发生的
以上只是连接数据库的JAVABEAN代码!
JSP页面代用的代码我没写出来
我认为应该就是这里的问题!
我开始在运行的时候都很正常!不知道怎么回事就突然连不上去了!

5.Re:JDBC Object has been closed [Re: huajiazhou] Copy to clipboard
Posted by: yhjvcnet
Posted on: 2005-06-02 14:31

把sqlserver的jdbc包放在WEB-INF/lib下。
同时,你在代码中应该显示关闭Statement


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923