为什么执行的时候总是有例外?

scarletg 2003-11-04 05:34:25
import java.util.*;
import java.sql.*;
public class select
{
Hashtable records[];
int columns=0;
int current=0;
int max=0;
Connection con;
String sql;

public select(Connection con, String sql){//构造方法
int index=0;
this.con=con;
this.sql=sql;
try{
Statement stmt=this.con.createStatement();
ResultSet rs=stmt.executeQuery(this.sql);
ResultSetMetaData md=rs.getMetaData();
this.columns=md.getColumnCount();
Hashtable records[]=new Hashtable[this.columns];
while(rs.next()){
index++;
for(int i=0;i<columns;i++){
if(index==1){
records[i]=new Hashtable();
}
records[i].put(new Integer(index),rs.getString(i+1));
System.out.println(rs.getString(i+1));
}
}
max=index;
}catch(SQLException e){
//max=-1;
}
}
public String getItem(int row,int column){
System.out.println(max);
if ((row<1)||(row>max)){
return "错了";
}
if ((column<1)||(column>this.columns)){
return "嘿嘿 ";
}

return(String)records[column-1].get(new Integer(row));
}
}
我在调用select.getItem(int ,int)的时候,总是出错,
原因是在select的构造方法里,捕捉了例外,从例外里跳出来了.
...全文
63 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
scarletg 2003-11-06
  • 打赏
  • 举报
回复
现在没有exception了,但是每次hashtable取数据得那个循环,就只取一次,就不再取了
binny 2003-11-05
  • 打赏
  • 举报
回复
把你的Exception贴出来好吗?

另外
你先
Hashtable records[];

Hashtable records[]=new Hashtable[this.columns];
这是你想要的吗?
scarletg 2003-11-05
  • 打赏
  • 举报
回复
no, 我就是不知道错在什么地方了
lllmacro 2003-11-05
  • 打赏
  • 举报
回复
解决了没有啊?
scarletg 2003-11-05
  • 打赏
  • 举报
回复
key-value
是对的啊
zhanghuazhanghome 2003-11-04
  • 打赏
  • 举报
回复
Hashtable中的键-值对能为对象吗?
put(new Integer(index),rs.getString(i+1));
scarletg 2003-11-04
  • 打赏
  • 举报
回复
你们说得都不对
我这个是类阿
什么初始不初始的
还没有实例化那,
我调试过后,知道错在什么地方了,就是
records[i].put(new Integer(index),rs.getString(i+1));
这句话出错了,但是错在什么地方那?我就不知道了
caojinfeng 2003-11-04
  • 打赏
  • 举报
回复
你有调用构造函数吗,注意,必须调用你写的那个带参数的构造函数.
yangtaylor 2003-11-04
  • 打赏
  • 举报
回复
我记忆中Connection con需要给初值null。不然的话就是声明,而没有初始化,也就不能直接赋值,于是this.con=con就错误了。只有基本类不需要初始化。
其他地方就没有细看了。估计也就是这个错误。
yugona 2003-11-04
  • 打赏
  • 举报
回复
看错了,不好意思
yugona 2003-11-04
  • 打赏
  • 举报
回复
那个index来的莫名其妙!
zhanghuazhanghome 2003-11-04
  • 打赏
  • 举报
回复
只有select * from table之类的查询才能用executeQuery(),你的SQL语句是吗?

62,614

社区成员

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

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