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 }

Mysql Dao的更多相关文章

  1. springboot+mybatis集成多数据源MySQL/Oracle/SqlServer

    日常开发中可能时常会遇到一些这样的需求,业务数据库和第三方数据库,两个或多个数据库属于不同数据库厂商,这时候就需要通过配置来实现对数据库实现多源处理.大致说一下我的业务场景,框架本身是配置的sprin ...

  2. Mysql分区,分库和分表

    作者说的非常清楚了,感谢.地址为:http://haitian299.github.io/2016/05/26/mysql-partitioning/. 本人项目实践,使用sharding-jdbc进 ...

  3. JDBC与javaBean

    1.JDBC的概念: Java数据库连接技术(Java DataBase Connectivity)能实现java程序对各种数据库的访问, 由一组使用java语言编写的类 和 接口(jdbc api) ...

  4. springboot情操陶冶-web配置(六)

    本文则针对数据库的连接配置作下简单的分析,方便笔者理解以及后续的查阅 栗子当先 以我们经常用的mybatis数据库持久框架来操作mysql服务为例 环境依赖 1.JDK v1.8+ 2.springb ...

  5. springBoot多数据源(不同类型数据库)项目

    一个基于springboot的多数据源(mysql.sqlserver)项目,先看看项目结构,注意dao层 多数据源mysql配置代码: package com.douzi.robotcenter.c ...

  6. spring ,springmvc,mybatis 最基本的整合,没有多余的jar包和依赖 2018.9.29日

    最基本的ssm框架整合 本案例采用2018商业版intellij  idea  编辑器    maven项目管理工具  tomcat8.5 接着上一篇使用springmvc最基本配置开始 https: ...

  7. Mybatis的SqlSession理解(二)

    Mybaits加载执行该xml配置 class SqlSessionFactoryBean implements FactoryBean<SqlSessionFactory>, Initi ...

  8. Spring项目的配置文件们(web.xml context servlet springmvc)

    我们的spring项目目前用到的配置文件包括1--web.xml文件,这是java的web项目的配置文件.我理解它是servlet的配置文件,也就是说,与spring无关.即使你开发的是一个纯粹jsp ...

  9. 【异常】java.sql.SQLException: Could not retrieve transaction read-only status from server Query

    1 详细异常 java.sql.SQLException: Could not retrieve transaction read-only status , ], [ChargingOrderRea ...

  10. 【异常】update更新java.sql.SQLException: Duplicate entry '2019-07-30 00:00:00-110100' for key

    1 详细异常信息 User class threw exception: java.sql.SQLException: Duplicate entry '2019-07-30 00:00:00-110 ...

随机推荐

  1. laravel 服务容器,容器概念

    Laravel 服务容器 发现一篇讲服务容器的文章,讲的很好,转载过来laravel 服务容器 实质是工厂模式的升级,类的传递动态加载 ####以下内容转载 容器,字面上理解就是装东西的东西.常见的变 ...

  2. 069_VisualForce Ajax 更多用法解读02

    一.同步调用的异常处理:AJAX Toolkit  try catch... <html> <head> <script src="/soap/ajax/44. ...

  3. GitLab-Runner安装及使用

    Git_Lab CI Setting 根据该教程本地下载并安装 gitlab-runner.exe 在GitLab远程仓库页面点击 setting-> CI/CD ->runner Exp ...

  4. 转发-》c++ stl multimap基本操作使用技巧详细介绍

    c++ stl multimap基本操作使用技巧详细介绍 C++ stl  Multimap 和C++ stl  map 很相似,但是MultiMap允许重复的元素. C++ stl  Multima ...

  5. tomcat程序闪退,如何让tomcat不闪退,可以看见报错

    1.在 startup.bat 文件最后加 pause 2.cd 到tomcat bin 目录下执行 startup.bat 3.直接去Tomcat目录下的log看输出日志,即catalina.out ...

  6. htmlApk:一个轻松在线打包生成双端app应用的网站

    htmlApk平台,基于HTML打包制作APP,只要有手机网站或者HTML文件,3分钟即可生成app多端应用,同时拥有苹果版和安卓版双系统APP,快速进入移动互联时代. 全面支持h5网址打包,可设置图 ...

  7. 12组-Beta冲刺-2/5

    一.基本情况 队名:字节不跳动 组长博客:https://www.cnblogs.com/147258369k/p/15594989.html Github链接:https://github.com/ ...

  8. CentOS 7 安装步骤以及初始化

    2. 虚拟机分配的资源 因为用的软件不一样,这里设置方法无法截图,但大至如下: 2CPU/1G内存/200G硬盘 去掉打印机等没用的硬件(macOS要去掉打印机和摄像头) 光盘开始选择空白光盘,不要在 ...

  9. QTcpSocket 设置接收数据延时等待时间

    /* 客户端接入槽函数 */ void TcpServer::slotNewConnect(void) {/* 获取连接的客户端句柄 这里设置刷新数据时间 1ms */ QTcpSocket *pSo ...

  10. 日記かな、自分のサーバ作りの?(01、try…catch…の問題)

    今日まだ終わらないうちに.昼間から出会ったbugについて.取りまとめましょう. 一応try-catch-にかかわる問題かな- try(do something){ }catch{ } と try(){ ...