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

2024年11月29日 11:53
有5个网友回答
网友(1):

给你一段代码你就会用了!
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();
}
}
}
}

网友(2):

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都是可以的,但是安全性上会有问题

网友(3):

既然使用java,最好别忘了使用他最重要的东西,对象
回答者:edisonrf - 助理 三级 6-8 11:02
说的已经很清楚了,我在这是想修改一下
List oneElementList = new ArrayList(); 去掉
在while中创建你这个表对应的对象,然后在totalList.add(obj);应该更好一些

网友(4):

while(rs.next())
{
list.additem(rs.getString("数据库里的字段"));
}

网友(5):

ResultSet类的next方法,getString;ResultSet类的next方法,getString;ktiilissjvjkf