1 package dao;
2
3 import java.sql.Connection;
4 import java.sql.PreparedStatement;
5 import java.sql.ResultSet;
6 import java.sql.SQLException;
7 import java.util.ArrayList;
8 import java.util.List;
9
10 import entity.User;
11 import util.DButils;
12
13 public class UserDAO {
14
15 //DAO类:用于封装数据访问逻辑
16
17 public User find(String username) throws Exception {
18 User user = null;
19 Connection conn=null;
20 try {
21 conn=DButils.getConnection();
22 String sql="SELECT * FROM t_user WHERE username = ?";
23 PreparedStatement ps=conn.prepareStatement(sql);
24 ps.setString(1, username);
25 ResultSet rs=ps.executeQuery();
26
27 while(rs.next()) {
28
29 user=new User();
30 user.setUsername(username);
31 user.setId(rs.getInt("id"));
32 user.setPhone(rs.getString("phone"));
33 user.setPwd(rs.getString("pwd"));
34
35 }
36
37 } catch (SQLException e) {
38 e.printStackTrace();
39 throw new RuntimeException(e);
40 }finally {
41 DButils.closeConnection(conn);
42 }
43 return user;
44 }
45
46 public void delete(int id) throws Exception {
47 Connection conn=null;
48
49 try {
50 conn=DButils.getConnection();
51 String sql="delete from t_user where id=?";
52 PreparedStatement ps=conn.prepareStatement(sql);
53 ps.setInt(1, id);
54 ps.executeUpdate();
55
56 } catch (SQLException e) {
57 e.printStackTrace();
58 throw new RuntimeException(e);
59 }
60 }
61
62 public void save(User user) throws Exception {
63 Connection conn=null;
64
65 try {
66 conn=DButils.getConnection();
67 String sql="insert into t_user"
68 +"(username,pwd,phone)"
69 +"VALUES(?,?,?)";
70 PreparedStatement ps=conn.prepareStatement(sql);
71 ps.setString(1, user.getUsername());
72 ps.setString(2, user.getPwd());
73 ps.setString(3, user.getPhone());
74 ps.executeUpdate();
75 } catch (SQLException e) {
76 e.printStackTrace();
77 throw new RuntimeException(e);
78 }finally {
79 DButils.closeConnection(conn);
80 }
81 }
82
83 public List<User> findAll() throws Exception{
84 List<User> users=new ArrayList<User>();
85 Connection conn=null;
86
87 try {
88 conn=DButils.getConnection();
89 String sql="select * from t_user";
90 PreparedStatement ps=conn.prepareStatement(sql);
91 ResultSet rs=ps.executeQuery();
92
93 while(rs.next()){
94 int id=rs.getInt("id");
95 String username=rs.getString("username");
96 String pwd=rs.getString("pwd");
97 String phone=rs.getString("phone");
98
99 User user=new User();
100 user.setId(id);
101 user.setUsername(username);
102 user.setPwd(pwd);
103 user.setPhone(phone);
104
105 users.add(user);
106 }
107 } catch (SQLException e) {
108 e.printStackTrace();
109 throw new RuntimeException(e);
110 }finally{
111 DButils.closeConnection(conn);
112 }
113 return users;
114 }
115
116 }

最新文章

  1. DB2 中文排序问题
  2. Codeforces 740C. Alyona and mex 思路模拟
  3. Android添加快捷方式(Shortcut)到手机桌面
  4. [Noi2015]软件包管理器 题解
  5. 【液晶模块系列基础视频】4.2.X-GUI图形界面库-画矩形函数简介
  6. swiper轮播图--兼容IE8
  7. The life of an HTML HTTP request
  8. Modified LCS
  9. c++ 私有函数 头文件设计
  10. win7系统64位eclipse环境超详细暗黑1.4服务器搭建
  11. kiki&#39;s game
  12. [补档][Tyvj 1518]CPU监控
  13. 简单的用js打印乘法口诀表
  14. 用Composer获取第三方资源总是失败咋办?
  15. 痞子衡嵌入式:恩智浦i.MXRT系列微控制器量产神器RT-Flash用户指南
  16. 小白Monkey学习笔记
  17. Fusebox 类似WEBPACK 的工具,React Studio
  18. HttpWebRequest 高效并发问题
  19. python基础之单例设计模式
  20. Darwin Streaming Server for Windows 安装

热门文章

  1. 题解[CF575E]Spectator_Riots
  2. 超级详细的Vue安装与配置教程
  3. git 修改commit 备注
  4. memoのPython和3D那点事
  5. 实验一-Password engine-加密API研究
  6. Python 使用mysql.connector、pymysql和 MYSQLdb(MysqlClient)操作MySQL数据库
  7. echarts柱状图快速上手笔记地址
  8. GoLang之ACM控制台输入输出
  9. [部署日记]GO在Visual Studio Code初次运行时提示The &quot;gopls&quot; command is not available. Run &quot;go get -v golang.org/x/tools/gopls&quot; to install.
  10. Redis 实战(一)AOF 持久化配置和数据恢复