DeptDao
package com.javasm.dao; import com.javasm.bean.Dept; import com.javasm.util.JDBCUtils; import java.sql.*; import java.util.ArrayList; import java.util.List; /** * @Author:Zxb * @Version:1.0 * @Date:2022/12/12-14:37 * @Since:jdk1.8 * @Description: */ public class DeptDao { /** * 添加部门 * * @param dept * @return 返回受影响行数 */ public int addDept(Dept dept) { int i = 0; //准备sql String sql = "insert into tb_department values(?,?)"; //获取数据库连接对象 Connection connection = JDBCUtils.getConnection(); PreparedStatement statement = null; //先判断 if (JDBCUtils.isConnection(connection)) { try { //获取执行对象 statement = connection.prepareStatement(sql); statement.setInt(1, dept.getDeptId()); statement.setString(2, dept.getDeptName()); //发送语句并执行 i = statement.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); } finally { //资源回收 JDBCUtils.close(connection, statement); } } return i; } /** * 根据id删除部门 * * @param id * @return 受影响行数 */ public int deleteDeptById(int id) { int i = 0; String sql = "delete from tb_department where dept_id=?"; //获取数据库连接对象 Connection connection = JDBCUtils.getConnection(); //执行对象 PreparedStatement statement = null; //先判断 if (JDBCUtils.isConnection(connection)){ try { //获取执行对象 statement = connection.prepareStatement(sql); //解决参数 statement.setInt(1, id); //执行 i = statement.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); } finally { JDBCUtils.close(connection, statement); } } //返回受影响行数 return i; } /** * 修改 * * @param dept * @return 返回收影响行数 */ public int updateDept(Dept dept) { int i = 0; String sql = "update tb_department set dept_name=? where dept_id=?"; //获取连接对象 Connection connection = JDBCUtils.getConnection(); //执行对象 PreparedStatement statement = null; try { statement = connection.prepareStatement(sql); //解决参数 statement.setString(1, dept.getDeptName()); statement.setInt(2, dept.getDeptId()); //执行 i = statement.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); } finally { JDBCUtils.close(connection, statement); } return i; } /** * 根据编号查询部门信息 * @param id * @return */ public Dept getDeptById(int id) { //准备对象 Dept dept = null; //准备sql String sql = "select * from tb_department where dept_id=?"; //获取连接对象 Connection connection = JDBCUtils.getConnection(); //执行对象 PreparedStatement statement = null; //结果集 ResultSet resultSet = null; //先判断 if (JDBCUtils.isConnection(connection)) { try { statement = connection.prepareStatement(sql); //解决参数 statement.setInt(1, id); //执行 resultSet = statement.executeQuery(); if (resultSet.next()) { dept = new Dept(); dept.setDeptId(resultSet.getInt("dept_id")); dept.setDeptName(resultSet.getString("dept_name")); } } catch (SQLException e) { e.printStackTrace(); }finally { JDBCUtils.close(connection,statement,resultSet); } } return dept; } /** * 查询全部部门信息 * @return */ public List<Dept> findAllDept(){ //准备集合 List<Dept> deptList = new ArrayList<>(10); //准备sql String sql="select * from tb_department order by dept_id"; //获取连接对象 Connection connection = JDBCUtils.getConnection(); //执行对象 PreparedStatement statement = null; ResultSet resultSet = null; if(JDBCUtils.isConnection(connection)){ try { statement=connection.prepareStatement(sql); //执行 resultSet=statement.executeQuery(); //循环判断 while (resultSet.next()){ //创建一个部门对象 Dept dept = new Dept(); //取值存入到属性中 dept.setDeptId(resultSet.getInt("dept_id")); dept.setDeptName(resultSet.getString("dept_name")); //将对象存入到集合中 deptList.add(dept); } } catch (SQLException e) { e.printStackTrace(); }finally { JDBCUtils.close(connection,statement,resultSet); } } return deptList; } }
DeptService
package com.javasm.service; import com.javasm.bean.Dept; import com.javasm.dao.DeptDao; import javax.sound.midi.Soundbank; import java.util.List; import java.util.Scanner; /** * @Author:Zxb * @Version:1.0 * @Date:2022/12/12-15:29 * @Since:jdk1.8 * @Description: */ public class DeptService { private Scanner scanner = new Scanner(System.in); private DeptDao deptDao = new DeptDao(); /** * 添加 */ public void add(){ System.out.println("部门管理>>>添加部门"); Dept dept = new Dept(); System.out.println("请输入部门的编号"); dept.setDeptId(scanner.nextInt()); System.out.println("请输入部门的名称"); dept.setDeptName(scanner.next()); int i = deptDao.addDept(dept); if(i>0){ System.out.println("添加成功"); }else{ System.out.println("添加失败"); } } /** * 删除 */ public void delete(){ System.out.println("部门管理>>>删除部门"); System.out.println("请输入要删除的部门编号:"); int inputId = scanner.nextInt(); int i=deptDao.deleteDeptById(inputId); if(i>0){ System.out.println("删除成功"); }else{ System.out.println("删除失败"); } } /** * 修改 */ public void update(){ System.out.println("部门管理>>>修改部门"); System.out.println("请输入修改部门的编号"); int id = scanner.nextInt(); //根据编号查找部门信息 Dept dept = deptDao.getDeptById(id); if (dept == null) { System.out.println("[修改失败,该部门不存在!]"); return; } System.out.println("原部门名称:<" + dept.getDeptName() + ">请输入修改之后的部门名称:"); dept.setDeptName(scanner.next()); int i = deptDao.updateDept(dept); if(i>0){ System.out.println("修改成功"); }else{ System.out.println("修改失败"); } } /** * 查询 */ public void find(){ System.out.println("部门管理>>>查看部门"); List<Dept> deptList = deptDao.findAllDept(); if(deptList.isEmpty()){ System.out.println("还没有任何部门信息"); return; } for (Dept dept : deptList) { System.out.println(dept); } } }
标签:Service,System,dept,connection,statement,DeptDao,println,out From: https://www.cnblogs.com/19981206-zxb/p/16977276.html