当前位置:网站首页>Dao与实体类的封装
Dao与实体类的封装
2022-06-21 17:12:00 【LvhaoIT】
Dao与实体类的封装
一、jdbc工具类封装
package JDBC.Dao;
import java.sql.*;
public class JdbcUtil {
private Connection conn = null;
private PreparedStatement ps = null;
static {
try {
//注册驱动
Class.forName("com.mysql.cj.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
//封装conn对象创建
public Connection createCon() {
try {
conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/csdb?serverTimezone=UTC", "root", "lh051920");
System.out.println("conn对象创建成功!");
} catch (SQLException throwables) {
throwables.printStackTrace();
System.out.println("conn对象创建失败!");
}
return conn;
}
//封装PreparedStatement对象
public PreparedStatement createStatement(String sql) {
Connection conn = createCon();
try {
ps = conn.prepareStatement(sql);
} catch (SQLException throwables) {
throwables.printStackTrace();
}
return ps;
}
//封装销毁
public void close(ResultSet rs) {
try {
if (rs != null)
rs.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
public void close() {
try {
if (ps != null)
ps.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
try {
if (conn != null)
conn.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
二、DeptDao的封装
package JDBC.Dao;
import JDBC.entity.Dept;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
/** * 封装dept表的操作 */
public class DeptDao {
private JdbcUtil util = new JdbcUtil();
public int add(String deptno, String dname, String loc) {
String sql = "insert into dept (deptno,dname,loc) values(?,?,?)";
PreparedStatement ps = this.util.createStatement(sql);
int result = 0;
try {
ps.setInt(1, Integer.valueOf(deptno));
ps.setString(2, dname);
ps.setString(3, loc);
result = ps.executeUpdate();
} catch (SQLException throwables) {
throwables.printStackTrace();
} finally {
util.close();
}
return result;
}
public int delete(String deptno) {
String sql = "delete from dept where deptno=?";
PreparedStatement ps = this.util.createStatement(sql);
int result = 0;
try {
ps.setInt(1, Integer.valueOf(deptno));
result = ps.executeUpdate();
} catch (SQLException throwables) {
throwables.printStackTrace();
} finally {
util.close();
}
return result;
}
public int update(String deptno, String dname, String loc) {
String sql = "update dept set dname=?,loc=? where deptno=?";
PreparedStatement ps = this.util.createStatement(sql);
int result = 0;
try {
ps.setString(1, dname);
ps.setString(2, loc);
ps.setInt(3, Integer.valueOf(deptno));
result = ps.executeUpdate();
} catch (SQLException throwables) {
throwables.printStackTrace();
} finally {
util.close();
}
return result;
}
public List select() {
List list = new ArrayList();
String sql = "select * from dept";
ResultSet rs = null;
PreparedStatement ps = this.util.createStatement(sql);
try {
rs = ps.executeQuery();
while (rs.next()) {
int deptno = rs.getInt("deptno");
String dname = rs.getString("dname");
String loc = rs.getString("loc");
list.add(new Dept(deptno, dname, loc));
}
} catch (SQLException throwables) {
throwables.printStackTrace();
} finally {
util.close(rs);
}
return list;
}
}
三、Dept数据表的封装
package JDBC.entity;
//表实体
public class Dept {
private int deptno;
private String dname;
private String loc;
public Dept(int deptno, String dname, String loc) {
this.deptno = deptno;
this.dname = dname;
this.loc = loc;
}
public Dept() {
}
public int getDeptno() {
return deptno;
}
public void setDeptno(int deptno) {
this.deptno = deptno;
}
public String getDname() {
return dname;
}
public void setDname(String dname) {
this.dname = dname;
}
public String getLoc() {
return loc;
}
public void setLoc(String loc) {
this.loc = loc;
}
}
四、测试代码
package JDBC.Test;
import JDBC.Dao.DeptDao;
import JDBC.entity.Dept;
import java.util.List;
import java.util.Scanner;
public class 部门管理 {
static Scanner in = new Scanner(System.in);
public static void main(String[] args) {
menu();
}
private static void menu() {
int flag;
String deptno = null;
String dname = null;
String loc = null;
DeptDao dept = new DeptDao();
System.out.println("=====部门管理系统=====");
System.out.println("===1.添加部门========");
System.out.println("===2.删除部门========");
System.out.println("===3.更新部门========");
System.out.println("===4.查询所有部门=====");
System.out.println("===0.退出系统========");
System.out.print("请选择功能:");
flag = in.nextInt();
in.nextLine();
int result = 0;
switch (flag) {
case 1:
System.out.print("请输入部门号:");
deptno = in.nextLine();
System.out.print("请输入部门名称:");
dname = in.nextLine();
System.out.print("请输入地址:");
loc = in.nextLine();
result = dept.add(deptno, dname, loc);
if (result == 1)
System.out.println("添加部门成功!");
else
System.out.println("添加部门失败!");
break;
case 2:
System.out.print("请输入部门号:");
deptno = in.nextLine();
result = dept.delete(deptno);
if (result == 1)
System.out.println("删除部门成功!");
else
System.out.println("删除部门失败!");
break;
case 3:
System.out.print("请输入部门号:");
deptno = in.nextLine();
System.out.print("请输入部门名称:");
dname = in.nextLine();
System.out.print("请输入地址:");
loc = in.nextLine();
result = dept.update(deptno, dname, loc);
if (result == 1)
System.out.println("修改部门成功!");
else
System.out.println("修改部门失败!");
break;
case 4:
System.out.println("部门编号\t\t部门名称\t\t部门地址");
List<Dept> list = dept.select();
for (Dept d : list) {
System.out.println(d.getDeptno() + "\t\t" + d.getDname() + "\t\t" + d.getLoc());
}
case 0:
break;
}
}
}
五、运行结果
=====部门管理系统=====
===1.添加部门========
===2.删除部门========
===3.更新部门========
===4.查询所有部门=====
===0.退出系统========
请选择功能:4
部门编号 部门名称 部门地址
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON
60 工程部 徐州
边栏推荐
猜你喜欢

存储器分级介绍

缓存型数据库Redis的配置与优化

Day18Qt信号与槽2021-10-29

协同过滤(Collaborative Filtering)

信创环境下缓存服务Redis集群部署

Byte Jump propose un nouveau type de réseau léger et efficace, mobovit, qui surpasse GhostNet et mobilenetv3 dans la classification, la détection et d'autres tâches CV!

TypeScript的类型检查

AWS Device Shadow使用

Day11QPainter2021-09-26

Node的字符处理
随机推荐
Typescript的继承
会议聊天室--开发文档
Typescript的构造方式
Generic type checking for typescript
Collaborative filtering
With mitmdump, don't throw it away, Charles
C语言__attribute__(packed)属性(学习一下)
[pwn基础]Pwntools学习
Byte traffic business experience: realize as soon as possible, sew money bags, and sell all in goods
【艾思软件】微信小程序开发报价方案模版
2022年高处安装、维护、拆除上岗证题库及模拟考试
Day12QFile2021-09-27
Typescript的泛型
摩尔投票法
工地建设动画网页建设中js特效
网络爬虫开发工具:Screaming Frog SEO Spider
最佳网络数据抓包工具mitmproxy
Redis配置与优化
Book at the end of the article | new work by teacher Li Hang! A new upgrade of the classic machine learning work statistical learning methods
剑指 Offer 12. 矩阵中的路径