当前位置:网站首页>Dao and encapsulation of entity classes
Dao and encapsulation of entity classes
2022-06-21 18:49:00 【LvhaoIT】
Dao And entity class encapsulation
List of articles
One 、jdbc Tool class encapsulation
package JDBC.Dao;
import java.sql.*;
public class JdbcUtil {
private Connection conn = null;
private PreparedStatement ps = null;
static {
try {
// Registration drive
Class.forName("com.mysql.cj.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
// encapsulation conn objects creating
public Connection createCon() {
try {
conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/csdb?serverTimezone=UTC", "root", "lh051920");
System.out.println("conn Object created successfully !");
} catch (SQLException throwables) {
throwables.printStackTrace();
System.out.println("conn Object creation failed !");
}
return conn;
}
// encapsulation PreparedStatement object
public PreparedStatement createStatement(String sql) {
Connection conn = createCon();
try {
ps = conn.prepareStatement(sql);
} catch (SQLException throwables) {
throwables.printStackTrace();
}
return ps;
}
// Package destruction
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();
}
}
}
Two 、DeptDao Encapsulation
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;
/** * encapsulation dept The operation of the table */
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;
}
}
3、 ... and 、Dept Data table encapsulation
package JDBC.entity;
// Table 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;
}
}
Four 、 Test code
package JDBC.Test;
import JDBC.Dao.DeptDao;
import JDBC.entity.Dept;
import java.util.List;
import java.util.Scanner;
public class Department of management {
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("===== Department management system =====");
System.out.println("===1. Add Department ========");
System.out.println("===2. Delete Department ========");
System.out.println("===3. Update department ========");
System.out.println("===4. Check all departments =====");
System.out.println("===0. Exit the system ========");
System.out.print(" Please select the function :");
flag = in.nextInt();
in.nextLine();
int result = 0;
switch (flag) {
case 1:
System.out.print(" Please enter the department number :");
deptno = in.nextLine();
System.out.print(" Please enter the Department name :");
dname = in.nextLine();
System.out.print(" Please enter the address :");
loc = in.nextLine();
result = dept.add(deptno, dname, loc);
if (result == 1)
System.out.println(" Department added successfully !");
else
System.out.println(" Failed to add Department !");
break;
case 2:
System.out.print(" Please enter the department number :");
deptno = in.nextLine();
result = dept.delete(deptno);
if (result == 1)
System.out.println(" Delete Department succeeded !");
else
System.out.println(" Failed to delete Department !");
break;
case 3:
System.out.print(" Please enter the department number :");
deptno = in.nextLine();
System.out.print(" Please enter the Department name :");
dname = in.nextLine();
System.out.print(" Please enter the address :");
loc = in.nextLine();
result = dept.update(deptno, dname, loc);
if (result == 1)
System.out.println(" Department modified successfully !");
else
System.out.println(" Failed to modify Department !");
break;
case 4:
System.out.println(" Department number \t\t Department name \t\t Department Address ");
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;
}
}
}
5、 ... and 、 Running results
===== Department management system =====
===1. Add Department ========
===2. Delete Department ========
===3. Update department ========
===4. Check all departments =====
===0. Exit the system ========
Please select the function :4
Department number Department name Department Address
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON
60 engineering department xuzhou
边栏推荐
- R language bug? report errors? As for the outcome of sub variables 0 and 1, the original content of the outcome variable is changed through the process of factor and numeric?
- 存储器分级介绍
- TypeScript的类型检查
- The best network packet capturing tool mitmproxy
- 国产API管理平台横向比较,到底哪家强?
- Dao与实体类的封装
- Servlet中Listener与Filter (监视器与拦截器)
- R语言 bug?报错?关于亚变量0、1 结局outcome,outcome变量经过factor和numeric过程,改变了原始内容?
- Canvas动态网状背景js特效
- 文末送书 | 李航老师新作!机器学习经典著作《统计学习方法》全新升级
猜你喜欢
随机推荐
网络爬虫开发工具:Screaming Frog SEO Spider
力扣239. 滑动窗口最大值
Servlet规范(一)
新赛季的中超和国安,荆棘中前行
Day11QPainter2021-09-26
Under the banana whose R & D accounts for only 3%, is it sunscreen black technology or IQ tax in summer?
TypeScript的类型检查
Node模块化管理
Typescript的复合类型
9. suffix and prefix functions -suffix basename addsuffix addprefix
RK3566调试GC2053
TypeScript对象类型
TypeScript类对象的初始化
互联网通信流程
JDBC 笔记
RMB 18billion, a super master fund was born in Suzhou
Typescript接口
C3—Qt实现五子棋小游戏(一)2021.11.07
Inheritance of typescript
epoll+threadpool高并发网络IO模型的实现









