Results 1 to 2 of 2
  1. #1
    Join Date
    May 2004
    Posts
    1

    Unanswered: Object has been closed

    i am running a java program in tomcat to connect SQL Server,using the Microsoft's jdbc driver ,as the following code :

    import java.sql.*;
    class Bkjz{
    ResultSet rs=null;
    String spde;
    String condition;
    Connection con=null;
    Statement sql;
    public String getSpde(){
    return spde;
    }
    public void setSpde(String spde){
    this.spde=spde;
    }
    public ResultSet Searchsjk(){
    try {
    Class.forName("com.microsoft.jdbc.sqlserver.SQLSer verDriver");
    con=DriverManager.getConnection("jdbc:microsoft:sq lserver://localhost:1433;databasename=zkbm","zkbm","zkbm");
    sql=con.createStatement(ResultSet.TYPE_SCROLL_SENS ITIVE,ResultSet.CONCUR_READ_ONLY);
    if(spde.equals(""))
    condition="select CRCC,CRNM,SPDE,SPNM from SPCR where EMTP='5'group by SPDE,SPNM,CRCC,CRNM";
    else
    condition="select CRCC,CRNM,SPDE,SPNM from SPCR where SPDE='"+spde+"'"+"and EMTP='5' group by SPDE,SPNM,CRCC,CRNM";
    rs=sql.executeQuery(condition);
    //con.close();
    if(!rs.next())
    {
    return null;
    }
    else
    {
    rs.previous();
    return rs;
    }



    }
    catch(Exception e){
    message="exception!!! "+e.toString();
    System.out.println(e);
    return null;
    }



    }
    }
    public class Bk{
    public static void main(String args[]){
    Bkjz bbb=new Bkjz();
    bbb.setSpde("1020110");
    try{
    ResultSet rr=bbb.Searchsjk();

    while(rr.next()){
    System.out.println(rr.getString("CRCC"));
    }
    }
    catch(Exception e){
    System.out.println(e);
    }
    }
    }


    without con.close(),it can return Resultset ,but when includeing con.close(),an Exception tell me:Object has been closed, in other programms i've close connection,but it never throws this Exception,why? thanks

  2. #2
    Join Date
    Jul 2003
    Location
    SoCal
    Posts
    721
    Have you tried closing the connection AFTER you close the recordset? If you're not using a client-side recordset, you may not be able to close the connection until you close the recordset.
    That which does not kill me postpones the inevitable.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •