码迷,mamicode.com
首页 > 数据库 > 详细

JDBC查询指定条件的数据

时间:2016-05-07 22:20:55      阅读:333      评论:0      收藏:0      [点我收藏+]

标签:

使用select语句的条件查询,需要用到where子句。

 1 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\t");
41             System.out.print("pid\t");
42             System.out.print("rootid\t");
43             System.out.print("title\t\t");
44             System.out.print("cont\t");
45             System.out.print("pdate\t");
46             System.out.print("isleaf\t");
47             System.out.println();
48             System.out.print(vo.getId()+"\t");
49             System.out.print(vo.getPid()+"\t");
50             System.out.print(vo.getRootid()+"\t");
51             System.out.print(vo.getTitle()+"\t");
52             System.out.print(vo.getCont()+"\t");
53             System.out.print(vo.getPdate()+"\t");
54             System.out.print(vo.getIsleaf()+"\t");
55         }else{
56             System.out.println("id为"+id+" 的用户不存在");
57         }
58     }
59 
60 }

 

JDBC查询指定条件的数据

标签:

原文地址:http://www.cnblogs.com/maple-shanqiu/p/5469244.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!