Hashtable问题?

zyfire88 2006-01-11 12:40:22
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();
}
}


不知道哪出了错,请大家帮忙,谢!
...全文
152 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
zyfire88 2006-01-11
  • 打赏
  • 举报
回复
恩,可以了,是Statement的问题 谢了!!!
yuzl32 2006-01-11
  • 打赏
  • 举报
回复
System.out.println(author.au_id+" ");
rs1=sta.executeQuery
... ...

改:
System.out.println(author.au_id+" ");
Statement stmt = con.createStatement();
rs1=stmt.executeQuery

... ...

PS: 内循环完记得马上释放stmt.
gongxsh00 2006-01-11
  • 打赏
  • 举报
回复
应该是数据库操作的问题,你再用一个Statement试试。ResultSet中并不直接存储查找到的结果,对象里有一个Statement,所以,两个ResultSet都指向了同一个Statement,内循环的查询影响了外循环的结果。
zyfire88 2006-01-11
  • 打赏
  • 举报
回复
我不是连接数据库的问题,是
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);
}

外面一层循环好象循环一次后就有异常发生,不再做循环了
franckjun 2006-01-11
  • 打赏
  • 举报
回复
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
看看有没有sun.jdbc.odbc.JdbcOdbcDriver这个驱动,用这个或许可以。
zyfire88 2006-01-11
  • 打赏
  • 举报
回复
不好意思

这是错误信息:
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)
chg2008 2006-01-11
  • 打赏
  • 举报
回复
晕死,好歹把错误提示给弄出来吧
xiaobzg 2006-01-11
  • 打赏
  • 举报
回复
什么异常?runtime异常这里不好判断,用debug查
另外 rs.close();
con.close();
写在finally里
kingofhawks 2006-01-11
  • 打赏
  • 举报
回复
老大,什么错都不说,让人家怎么帮你看哪出错阿?

62,624

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧