import java.util.*;
import java.sql.*;public class table {
private Connection con;
private Statement sta;
private ResultSet rs;
private ResultSet rs1;
private Hashtable has=new Hashtable();
private Hashtable ha=new Hashtable();
private class authors{
String au_id;
String au_lname;
String au_fname;
}
private class titles{
String title_id;
String title;
}
public table(){
try{
String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=pubs";
String user="sa";
String pwd="sa";
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
con=DriverManager.getConnection(url,user,pwd);
System.out.println("ok");
sta=con.createStatement();
rs=sta.executeQuery("select au_id,au_lname,au_fname from authors");
while(rs.next()){
authors author=new authors();
author.au_id=rs.getString(1);
author.au_lname=rs.getString(2);
author.au_fname=rs.getString(3);
System.out.println(author.au_id+"  ");
rs1=sta.executeQuery("select title_id,title from titles where title_id in(select title_id from titleauthor where au_id='"+author.au_id+"')");
while(rs1.next()){
System.out.println(rs1.getString("title_id"));
titles title=new titles();
title.title_id=rs1.getString(1);
title.title=rs1.getString(2);
System.out.println(title.title=rs1.getString(2));
ha.put(title.title_id,title.title);
}
rs1.close();
has.put(author.au_id,ha);
}
rs.close();
con.close();
}
catch(Exception ex){
System.out.println("err");
}
}
public static void main(String args[]){
new table();
}
}
不知道哪出了错,请大家帮忙,谢!

解决方案 »

  1.   

    什么异常?runtime异常这里不好判断,用debug查
    另外 rs.close();
    con.close();
    写在finally里
      

  2.   

    不好意思这是错误信息:
    java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC]Object has been closed.
    at com.microsoft.jdbc.base.BaseExceptions.createException(Unknown Source)
    at com.microsoft.jdbc.base.BaseExceptions.getException(Unknown Source)
    at com.microsoft.jdbc.base.BaseResultSet.validateClosedState(Unknown Source)
    at com.microsoft.jdbc.base.BaseResultSet.commonFetchInitialize(Unknown Source)
    at com.microsoft.jdbc.base.BaseResultSet.next(Unknown Source)
    at table.<init>(table.java:30)
    at table.main(table.java:56)
      

  3.   

    Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
    看看有没有sun.jdbc.odbc.JdbcOdbcDriver这个驱动,用这个或许可以。
      

  4.   

    我不是连接数据库的问题,是
    while(rs.next()){
    authors author=new authors();
    author.au_id=rs.getString(1);
    author.au_lname=rs.getString(2);
    author.au_fname=rs.getString(3);
    System.out.println(author.au_id+"  ");
    rs1=sta.executeQuery("select title_id,title from titles where title_id in(select title_id from titleauthor where au_id='"+author.au_id+"')");
    while(rs1.next()){
    System.out.println(rs1.getString("title_id"));
    titles title=new titles();
    title.title_id=rs1.getString(1);
    title.title=rs1.getString(2);
    System.out.println(title.title=rs1.getString(2));
    ha.put(title.title_id,title.title);
    }
    rs1.close();
    has.put(author.au_id,ha);
    }外面一层循环好象循环一次后就有异常发生,不再做循环了
      

  5.   

    应该是数据库操作的问题,你再用一个Statement试试。ResultSet中并不直接存储查找到的结果,对象里有一个Statement,所以,两个ResultSet都指向了同一个Statement,内循环的查询影响了外循环的结果。
      

  6.   

    System.out.println(author.au_id+"  ");
    rs1=sta.executeQuery 
    ...  ...改:
    System.out.println(author.au_id+"  ");
    Statement stmt = con.createStatement(); 
    rs1=stmt.executeQuery...   ... PS: 内循环完记得马上释放stmt.
      

  7.   

    恩,可以了,是Statement的问题 谢了!!!