package cn.jy.demo;
import java.sql.Connection;
import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import java.util.ArrayList;import java.util.List;import java.util.Scanner;public class storetraverse {
public static void main(String[] args) throws SQLException { fun1(); //fun2(); } //通过遍历将表格中的数据存放到zhangwu1对象中(根据zhangwu1的有参构造)然后在将其存放到集合中 最后对集合进行遍历 输出结果 public static void fun1() throws SQLException{ Connection con=JDBCUtilsConfig.getConnection(); //连接 其中JDBCUtilsConfig为我自己为方便连接封装的类 String sql="SELECT * FROM zhangwu1"; //遍历我们创建的表格 PreparedStatement pst=con.prepareStatement(sql); //执行平台对象 ResultSet rs= pst.executeQuery(); //将查询的信息存放到ResultSet集合中 List<zhangwu1> list = new ArrayList<>();//创建集合 为之后的数据存入到集合中方便 while(rs.next()){ zhangwu1 zw=new zhangwu1(rs.getInt("id"),rs.getString("flname"),rs.getDouble("money"),rs.getString("zhanghu"),rs.getString("date"),rs.getString("description")); list.add(zw); } for(zhangwu1 zw:list){ System.out.println(zw); } JDBCUtilsConfig.close(con, pst,rs); } //通过构造方法将一组数据用new对象的方式存入到集合中 先遍历 让后根据输入将该组数据输出 public static void fun2(){ List<zhangwu1> list=new ArrayList<>(); list.add(new zhangwu1(11,"女警",999,"4566","2015-6","优秀")); list.add(new zhangwu1(12,"寒冰",599,"4567","2015-6","优秀")); list.add(new zhangwu1(13,"女警1",699,"4568","2015-6","优秀")); list.add(new zhangwu1(14,"女警2",799,"4569","2015-6","优秀")); list.add(new zhangwu1(15,"女警3",199,"4576","2015-6","优秀")); list.add(new zhangwu1(16,"女警4",99,"4598","2015-6","优秀")); for(zhangwu1 zw:list){ System.out.println(zw); } Scanner sc=new Scanner(System.in); System.out.println("请输入大于11小于16的整数"); int i=sc.nextInt(); for(int j=0;j<list.size();j++){ zhangwu1 item=list.get(j); if(item.getId()==i){ System.out.println(item.getId()+" "+item.getFlname()+" "+item.getMoney()); } } sc.close(); }}