Java - 数据操作 - JDBC

Posted 你的踏板车要滑向哪里

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java - 数据操作 - JDBC相关的知识,希望对你有一定的参考价值。

实体类

 1 import java.util.Date;
 2 
 3 public class User {
 4     private Integer id;
 5     private String username;
 6     private Integer sex;
 7     private Integer age;
 8     private Date createDate;
 9     private Integer isDel;
10     public Integer getId() {
11         return id;
12     }
13     public void setId(Integer id) {
14         this.id = id;
15     }
16     public String getUsername() {
17         return username;
18     }
19     public void setUsername(String username) {
20         this.username = username;
21     }
22     public Integer getSex() {
23         return sex;
24     }
25     public void setSex(Integer sex) {
26         this.sex = sex;
27     }
28     public Integer getAge() {
29         return age;
30     }
31     public void setAge(Integer age) {
32         this.age = age;
33     }
34     public Date getCreateDate() {
35         return createDate;
36     }
37     public void setCreateDate(Date createDate) {
38         this.createDate = createDate;
39     }
40     public Integer getIsDel() {
41         return isDel;
42     }
43     public void setIsDel(Integer isDel) {
44         this.isDel = isDel;
45     }
46     @Override
47     public String toString() {
48         return "User [id=" + id + ", username=" + username + ", sex=" + sex + ", age=" + age + ", createDate="
49                 + createDate + ", isDel=" + isDel + "]";
50     }
51 
52 }
View Code

JDBC工具类 - 获得连接

 1 import java.sql.Connection;
 2 import java.sql.DriverManager;
 3 import java.sql.SQLException;
 4 
 5 public class DBUtil {
 6     
 7     private static final String URL = "jdbc:mysql:///imooc";
 8     private static final String USER = "root";
 9     private static final String PASSWORD = "root";
10     
11     private static Connection conn = null;
12     
13     static {
14         try {
15             //1.加载驱动程序
16             Class.forName("com.mysql.jdbc.Driver");
17             //2.获得数据库的连接
18             conn = DriverManager.getConnection(URL, USER, PASSWORD);
19         } catch (ClassNotFoundException e) {
20             e.printStackTrace();
21         } catch (SQLException e) {
22             e.printStackTrace();
23         }
24     }
25     
26     public static Connection getConn(){
27         return conn;
28     }
29     
30 }
View Code

数据持久类

  1 import java.sql.Connection;
  2 import java.sql.Date;
  3 import java.sql.PreparedStatement;
  4 import java.sql.ResultSet;
  5 import java.sql.SQLException;
  6 import java.util.ArrayList;
  7 import java.util.List;
  8 import java.util.Map;
  9 
 10 import com.imooc.db.DBUtil;
 11 import com.imooc.model.User;
 12 
 13 public class UserDao {
 14 
 15     public void addUser(User u) throws SQLException{
 16         Connection conn = DBUtil.getConn();
 17         String sql = "insert into user (user_name,sex,age,create_date,isdel) values(?,?,?,current_date(),?)";
 18         PreparedStatement ptmt = conn.prepareStatement(sql);
 19         ptmt.setString(1, u.getUsername());
 20         ptmt.setInt(2, u.getSex());
 21         ptmt.setInt(3, u.getAge());
 22         ptmt.setInt(4, u.getIsDel());
 23         ptmt.execute();
 24     }
 25     
 26     public void deleteUser(Integer id) throws SQLException{
 27         Connection conn = DBUtil.getConn();
 28         String sql = "delete from user where id=?";
 29         PreparedStatement ptmt = conn.prepareStatement(sql);
 30         ptmt.setInt(1, id);
 31         ptmt.execute();
 32     }
 33     
 34     public void updateUser(User u) throws SQLException{
 35         Connection conn = DBUtil.getConn();
 36         String sql = "update user set user_name=?,sex=?,age=?,create_date=?,isdel=? where id=?";
 37         PreparedStatement ptmt = conn.prepareStatement(sql);
 38         ptmt.setString(1, u.getUsername());
 39         ptmt.setInt(2, u.getSex());
 40         ptmt.setInt(3, u.getAge());
 41         ptmt.setDate(4, new Date(u.getCreateDate().getTime()));
 42         ptmt.setInt(5, u.getIsDel());
 43         ptmt.setInt(6, u.getId());
 44         ptmt.execute();
 45     }
 46     
 47     public List<User> conditionQuery(List<Map<String, Object>> params) throws SQLException{
 48         Connection conn = DBUtil.getConn();
 49         StringBuilder sb = new StringBuilder();
 50         sb.append("select * from user where 1=1 ");
 51         if(params!=null && params.size()>0){
 52             for (Map<String, Object> param : params) {
 53                 sb.append("and "+param.get("field")+param.get("rela")+param.get("value"));
 54             }
 55         }
 56         System.out.println(sb.toString());
 57         PreparedStatement ptmt = conn.prepareStatement(sb.toString());
 58         ResultSet rs = ptmt.executeQuery();
 59         
 60         List<User> users = new ArrayList<>();
 61         User u = null;
 62         while(rs.next()){
 63             u = new User();
 64             u.setId(rs.getInt("id"));
 65             u.setUsername(rs.getString("user_name"));
 66             u.setSex(rs.getInt("sex"));
 67             u.setAge(rs.getInt("age"));
 68             u.setCreateDate(rs.getDate("create_date"));
 69             u.setIsDel(rs.getInt("isdel"));
 70             users.add(u);
 71         }
 72         return users;
 73     }
 74     
 75     public List<User> queryAll() throws SQLException{
 76         Connection conn = DBUtil.getConn();
 77         StringBuilder sb = new StringBuilder();
 78         sb.append("select * from user");
 79         PreparedStatement ptmt = conn.prepareStatement(sb.toString());
 80         ResultSet rs = ptmt.executeQuery();
 81         List<User> users = new ArrayList<>();
 82         User u = null;
 83         while(rs.next()){
 84             u = new User();
 85             u.setId(rs.getInt("id"));
 86             u.setUsername(rs.getString("user_name"));
 87             u.setSex(rs.getInt("sex"));
 88             u.setAge(rs.getInt("age"));
 89             u.setCreateDate(rs.getDate("create_date"));
 90             u.setIsDel(rs.getInt("isdel"));
 91             users.add(u);
 92         }
 93         return users;
 94     }
 95     
 96     public User queryOne(Integer id) throws SQLException{
 97         Connection conn = DBUtil.getConn();
 98         StringBuilder sb = new StringBuilder();
 99         sb.append("select * from user where id=?");
100         PreparedStatement ptmt = conn.prepareStatement(sb.toString());
101         ptmt.setInt(1, id);
102         ResultSet rs = ptmt.executeQuery();
103         User u = new User();
104         u.setId(rs.getInt("id"));
105         u.setUsername(rs.getString("user_name"));
106         u.setSex(rs.getInt("sex"));
107         u.setAge(rs.getInt("age"));
108         u.setCreateDate(rs.getDate("create_date"));
109         u.setIsDel(rs.getInt("isdel"));
110         return u;
111     }
112     
113 }
View Code

控制类

 1 import java.sql.SQLException;
 2 import java.util.ArrayList;
 3 import java.util.HashMap;
 4 import java.util.List;
 5 import java.util.Map;
 6 
 7 import com.imooc.dao.UserDao;
 8 import com.imooc.model.User;
 9 
10 public class Action {
11 
12     private UserDao dao;
13     
14     public void add(User u) throws SQLException {
15         dao = new UserDao();
16         dao.addUser(u);
17     }
18     
19     public void delete(Integer id) throws SQLException{
20         dao = new UserDao();
21         dao.deleteUser(id);
22     }
23     
24     public void update(User u) throws SQLException{
25         dao = new UserDao();
26         dao.updateUser(u);
27     }
28     
29     public void conditionQuery() throws SQLException{
30         dao = new UserDao();
31         List<Map<String, Object>> params = new ArrayList<>();
32         Map<String, Object> param = new HashMap<>();
33         param.put("field", "user_name");
34         param.put("rela", "=");
35         param.put("value", "\'望望\'");
36         params.add(param);
37         param.put("field", "id");
38         param.put("rela", "=");
39         param.put("value", "\'2\'");
40         params.add(param);
41         
42         List<User> users = dao.conditionQuery(params);
43         for (User u : users) {
44             System.out.println(u);
45         }
46     }
47     
48     public List<User> queryAll() throws SQLException{
49         dao = new UserDao();
50         return dao.queryAll();
51     }
52     
53     public User queryOne(Integer id) throws SQLException{
54         dao = new UserDao();
55         return dao.queryOne(id);
56     }
57 
58 }
View Code

以上是关于Java - 数据操作 - JDBC的主要内容,如果未能解决你的问题,请参考以下文章

java jdbc操作数据库通用代码

JDBC快速入门,如何使用JDBC操作数据库?

jdbc

用jdbc连接oracle数据库,现在要实现数据的更新操作,在java web 项目里,求大神简单讲解下,再给出点代码

jdbc操作数据库(详细)

19-JDBC代码优化