From 5634c56f9a55da224fdda445174ab2f041630a10 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=81=E4=B8=9A=E6=9E=97-17050417?= <17050417@suning-sports.com> Date: Wed, 28 Sep 2022 20:28:32 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8A=BD=E5=8F=96basedao?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../JDBCDemo2/src/com/msb/dao/BaseDao.java | 71 ++++++++++++ .../src/com/msb/dao/imp/DeptDaoImp.java | 41 ++++++- .../src/com/msb/dao/imp/EmpDaoImp.java | 101 +++++++++++++++++- .../src/com/msb/view/EmpManageSystem.java | 92 ++++++++++++++++ 4 files changed, 299 insertions(+), 6 deletions(-) create mode 100644 testJDBC/JDBCDemo2/src/com/msb/dao/BaseDao.java diff --git a/testJDBC/JDBCDemo2/src/com/msb/dao/BaseDao.java b/testJDBC/JDBCDemo2/src/com/msb/dao/BaseDao.java new file mode 100644 index 0000000..b4f42f9 --- /dev/null +++ b/testJDBC/JDBCDemo2/src/com/msb/dao/BaseDao.java @@ -0,0 +1,71 @@ +package com.msb.dao; + +import java.lang.reflect.Field; +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.List; + +public abstract class BaseDao { + public List baseQuery(Connection connection, Class clazz, String sql, Object ... args) { + List list = new ArrayList(); + PreparedStatement statement = null; + try { + statement = connection.prepareStatement(sql); + for (int i = 0; i < args.length; i++) { + statement.setObject(i + 1, args[i]); + } + ResultSet rs = statement.executeQuery(); + Field[] fields = clazz.getDeclaredFields(); + for (Field field : fields) { + field.setAccessible(true); + } + while (rs.next()) { + Object obj = clazz.getDeclaredConstructor().newInstance(); + for (Field field : fields) { + Object data = rs.getObject(field.getName()); + field.set(obj, data); + } + list.add(obj); + } + } catch (Exception e) { + e.printStackTrace(); + } finally { + try { + statement.close(); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return list; + } + + public int baseUpdate(Connection connection, String sql,Object ... args){ + PreparedStatement preparedStatement=null; + int rows=0; + + try{ + preparedStatement = connection.prepareStatement(sql); + //设置参数 + for (int i = 0; i findAll() { - return null; + Connection connection = null; + + List list =null; + + try { + // 选择数据库 + Class.forName(driver); + + // 建立连接 + connection = DriverManager.getConnection(url, user, password); + + // 执行sql + String sql = "select * from dept"; + list = baseQuery(connection, Dept.class, sql); + } + catch (Exception e) { + e.printStackTrace(); + } + finally { + try { + if (connection != null) { + connection.close(); + } + } + catch (SQLException e) { + e.printStackTrace(); + } + } + + return list; } @Override diff --git a/testJDBC/JDBCDemo2/src/com/msb/dao/imp/EmpDaoImp.java b/testJDBC/JDBCDemo2/src/com/msb/dao/imp/EmpDaoImp.java index a2f7d90..c9a09c8 100644 --- a/testJDBC/JDBCDemo2/src/com/msb/dao/imp/EmpDaoImp.java +++ b/testJDBC/JDBCDemo2/src/com/msb/dao/imp/EmpDaoImp.java @@ -1,19 +1,84 @@ package com.msb.dao.imp; import com.msb.bean.Emp; +import com.msb.dao.BaseDao; import com.msb.dao.EmpDao; +import java.sql.*; +import java.util.ArrayList; import java.util.List; -public class EmpDaoImp implements EmpDao { +public class EmpDaoImp extends BaseDao implements EmpDao { + public static String driver = "com.mysql.cj.jdbc.Driver"; + public static String url = "jdbc:mysql://127.0.0.1:3306/mydb?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai"; + public static String user = "root"; + public static String password = "Fnst1234"; + @Override public List findAll() { - return null; + Connection connection = null; + + List list =null; + + try { + // 选择数据库 + Class.forName(driver); + + // 建立连接 + connection = DriverManager.getConnection(url, user, password); + + // 执行sql + String sql = "select * from emp"; + list = baseQuery(connection, Emp.class, sql); + } + catch (Exception e) { + e.printStackTrace(); + } + finally { + try { + if (connection != null) { + connection.close(); + } + } + catch (SQLException e) { + e.printStackTrace(); + } + } + + return list; } @Override public int deleteByEmpno(int empno) { - return 0; + Connection connection = null; + + int rows = 0; + + try { + // 选择数据库 + Class.forName(driver); + + // 建立连接 + connection = DriverManager.getConnection(url, user, password); + + // 执行sql + String sql = "delete from emp where empno = ?"; + rows = baseUpdate(connection, sql, empno); + } + catch (Exception e) { + e.printStackTrace(); + } + finally { + try { + if (connection != null) { + connection.close(); + } + } + catch (SQLException e) { + e.printStackTrace(); + } + } + return rows; } @Override @@ -23,6 +88,34 @@ public class EmpDaoImp implements EmpDao { @Override public int addEmp(Emp emp) { - return 0; + Connection connection = null; + + int rows = 0; + + try { + // 选择数据库 + Class.forName(driver); + + // 建立连接 + connection = DriverManager.getConnection(url, user, password); + + // 执行sql + String sql = "insert into emp values(?,?,?,?,?,?,?,?)"; + rows = baseUpdate(connection, sql, emp.getEmpno(), emp.getEname(), emp.getJob(),emp.getMgr(),emp.getHiredate(),emp.getSal(),emp.getComm(),emp.getDeptno()); + } + catch (Exception e) { + e.printStackTrace(); + } + finally { + try { + if (connection != null) { + connection.close(); + } + } + catch (SQLException e) { + e.printStackTrace(); + } + } + return rows; } } diff --git a/testJDBC/JDBCDemo2/src/com/msb/view/EmpManageSystem.java b/testJDBC/JDBCDemo2/src/com/msb/view/EmpManageSystem.java index 9dc93f9..e0a03d6 100644 --- a/testJDBC/JDBCDemo2/src/com/msb/view/EmpManageSystem.java +++ b/testJDBC/JDBCDemo2/src/com/msb/view/EmpManageSystem.java @@ -1,7 +1,99 @@ package com.msb.view; +import com.msb.bean.Dept; +import com.msb.bean.Emp; +import com.msb.dao.DeptDao; +import com.msb.dao.EmpDao; +import com.msb.dao.imp.DeptDaoImp; +import com.msb.dao.imp.EmpDaoImp; + +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.List; +import java.util.Scanner; + public class EmpManageSystem { + private static Scanner sc =new Scanner(System.in); + private static EmpDao empDao =new EmpDaoImp(); + private static DeptDao deptDao=new DeptDaoImp(); + private static SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd"); + public static void main(String[] args) { + while (true) { + showMenu(); + System.out.println("请录入选项"); + int option =sc.nextInt(); + switch (option) { + case 1: + case1(); + break; + case 2: + case2(); + break; + case 3: + case3(); + break; + case 5: + case5(); + break; + default: + + } + } + } + + private static void showMenu(){ + System.out.println("************************************"); + System.out.println("* 1 查看所有员工信息"); + System.out.println("* 2 查看所有部门信息"); + System.out.println("* 3 根据工号删除员工信息"); + System.out.println("* 4 根据工号修改员工信息"); + System.out.println("* 5 增加员工信息"); + System.out.println("* 6 增加部门信息"); + System.out.println("* 7 退出"); + System.out.println("************************************"); + } + + private static void case1(){ + List emps = empDao.findAll(); + emps.forEach(System.out::println); + } + + private static void case2(){ + List depts = deptDao.findAll(); + depts.forEach(System.out::println); + } + + private static void case3(){ + System.out.println("请输入要删除的员工编号"); + int empno=sc.nextInt(); + empDao.deleteByEmpno(empno); + } + private static void case5(){ + System.out.println("请输入员工编号"); + int empno =sc.nextInt(); + System.out.println("请输入员工姓名"); + String ename =sc.next(); + System.out.println("请输入员工职位"); + String job =sc.next(); + System.out.println("请输入员工上级"); + int mgr =sc.nextInt(); + System.out.println("请输入员工入职日期,格式为yyyy-MM-dd"); + Date hiredate =null; + try { + hiredate = simpleDateFormat.parse(sc.next()); + } catch (ParseException e) { + e.printStackTrace(); + } + System.out.println("请输入员工工资"); + double sal =sc.nextDouble(); + System.out.println("请输入员工补助"); + double comm=sc.nextDouble(); + System.out.println("请输入员工部门号"); + int deptno =sc.nextInt(); + Emp emp=new Emp(empno, ename, job, mgr, hiredate, sal, comm,deptno); + empDao.addEmp(emp); } }