发布网友 发布时间:2022-04-09 00:14
共2个回答
懂视网 时间:2022-04-09 04:35
package qddx.JDBC; 2 import java.sql.*; 3 public class QueryById { 4 5 public bbsVo QuerybbsVoById(int id){ 6 bbsVo vo = null; 7 Connection conn = null; 8 PreparedStatement pst = null; 9 ResultSet rs = null; 10 try{ 11 conn = JDBC_Connection.getConnection(); 12 pst = conn.prepareStatement("select * from article where id=?"); 13 pst.setInt(1, id);//设置条件id 14 rs=pst.executeQuery(); 15 while(rs.next()){//结果集存在则进行遍历 16 vo = new bbsVo(); 17 vo.setId(rs.getInt("id")); 18 vo.setPid(rs.getInt("pid")); 19 vo.setRootid(rs.getInt("rootid")); 20 vo.setCont(rs.getString("cont")); 21 vo.setPdate(rs.getTimestamp("pdate")); 22 vo.setIsleaf(rs.getInt("isleaf")); 23 vo.setTitle(rs.getString("title")); 24 25 } 26 }catch(SQLException e){ 27 e.printStackTrace(); 28 }finally{ 29 JDBC_Connection.free(rs, conn, pst); 30 } 31 return vo; 32 33 } 34 public static void main(String[] args) { 35 // TODO Auto-generated method stub 36 QueryById byid = new QueryById(); 37 int id = 3; 38 bbsVo vo = byid.QuerybbsVoById(id); 39 if(vo!=null){ 40 System.out.print("id "); 41 System.out.print("pid "); 42 System.out.print("rootid "); 43 System.out.print("title "); 44 System.out.print("cont "); 45 System.out.print("pdate "); 46 System.out.print("isleaf "); 47 System.out.println(); 48 System.out.print(vo.getId()+" "); 49 System.out.print(vo.getPid()+" "); 50 System.out.print(vo.getRootid()+" "); 51 System.out.print(vo.getTitle()+" "); 52 System.out.print(vo.getCont()+" "); 53 System.out.print(vo.getPdate()+" "); 54 System.out.print(vo.getIsleaf()+" "); 55 }else{ 56 System.out.println("id为"+id+" 的用户不存在"); 57 } 58 } 59 60 }
JDBC查询指定条件的数据
标签:
热心网友 时间:2022-04-09 01:43
用eclipse