怎么将ResultSet中的数据全部遍历到集合list中

在写一个jsp页面,想把从数据库取到的值,就是ResultSet对象,全部遍历到list的集合中。不知道这个需要怎么去做....... 在写一个jsp页面,想把从数据库取到的值,就是ResultSet对象,全部遍历到list的集合中。不知道这个需要怎么去做.... 展开
 我来答
百度网友5e2b30a42
2007-06-15 · TA获得超过643个赞
知道小有建树答主
回答量:463
采纳率:0%
帮助的人:337万
展开全部
给你一段代码你就会用了!
package custom.dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import custom.struts.form.SellerForm;
import custom.struts.form.CustomForm;
import custom.dao.ConnectionManager;

public class SellerDao {
/*针对seller表*/
//1,查询
public static List findAll(Connection conn){
Statement stmt = null;
ResultSet rs = null;
List list = new ArrayList();
try{
conn = ConnectionManager.getConnection();
stmt = conn.createStatement();
rs = stmt.executeQuery("select * from seller where seller_power='2'");
while(rs.next()){
SellerForm sf = new SellerForm();
sf.setSeller_name(rs.getString("seller_name"));
sf.setSeller_psw(rs.getString("seller_psw"));
sf.setSeller_power(rs.getString("seller_power"));
sf.setSeller_id(rs.getString("seller_id"));
list.add(sf);
}
}catch(Exception e){
e.printStackTrace();
}finally{
ConnectionManager.closeConnection(conn);
}
return list;
}
public static List findId(String id){
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
List list = new ArrayList();
try{
stmt = conn.createStatement();
rs = stmt.executeQuery("select * from seller where seller_id='"+id+"'");
while(rs.next()){
SellerForm sf = new SellerForm();
sf.setSeller_id(rs.getString("seller_id"));
sf.setSeller_name(rs.getString("seller_name"));
sf.setSeller_power(rs.getString("seller_power"));
sf.setSeller_psw(rs.getString("seller_psw"));
list.add(sf);
}
}catch(Exception e){
e.printStackTrace();
}finally{
ConnectionManager.getConnection();
}
return list;
}
//2,删除
public static int delete(String id){
Connection conn = null;
Statement stmt = null;
int flag = 0;
try{
conn = ConnectionManager.getConnection();
stmt = conn.createStatement();
flag = stmt.executeUpdate("delete seller where seller_id='"+id+"'");
}catch(Exception e){
e.printStackTrace();
}finally{
ConnectionManager.getConnection();
}
return flag;
}
//3,修改
public static int update(String id,String name,String psw){
Connection conn = null;
Statement stmt = null;
int n = 0;
try{
stmt = conn.createStatement();
n = stmt.executeUpdate("update seller set seller_name='"+name+"',seller_psw='"+psw+"' where seller_id="+id);
}catch(Exception e){
e.printStackTrace();
}finally{
ConnectionManager.getConnection();
}
return n;
}
//4,插入
public static int insert(String name,String psw){
Connection conn = null;
Statement stmt = null;
int i = 0;
try{
stmt = conn.createStatement();
i = stmt.executeUpdate("insert into seller (select max(seller_id)+1 ,"+name+","+psw+",'2' from seller)");
}catch(Exception e){
e.printStackTrace();
}finally{
ConnectionManager.getConnection();
}
return i;
}

//处理登录这个模块
public static String login(String name,String psw,Connection conn){
Statement stmt = null;
ResultSet rs = null;
String power = "";
try{
stmt = conn.createStatement();
rs = stmt.executeQuery("select * from seller where seller_name='"+name+"' and seller_psw='"+psw+"'");
//如果查询出姓名密码来了就获取权限值
if(rs.next()){
power = rs.getString("seller_power");
}
}catch(Exception e){
e.printStackTrace();
}finally{
ConnectionManager.getConnection();
}
return power;
}
/*针对custom表*/
//1,查询
public static List findAllCustom(Connection conn){
Statement stmt = null;
ResultSet rs= null;
String sql1 = "";
String sql2 = "";
List list = new ArrayList();
try{
stmt=conn.createStatement();
sql1 = "select seller_name,customer_name,customer_sex,customer_email,customer_phone ";
sql2 = "from customer,seller where customer.seller_id=seller.seller_id and customer_state='1'";
rs = stmt.executeQuery(sql1+sql2);
while(rs.next()){
CustomForm c = new CustomForm();
c.setSeller_name(rs.getString("seller_name"));
c.setCustomer_name(rs.getString("customer_name"));
c.setCustomer_sex(rs.getString("customer_sex"));
c.setCustomer_email(rs.getString("customer_email"));
c.setCustomer_phone(rs.getString("customer_phone"));
list.add(c);
}
}catch(Exception e){
e.printStackTrace();
}finally{
ConnectionManager.getConnection();
}
return list;
}
public static List dispaterSeller(Connection conn){
Statement stmt = null;
ResultSet rs = null;
List list = new ArrayList();
try{
stmt = conn.createStatement();
String sql = "select * from seller where seller_power='2'";
rs = stmt.executeQuery(sql);
while(rs.next()){
SellerForm sf = new SellerForm();
sf.setSeller_name(rs.getString("seller_name"));
sf.setSeller_id(rs.getString("seller_id"));
list.add(sf);
}
}catch(Exception e){
e.printStackTrace();
}finally{
ConnectionManager.getConnection();
}
return list;
}
public static List dispaterCustom(Connection conn){
Statement stmt = null;
ResultSet rs = null;
List list = new ArrayList();
try{
stmt = conn.createStatement();
String sql = "select * from customer where customer_state='0'";
rs = stmt.executeQuery(sql);
while(rs.next()){
CustomForm cf = new CustomForm();
cf.setCustomer_name(rs.getString("customer_name"));
cf.setCustomer_id(rs.getString("customer_id"));
list.add(cf);
}
}catch(Exception e){
e.printStackTrace();
}finally{
ConnectionManager.getConnection();
}
return list;
}
public static void dispater(String sid,String cid,Connection conn,int count,int i){
Statement stmt = null;
try{
stmt = conn.createStatement();
stmt.executeUpdate("update customer set seller_id='"+sid+"',customer_state='1' where customer_id ="+cid);
}catch(Exception e){
e.printStackTrace();
}finally{
if(count==i){
ConnectionManager.getConnection();
}
}
}
}
百度网友81a639d
推荐于2018-04-12 · 超过31用户采纳过TA的回答
知道答主
回答量:102
采纳率:0%
帮助的人:82.6万
展开全部
List totalList = new ArrayList();
Statement stmt=conn.createStatement();
sql="select * from sometable";
rs=stmt.executeQuery(sql);
while(rs.next()){
List oneElementList = new ArrayList();
oneElementList.add(rs.getInt(1));
oneElementList.add(rs.getString(2));
......看你有几个columns
totalList.add(oneElementList);
}

如果没明白再问我吧

sorry,笔误 我明白cn_arthurs的意思,直接加rs都是可以的,但是安全性上会有问题
本回答被网友采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
cn_arthurs
2007-06-08 · TA获得超过438个赞
知道小有建树答主
回答量:566
采纳率:0%
帮助的人:330万
展开全部
既然使用java,最好别忘了使用他最重要的东西,对象
回答者:edisonrf - 助理 三级 6-8 11:02
说的已经很清楚了,我在这是想修改一下
List oneElementList = new ArrayList(); 去掉
在while中创建你这个表对应的对象,然后在totalList.add(obj);应该更好一些
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
拒绝孤独_
2007-06-08 · 超过13用户采纳过TA的回答
知道答主
回答量:86
采纳率:0%
帮助的人:53.2万
展开全部
while(rs.next())
{
list.additem(rs.getString("数据库里的字段"));
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
aoyupei
2007-06-08
知道答主
回答量:66
采纳率:0%
帮助的人:0
展开全部
ResultSet类的next方法,getString;ResultSet类的next方法,getString;ktiilissjvjkf
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(8)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式