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. 《CSOL大灾变》Mobile开发进度记录——扔掉与拾取武器的逻辑

    在武器系统的开发过程中,涉及到武器的丢弃逻辑.由于场景是复制场景,而自己写碰撞测试和抛物线以及重力下落来模拟扔掉一把武器,并且要防止武器扔到墙里.如果自己实现这些逻辑,那么会占用渲染线程的时间开销,即 ...

  2. mysql窗口函数

    使用MySQL开窗函数之前一定先确定当前数据库版本是否支持,因为只有MySQL8.0以上的版本才支持开窗函数 用navicat如何查看MySQL的版本的方法: 在出现的界面输入命令  select v ...

  3. mybatis读取blob类型

    mybatis 读取blob数据 mybatis读取blob数据过程: 1.从数据库中读出blob数据类型,用pojo中的byte[]接收. 2.把文件保存成文件(或者变成base64也行). Cus ...

  4. tesseract-ocr 安装、语言库、使用 随记

    前几日才听说ocr的图片识别功能.觉得很有意思.先体验一下. 地址: GitHub - tesseract-ocr/tesseract: Tesseract Open Source OCR Engin ...

  5. 利用shell脚本来监控linux系统的负载与CPU占用情况

    一.安装linux下面的一个邮件客户端msmtp软件(类似于一个foxmail的工具) 1.下载安装: http://downloads.sourceforge.net/msmtp/msmtp-1.4 ...

  6. 【PS】PS如何扩展画布?

    [PS]PS如何扩展画布? 选择裁剪,拉伸图片 选择上方工具栏的勾 即可扩展图片

  7. centos7.8 安装 redis5.0.2

    1.安装gcc依赖 redis是由C语言开发,因此安装之前必须要确保服务器已经安装了gcc,可以通过如下命令查看机器是否安装: gcc -v 如果没有安装则通过以下命令安装: yum install ...

  8. JupyterNotebook开发介绍

    简单介绍 核心目录在notebook下面,主页面在tempaltes目录下的notebook.html文件,没有用到传统的前端开发技术,还是jquery之类的前端,而且用了非常多的类库,开发环境的搭建 ...

  9. C#中socket的简单使用

    一.Socket的概念Socket其实并不是一个协议,而是为了方便使用TCP或UDP而抽象出来的一层,是位于应用层和传输控制层之间的一组接口. 当两台主机通信是,必须通过Socket连接,Socket ...

  10. Q查询和F查询

    F查询与Q查询 F查询 Django 提供 F() 来做这样的比较.F() 的实例可以在查询中引用字段,来比较同一个 model 实例中两个不同字段的值. # 查询评论数大于收藏数的书籍 from d ...