目录
一:项目功能
1.查询所有员工
2.查询指定编号的员工
3.添加员工信息
4.修改员工信息
5.删除员工信息
6.退出功能
二:技能的使用
三:项目框架
3.1:在MySQL中创建项目所需数据库及其数据
3.2:创建JDBC数据库连接(将数据库中数据与java代码连接起来)(DBUtil类)
3.3:编写员工类(Employee类)
3.4:编写对员工的操作方法,使用接口实现(EmployeeDao接口)
3.5:编写EmployeeDaoImpl类,用于对接口的实现(EmployeeDaoImpl类)
3.6:对系统的管理页面的编写(TestA类)
四:测试结果
4.1:查询所有员工
4.2:查询指定编号的员工
4.3:添加员工信息
4.4:修改员工信息
4.5:删除员工信息
4.6:退出功能
该项目应该具有以下功能:
*****欢迎进入员工管理系统******
1.查询所有员工
2.查询指定编号员工
3.添加员工信息
4.修改员工信息
5.删除员工信息
6.退出
***************************
请选择菜单:
1.使用JDBC访问数据库
2.分层开发:
前台:调用后台,并输出结果
后台:使用JDBC数据库连接技术访问数据并返回结果
3.提取工具类DBUtil,复用代码
4.使用Properties类读取属性文件(作用:提高代码的复用性,便于使用不同数据库时必要信息的修改)
private static String driver;
private static String url;
private static String uname;
private static String pwd;
static
{
// 1.获得Properties类对象
Properties properties = new Properties();
// 2.解析jdbc.properties属性文件(读取jdbc.properties配置文件到输入流中)
InputStream inputStream = DBUtil.class.getResourceAsStream("/jdbc.properties");
try {
// 把读取的属性文件中的数据记载到Properties对象中
properties.load(inputStream);
// 获取properties文件中的属性值
driver = properties.getProperty("driver");
url = properties.getProperty("url");
uname = properties.getProperty("username");
pwd = properties.getProperty("password");
} catch (IOException e) {
throw new RuntimeException(e);
}
}
树状结构:
模块名:---project1
包名:---com.jiayifeng
①包名:---dao
包名:---impl
类名:---EmployeeDaoimpl
接口名:---EmployeeDao
②包名:---pojo(简单的 Java 对象(Plain Ordinary Java Object))
类名:---Employee
③包名:-util
类名:---DBUtil
④包名:-test
类名:---TestA
①创建数据库,数据库名:xiaojia
②创建数据库相应的表,表名:emp(全称:Employee)
JDBC介绍与使用:http://t.csdnimg.cn/z0Zd9
package com.jiayifeng.util;
import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;
/**
* author 爱编程的小贾
* create 2023-10-25 19:22
*/
public class DBUtil {
// 获得conn对象
public static Connection getConn(){
Connection conn = null;
try {
Class.forName("com.mysql.cj.jdbc.Driver");
conn = DriverManager.
getConnection("jdbc:mysql://127.0.0.1:3306/xiaojia",
"root",
"ASUS010519");
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
} catch (SQLException e) {
throw new RuntimeException(e);
}
return conn;
}
// 关闭操作封装
public static void closeAll(Connection conn, PreparedStatement pstmt, ResultSet rs){
if(conn != null){
try {
conn.close();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
if(pstmt != null){
try {
pstmt.close();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
if(rs != null){
try {
rs.close();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}
}
package com.jiayifeng.pojo;
import java.util.Date;
/**
* author 爱编程的小贾
* create 2023-10-25 19:16
*/
public class Employee {
private Integer empno;
private String ename;
private String job;
private int mgr;
private Date hiredate;
private double sal;
private double comm;
private int deptno;
public Employee(int empno2, String ename, String job, int mgr, long hiredate, double sal, double comm, int deptno) {
}
public Employee(Integer empno, String ename, String job,
int mgr, Date hiredate, double sal, double comm, int deptno) {
this.empno = empno;
this.ename = ename;
this.job = job;
this.mgr = mgr;
this.hiredate = hiredate;
this.sal = sal;
this.comm = comm;
this.deptno = deptno;
}
public Employee(String ename1, String job1, int empno3) {
this.ename = ename1;
this.job = job1;
this.empno = empno3;
}
public Integer getEmpno() {
return empno;
}
public void setEmpno(Integer empno) {
this.empno = empno;
}
public String getEName() {
return ename;
}
public void setEName(String ename) {
this.ename = ename;
}
public String getJob() {
return job;
}
public void setJob(String job) {
this.job = job;
}
public int getMgr() {
return mgr;
}
public void setMgr(int mgr) {
this.mgr = mgr;
}
public Date getHiredate() {
return hiredate;
}
public void setHiredate(Date hiredate) {
this.hiredate = hiredate;
}
public double getSal() {
return sal;
}
public void setSal(double sal) {
this.sal = sal;
}
public double getComm() {
return comm;
}
public void setComm(double comm) {
this.comm = comm;
}
public int getDeptno() {
return deptno;
}
public void setDeptno(int deptno) {
this.deptno = deptno;
}
@Override
public String toString() {
return "Employee{" +
"empno=" + empno +
", name='" + ename + '\'' +
", job='" + job + '\'' +
", mgr=" + mgr +
", hiredate=" + hiredate +
", sal=" + sal +
", comm=" + comm +
", deptno=" + deptno +
'}';
}
}
实现对员工的增删改查操作
package com.jiayifeng.dao;
import com.jiayifeng.pojo.Employee;
import java.util.List;
/**
* author 爱编程的小贾
* create 2023-10-25 19:33
*
* 一:用户的增删改查操作
*/
public interface EmployeeDao {
// 1.查询所有用户操作 A.返回值(不是对象(一条数据记录)而是集合(多条记录)) B.参数
List selectAll();
// 2.查询单个用户操作 A.返回值(对象) B.参数(empno)
Employee selectOne(int empno);
// 3.添加、修改、删除的返回值都是int A.返回值 B.参数
// 注意:添加的时候我们需要传递8个参数,我们可以依次的传递,但这样的操作太过麻烦,可以把参数保存到对象中
// 直接传递一个对象即可
int insert(Employee employee);
// 4.修改员工 A.返回值 B.参数
// 我们当前修改为了简单,所以只传入了3个参数,但是实际情况超过三个参数,所以这个参数还是传递对象
int updata(Employee employee);
// 5.删除员工 A.返回值 B.参数
int delete(int empno);
}
package com.jiayifeng.dao.impl;
import com.jiayifeng.dao.EmployeeDao;
import com.jiayifeng.pojo.Employee;
import com.jiayifeng.util.DBUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/**
* author 爱编程的小贾
* create 2023-10-25 19:39
*
* 一:接口的实现
*/
public class EmployeeDaoImpl implements EmployeeDao {
// 1.查询所有用户操作
@Override
public List selectAll() {
Connection conn = DBUtil.getConn();
List list = new ArrayList<>();
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
pstmt = conn.prepareStatement("select * from emp");
rs = pstmt.executeQuery();
// 从rs中取出数据
while (rs.next()) {
int empno = rs.getInt("empno");
String ename = rs.getString("ename");
String job = rs.getString("job");
int mgr = rs.getInt("mgr");
Date hiredate = rs.getDate("hiredate");
Double sal = rs.getDouble("sal");
Double comm = rs.getDouble("comm");
int deptno = rs.getInt("deptno");
// 将查询出来的数据保存到对象中
Employee emp = new Employee(empno, ename, job, mgr, hiredate,
sal, comm, deptno);
// 把当前对象保存到集合中
list.add(emp);
}
} catch (SQLException e) {
throw new RuntimeException(e);
} finally {
DBUtil.closeAll(conn, pstmt, rs);
}
return list;
}
// 2.查询单个用户操作
@Override
public Employee selectOne(int empno) {
Connection conn = DBUtil.getConn();
PreparedStatement pstmt = null;
ResultSet rs = null;
Employee emp = null;
try {
pstmt = conn.prepareStatement("select * from emp where empno = ?");
pstmt.setInt(1, empno);
rs = pstmt.executeQuery();
while (rs.next()) {
int empoo = rs.getInt("empno");
String ename = rs.getString("ename");
String job = rs.getString("job");
int mgr = rs.getInt("mgr");
Date hiredate = rs.getDate("hiredate");
Double sal = rs.getDouble("sal");
Double comm = rs.getDouble("comm");
int deptno = rs.getInt("deptno");
emp = new Employee(empoo, ename, job, mgr, hiredate,
sal, comm, deptno);
}
} catch (SQLException e) {
throw new RuntimeException(e);
} finally {
DBUtil.closeAll(conn, pstmt, rs);
}
return emp;
}
// 3.添加操作
@Override
public int insert(Employee employee) {
Connection conn = DBUtil.getConn();
PreparedStatement pstmt = null;
int i = 0;
try {
pstmt = conn.prepareStatement("insert into emp values(?,?,?,?,?,?,?,?)");
pstmt.setObject(1, employee.getEmpno());
pstmt.setObject(2, employee.getEName());
pstmt.setObject(3, employee.getJob());
pstmt.setObject(4, employee.getMgr());
pstmt.setObject(5, employee.getHiredate());
pstmt.setObject(6, employee.getSal());
pstmt.setObject(7, employee.getComm());
pstmt.setObject(8, employee.getDeptno());
i = pstmt.executeUpdate();
} catch (SQLException e) {
throw new RuntimeException(e);
} finally {
DBUtil.closeAll(conn, pstmt, null);
}
return i;
}
// 4.修改操作
@Override
public int updata(Employee employee) {
Connection conn = DBUtil.getConn();
PreparedStatement pstmt = null;
int i = 0;
try {
pstmt = conn.prepareStatement("update emp set ename=?,job=? where empno=?");
pstmt.setObject(1, employee.getEName());
pstmt.setObject(2, employee.getJob());
pstmt.setObject(3, employee.getEmpno());
i = pstmt.executeUpdate();
} catch (SQLException e) {
throw new RuntimeException(e);
} finally {
DBUtil.closeAll(conn, pstmt, null);
}
return i;
}
// 5.删除操作
@Override
public int delete(int empno) {
Connection conn = DBUtil.getConn();
PreparedStatement pstmt = null;
int i = 0;
pstmt = null;
try {
pstmt = conn.prepareStatement("delete from emp where empno=?");
pstmt.setObject(1, empno);
i = pstmt.executeUpdate();
} catch (SQLException e) {
throw new RuntimeException(e);
} finally {
DBUtil.closeAll(conn, pstmt, null);
}
return i;
}
}
package com.jiayifeng.test;
import com.jiayifeng.dao.EmployeeDao;
import com.jiayifeng.dao.impl.EmployeeDaoImpl;
import com.jiayifeng.pojo.Employee;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Scanner;
/**
* author 爱编程的小贾
* create 2023-10-29 17:12
*/
public class TestA {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
EmployeeDao employeeDao = new EmployeeDaoImpl();
while (true) {
System.out.println("*****欢迎进入员工管理系统******");
System.out.println("\t1.查询所有员工");
System.out.println("\t2.查询指定编号员工");
System.out.println("\t3.添加员工信息");
System.out.println("\t4.修改员工信息");
System.out.println("\t5.删除员工信息");
System.out.println("\t6.退出");
System.out.println("***************************");
System.out.println("请选择菜单:");
int num = scanner.nextInt();
switch (num){
case 1:
List list = employeeDao.selectAll();
for(Employee emp : list){
System.out.println(emp);
}
break;
case 2:
System.out.println("请输入要查询的员工编号:");
int empno = scanner.nextInt();
Employee employee = employeeDao.selectOne(empno);
if(employee != null){
System.out.println(employee);
}else{
System.out.println("查无此人!");
}
break;
case 3:
System.out.println("请输入要添加的员工信息:");
System.out.println("请输入员工编号:");
int empno2 = scanner.nextInt();
System.out.println("请输入员工姓名:");
String ename = scanner.next();
System.out.println("请输入员工工作:");
String job = scanner.next();
System.out.println("请输入员工上级:");
int mgr = scanner.nextInt();
System.out.println("请输入员工入职日期:");
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy.MM.dd");
String date = scanner.next();
Date hiredate = null;
try {
hiredate = sdf1.parse(date);
} catch (ParseException e) {
throw new RuntimeException(e);
}
System.out.println("请输入员工薪资:");
double sal = scanner.nextDouble();
System.out.println("请输入员工加班费:");
double comm = scanner.nextDouble();
System.out.println("请输入员工岗位编号:");
int deptno = scanner.nextInt();
Employee emp = new Employee(empno2,ename,job,mgr,hiredate,sal,comm,deptno);
int insert = employeeDao.insert(emp);
System.out.println();
if(insert > 0){
System.out.println("添加成功!");
}else{
System.out.println("添加失败!");
}
break;
case 4:
System.out.println("请输入要修改的员工信息");
System.out.println("请输入员工姓名:");
String ename1 = scanner.next();
System.out.println("请输入员工工作:");
String job1 = scanner.next();
System.out.println("请输入员工编号:");
int empno3 = scanner.nextInt();
Employee employee2 = new Employee(ename1,job1,empno3);
int emp1 = employeeDao.updata(employee2);
if(emp1 >= 1){
System.out.println("修改成功!");
}else{
System.out.println("修改失败!");
}
break;
case 5:
System.out.println("请输入要删除的员工信息");
int empnn = scanner.nextInt();
int employee1 = employeeDao.delete(empnn);
System.out.println("删除成功!");
break;
case 6:
System.out.println("退出成功!");
System.out.println("感谢使用该系统!");
System.exit(0);
break;
default:
System.out.println("请输入正确的编号!!!!");
}
}
}
}