只用JDBC技术查询表中的全部内容时,需要使用查询全部的SQL语句,把查询结果放到List集合中。
package qddx.JDBC;
import java.util.*;
import java.sql.*;
//查询操作
public class Query { public List<bbsVo> showBBS(){
Connection conn = null;
Statement st = null;
ResultSet rs = null;
List<bbsVo> list = new ArrayList<bbsVo>();//声明一个List集合,用于存放查询出的数据
try{
conn = JDBC_Connection.getConnection();//连接数据库
st = conn.createStatement();//创建Statement对象
rs=st.executeQuery("select * from article");
while(rs.next()){//结果集存在则进行遍历
bbsVo vo = new bbsVo();
vo.setId(rs.getInt("id"));
vo.setPid(rs.getInt("pid"));
vo.setRootid(rs.getInt("rootid"));
vo.setCont(rs.getString("cont"));
vo.setPdate(rs.getTimestamp("pdate"));
vo.setIsleaf(rs.getInt("isleaf"));
vo.setTitle(rs.getString("title")); list.add(vo);//把每次获得的对象添加到list集合中
}
}catch(SQLException e){
e.printStackTrace();
}finally{
JDBC_Connection.free(rs, conn, st); }
return list;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Query query = new Query();
List<bbsVo> list = query.showBBS();//调用查询方法
if(list!=null){
System.out.print("id ");
System.out.print("pid ");
System.out.print("rootid ");
System.out.print("title ");
System.out.print("cont ");
System.out.print("pdate ");
System.out.print("isleaf ");
System.out.println();
for(int i=0;i<list.size();i++){
System.out.print(list.get(i).getId()+"\t");
System.out.print(list.get(i).getPid()+"\t");
System.out.print(list.get(i).getRootid()+"\t");
System.out.print(list.get(i).getTitle()+"\t");
System.out.print(list.get(i).getCont()+"\t");
System.out.print(list.get(i).getPdate()+"\t");
System.out.print(list.get(i).getIsleaf()+"\t");
System.out.println();
}
}
}
}