只用JDBC技术查询表中的全部内容时,需要使用查询全部的SQL语句,把查询结果放到List集合中。
1 package qddx.JDBC; 2 import java.util.*; 3 import java.sql.*; 4 //查询操作 5 public class Query { 6 7 public List<bbsVo> showBBS(){ 8 Connection conn = null; 9 Statement st = null; 10 ResultSet rs = null; 11 List<bbsVo> list = new ArrayList<bbsVo>();//声明一个List集合,用于存放查询出的数据 12 try{ 13 conn = JDBC_Connection.getConnection();//连接数据库 14 st = conn.createStatement();//创建Statement对象 15 rs=st.executeQuery("select * from article"); 16 while(rs.next()){//结果集存在则进行遍历 17 bbsVo vo = new bbsVo(); 18 vo.setId(rs.getInt("id")); 19 vo.setPid(rs.getInt("pid")); 20 vo.setRootid(rs.getInt("rootid")); 21 vo.setCont(rs.getString("cont")); 22 vo.setPdate(rs.getTimestamp("pdate")); 23 vo.setIsleaf(rs.getInt("isleaf")); 24 vo.setTitle(rs.getString("title")); 25 26 list.add(vo);//把每次获得的对象添加到list集合中 27 } 28 }catch(SQLException e){ 29 e.printStackTrace(); 30 }finally{ 31 JDBC_Connection.free(rs, conn, st); 32 33 } 34 return list; 35 } 36 public static void main(String[] args) { 37 // TODO Auto-generated method stub 38 Query query = new Query(); 39 List<bbsVo> list = query.showBBS();//调用查询方法 40 if(list!=null){ 41 System.out.print("id "); 42 System.out.print("pid "); 43 System.out.print("rootid "); 44 System.out.print("title "); 45 System.out.print("cont "); 46 System.out.print("pdate "); 47 System.out.print("isleaf "); 48 System.out.println(); 49 for(int i=0;i<list.size();i++){ 50 System.out.print(list.get(i).getId()+"\t"); 51 System.out.print(list.get(i).getPid()+"\t"); 52 System.out.print(list.get(i).getRootid()+"\t"); 53 System.out.print(list.get(i).getTitle()+"\t"); 54 System.out.print(list.get(i).getCont()+"\t"); 55 System.out.print(list.get(i).getPdate()+"\t"); 56 System.out.print(list.get(i).getIsleaf()+"\t"); 57 System.out.println(); 58 } 59 } 60 } 61 }
时间: 2024-10-06 22:09:38