您的位置:程序门 -> java -> j2se / 扩展类



resultset 如果何转换成数组?


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


resultset 如果何转换成数组?
发表于:2007-07-29 12:00:20 楼主
小弟现在想将rs转换成数组,然后在connection   关闭的情况下,可以return数据!
发表于:2007-07-30 10:25:371楼 得分:0
数组不好用,还不如返回list(其中元素为hashtable--[字段名,值])
hashtable   item   =   null;
arraylist   list   =   new   arraylist();
while(rs.next())   {
    item   =   new   hashtable();
    item.put( "field_a ",   rs.getstring(0));
    item.put( "field_ba ",   rs.getstring(1));
    item.put( "field_c ",   rs.getstring(2));
    list.add(item);
}
return   list;
发表于:2007-07-30 12:50:302楼 得分:0
:)
hashtable   item   =   null;
while(rs.next())   {
    item   =   new   hashtable();
    item.put( "field_a ",   rs.getstring(0));
    item.put( "field_ba ",   rs.getstring(1));
    item.put( "field_c ",   rs.getstring(2));
    list.add(item);
}
return   item;
发表于:2007-07-30 12:56:013楼 得分:0
不好意思,不小心发了,
比较同意一楼的,换成hashmap是不是要好一点
发表于:2007-07-30 13:58:034楼 得分:0
不过按照   orm   的思想,应该使用一个   javabean,其中的每一个属性对应着数据表中的字段,通过逐一赋值,再将其添加到   list   会比较好些

...

list <student>   list   =   new   arraylist <student> ();

while(rs.next())   {
  student   stu   =   new   student();
  stu.setid   =   rs.getstring( "id ");
  stu.setclass   =   rs.getstring( "class ");
  stu.setname   =   rs.getstring( "name ");
  ...
  list.add(stu);
}
...
发表于:2007-07-30 13:58:555楼 得分:0
抱歉写错了,改一下:

...

list <student>   list   =   new   arraylist <student> ();

while(rs.next())   {
  student   stu   =   new   student();
  stu.setid(   rs.getstring( "id ")   );
  stu.setclass(   rs.getstring( "class ")   );
  stu.setname(   rs.getstring( "name ")   );
  ...
  list.add(stu);
}
...
发表于:2007-07-30 14:18:326楼 得分:0
如果对页面的外观要求不是那么高的话,用hashtable可以作出很通用的页面来,也就是jsp用一段代码就能够显示不同实体的结果列表.
而且在服务端也能开发出很通用的功能来
发表于:2007-07-30 15:05:037楼 得分:0
要在connection关闭的情况下返回数据   可以用行集吧?
发表于:2007-07-31 00:31:208楼 得分:0
sureyor()   没用过jdbc??
item.put( "field_a ",   rs.getstring(0));
<=> item.put( "field_a ",   rs.getstring(1));
发表于:2007-07-31 09:21:009楼 得分:0
-_-!!
jdbc   n年不用啦,现在一般都用成型的o/r   mapping,比如hibernate,其中基本上不需要自己对resultset进行操作...
发表于:2007-07-31 12:11:3510楼 得分:0
遍历呗
发表于:2007-08-01 15:04:5011楼 得分:0
该回复于2007-12-28 12:05:45被管理员或版主删除


快速检索

最新资讯
热门点击