Mysql Dao
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的更多相关文章
- springboot+mybatis集成多数据源MySQL/Oracle/SqlServer
日常开发中可能时常会遇到一些这样的需求,业务数据库和第三方数据库,两个或多个数据库属于不同数据库厂商,这时候就需要通过配置来实现对数据库实现多源处理.大致说一下我的业务场景,框架本身是配置的sprin ...
- Mysql分区,分库和分表
作者说的非常清楚了,感谢.地址为:http://haitian299.github.io/2016/05/26/mysql-partitioning/. 本人项目实践,使用sharding-jdbc进 ...
- JDBC与javaBean
1.JDBC的概念: Java数据库连接技术(Java DataBase Connectivity)能实现java程序对各种数据库的访问, 由一组使用java语言编写的类 和 接口(jdbc api) ...
- springboot情操陶冶-web配置(六)
本文则针对数据库的连接配置作下简单的分析,方便笔者理解以及后续的查阅 栗子当先 以我们经常用的mybatis数据库持久框架来操作mysql服务为例 环境依赖 1.JDK v1.8+ 2.springb ...
- springBoot多数据源(不同类型数据库)项目
一个基于springboot的多数据源(mysql.sqlserver)项目,先看看项目结构,注意dao层 多数据源mysql配置代码: package com.douzi.robotcenter.c ...
- spring ,springmvc,mybatis 最基本的整合,没有多余的jar包和依赖 2018.9.29日
最基本的ssm框架整合 本案例采用2018商业版intellij idea 编辑器 maven项目管理工具 tomcat8.5 接着上一篇使用springmvc最基本配置开始 https: ...
- Mybatis的SqlSession理解(二)
Mybaits加载执行该xml配置 class SqlSessionFactoryBean implements FactoryBean<SqlSessionFactory>, Initi ...
- Spring项目的配置文件们(web.xml context servlet springmvc)
我们的spring项目目前用到的配置文件包括1--web.xml文件,这是java的web项目的配置文件.我理解它是servlet的配置文件,也就是说,与spring无关.即使你开发的是一个纯粹jsp ...
- 【异常】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 ...
- 【异常】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 ...
随机推荐
- USB设备判断接入和移除
目录 以沁恒的CH582芯片为例,主机模式下,在R8_USB_INT_EN中可以使能RB_UIE_DETECT位,由中断来提醒检测USB设备的接入和移除:从机模式下,USB设备没有这样的中断功能(上述 ...
- 蓝牙mesh组网实践(手机配网例程改低功耗)
目录 在22年7月版本的CH583EVT更新之后,582芯片的adv_vendor_self_provision_with_peripheral例程,适配了wch mesh手机app,支持了OTA,成 ...
- lowcodeEngine 组件面板的拖拽功能
设计器和渲染器处在不同的 Frame 渲染器以单独的 iframe 嵌入,xxx-simulator-renderer 通过和 host进行通信来和设计器打交道,比如点击渲染画布任意一个位置,需要能计 ...
- ubuntu 20.04使用kubeadm安装k8s集群
本文主要用于记录,步骤参考了:https://blog.csdn.net/weixin_44559544/article/details/123381441 一.设备相关准备 1.修改节点主机名,这样 ...
- sentinel监控数据持久化&本地测试
官方文档 https://sentinelguard.io/zh-cn/ wiki: 在生产环境中使用-Sentinel 推荐方案:持久化到 时序数据库InfluxDB : 结合Grafana 可 ...
- 25_Webapck原理
Webpack源代码解析 webpack其实也就是一个函数的调用,返回一个Compile的对象,再调用Compile的run方法就可以完成项目的构建 那么我们肯定是先要从webpack这个函数去理解它 ...
- json类型数据取出想要的部分
因为才疏学浅,只能用很笨的方法. 以下是我拿到的数据的json型数据. {"result":{"ingredient":{"result": ...
- 实验1task1
<实验结论> #include <stdio.h> #include <stdlib.h> int main() { printf(" O \n&qu ...
- docker systemctl start报错: Failed to get D-Bus connection: Operation not permitted
转载自:https://blog.csdn.net/zhenliang8/article/details/78330658 最近使用docker部署ansible,安装ssh 遇到启动服务报错:Fai ...
- [Oracle19C 数据库管理] 管理存储与表空间
存储概览 存储的架构 Control File:储存了数据物理存储的信息.存在多个副本来避免单点故障.没有控制文件,数据库无法打开. DATA File: 存储用户与应用的信息,以及元数据与数据字典. ...