今天学的东西很多,数据多层次的运用连接java编译器:
数据表示层:
package com.lifeng.test;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Scanner;
import com.lifeng.dao.Dao;
import com.lifeng.dao.EmpDao;
import com.lifeng.dao.EmpDao02;
import com.lifeng.dao.EmpDao03;
import com.lifeng.dao.EmpDao04;
import com.lifeng.entity.Employee;
import com.lifeng.entity.User;
public class TestEmpDao {
public static void main(String[] args) {
//ShowAllEmp();
//SelectSingleEmp();
UserLogin();
//AddEmp();}
static EmpDao03 empDao03 = new EmpDao03();
private static void AddEmp() {
try {
Employee emp = new Employee();
Scanner input = new Scanner(System.in);
System.out.print("请输入员工编号:");
int empno=input.nextInt();
System.out.print("请输入员工姓名:");
String ename=input.next();
System.out.print("请输入员工 职位:");
String job=input.next();
System.out.print("请输入上司编号:");
int mgr=input.nextInt();
System.out.print("请输入入职日期(yyyy-MM-dd):");
String date=input.next();
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
Date hiredate = sdf.parse(date);
System.out.print("请输入基本工资:");
double sal=input.nextDouble();
System.out.print("请输入奖金:");
double comm=input.nextDouble();
System.out.print("请输入部门编号(10,20,30):");
int deptno=input.nextInt();
emp.setEmpno(empno);
emp.setEname(ename);
emp.setJob(job);
emp.setMgr(mgr);
emp.setHiredate(hiredate);
emp.setSal(sal);
emp.setComm(comm);
emp.setDeptno(deptno);
int count = empDao03.addEmp(emp);
if(count > 0) {
System.out.println("员工添加成功!");
System.out.println("如需返回系统界面请按0");
int sc = input.nextInt();
if(sc == 0) {
UserWelcome();
}else {
System.out.println("系统已为你返回界面");
UserWelcome();}
}else {
System.out.println("员工添加错失败!");}
} catch (Exception e) {
e.printStackTrace();}}
static Dao dao = new Dao();
private static void UserLogin() {
Scanner input = new Scanner(System.in);
System.out.println("请输入用户名:");
String username = input.next();
System.out.println("请输入用户密码:");
String pwd = input.next();
User user = dao.FindUserByNamePwd(username, pwd);
if(user != null) {
System.out.println("登陆成功");
UserWelcome();}else {
System.out.println("登陆失败");}}
static EmpDao04 empDao04 = new EmpDao04();
public static void UserWelcome() {
System.out.println("=========员工管理系统项目=========");
System.out.println("请选择操作:");
System.out.println("1.查询所有员工");
System.out.println("2.查询单个员工");
System.out.println("3.添加员工");
System.out.println("4.删除员工");
System.out.println("5.修改员工");
Scanner sc = new Scanner(System.in);
int input = sc.nextInt();
switch(input) {
case 1:
ShowAllEmp();//查询所有员工
break;
case 2:
SelectSingleEmp();//查询单个员工
break;
case 3:
AddEmp();//添加员工
break;
case 4:
empDao04.DeleteEmp();//删除员工
break;
case 5:
empDao04.Modify();//修改员工
break;}}
static EmpDao02 empDao02 = new EmpDao02();
private static void SelectSingleEmp() {
Scanner input = new Scanner(System.in);
System.out.println("请输入你需要查询的员工编号:");
int empno = input.nextInt();
Employee emp = empDao02.SelectSingleEmp(empno);
if(emp != null) {
System.out.println("员工姓名\t职位\t经理\t入职日期\t工资\t奖金\t部门");
System.out.print(emp.getEname()+"\t");
System.out.print(emp.getJob()+"\t");
System.out.print(emp.getMgr()+"\t");
System.out.print(emp.getHiredate()+"\t");
System.out.print(emp.getSal()+"\t");
System.out.print(emp.getComm()+"\t");
System.out.println(emp.getDeptno()+"\t");
System.out.println("如需返回系统界面请按0");
int sc = input.nextInt();
if(sc == 0) {
UserWelcome();
}else {
System.out.println("系统已为你返回界面");
UserWelcome();}
}else {
System.out.println("该员工编号不存在");
System.out.println("系统已为你返回界面");
UserWelcome();}}
static EmpDao empDao = new EmpDao();
private static void ShowAllEmp() {
Scanner sc = new Scanner(System.in);
List
System.out.println("员工编号\t员工姓名\t职位\t经理\t入职日期\t工资\t奖金\t部门");
for(Employee emp:list) {
System.out.print(emp.getEmpno()+"\t");
System.out.print(emp.getEname()+"\t");
System.out.print(emp.getJob()+"\t");
System.out.print(emp.getMgr()+"\t");
System.out.print(emp.getHiredate()+"\t");
System.out.print(emp.getSal()+"\t");
System.out.print(emp.getComm()+"\t");
System.out.println(emp.getDeptno()+"\t");}
System.out.println("如需返回系统界面请按0");
int input = sc.nextInt();
if(input == 0) {
UserWelcome();
}else {
System.out.println("系统已为你返回界面");
UserWelcome();}}}
访问层:
package com.lifeng.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import com.lifeng.entity.User;
import com.lifeng.jdbc.JdbcUtil;
public class Dao extends JdbcUtil{
public User FindUserByNamePwd(String username,String pwd) {
User user = null;
try {
Connection conn = getConnection();
String sql = "select * from user where username = ? and pwd = ?";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, username);
pstmt.setString(2, pwd);
ResultSet rs = pstmt.executeQuery();
if(rs.next()) {
user = new User();
user.setUsername(rs.getString("username"));
user.setPwd(rs.getString("pwd"));}
closeAll(conn,pstmt,rs);
}catch(Exception e){
e.printStackTrace();}
return user;}}
package com.lifeng.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import com.lifeng.entity.Employee;
import com.lifeng.jdbc.JdbcUtil;
import com.mysql.cj.xdevapi.Result;
public class EmpDao extends JdbcUtil{
public List
List
try {
Connection conn = getConnection();
String sql = "select * from emp";
PreparedStatement pstmt = conn.prepareStatement(sql);
ResultSet rs = pstmt.executeQuery();
while(rs.next()) {
Employee emp = new Employee();
emp.setEmpno(rs.getInt("empno"));
emp.setEname(rs.getString("ename"));
emp.setJob(rs.getString("job"));
emp.setMgr(rs.getInt("mgr"));
emp.setHiredate(rs.getDate("hiredate"));
emp.setSal(rs.getDouble("sal"));
emp.setComm(rs.getDouble("comm"));
emp.setDeptno(rs.getInt("deptno"));
list.add(emp);}
closeAll(conn,pstmt,rs);
} catch (Exception e) {}
return list;}}
package com.lifeng.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import com.lifeng.entity.Employee;
import com.lifeng.jdbc.JdbcUtil;
public class EmpDao02 extends JdbcUtil {
public Employee SelectSingleEmp(int empno) {
Employee emp=null;//里面跟外面有区别的
try {
Connection conn = getConnection();
String sql = "select * from emp where empno = ?";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, empno);
ResultSet rs = pstmt.executeQuery();
if(rs.next()) {
emp = new Employee();
emp.setEmpno(rs.getInt("empno"));
emp.setEname(rs.getString("ename"));
emp.setJob(rs.getString("job"));
emp.setMgr(rs.getInt("mgr"));
emp.setHiredate(rs.getDate("hiredate"));
emp.setSal(rs.getDouble("sal"));
emp.setComm(rs.getDouble("comm"));
emp.setDeptno(rs.getInt("deptno"));}
closeAll(conn,pstmt,rs);
} catch (Exception e) {
e.printStackTrace();}
return emp;}}
package com.lifeng.dao;
import com.lifeng.entity.Employee;
import com.lifeng.jdbc.JdbcUtil;
public class EmpDao03 extends JdbcUtil{
public int addEmp(Employee emp) {
String sql = "insert into emp values(?,?,?,?,?,?,?,?)";
Object[] params = { emp.getEmpno(),emp.getEname(), emp.getJob(), emp.getMgr(),
emp.getHiredate(),emp.getSal(),emp.getComm(),emp.getDeptno()};
return executeUpdate(sql,params);}}
package com.lifeng.dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
import com.lifeng.entity.Employee;
import com.lifeng.jdbc.JdbcUtil;
import com.lifeng.test.TestEmpDao;
public class EmpDao04 extends JdbcUtil{
public static void DeleteEmp() {
try {
Connection conn = getConnection();
//连接成后即可操作数据库,增删改查
Scanner input=new Scanner(System.in);
System.out.print("请输入要删除的员工编号:");
int empno=input.nextInt();
String sql="delete from emp where empno = ?";
Object[] params = {empno};
int count = executeUpdate(sql,params);
if(count > 0) {
System.out.println("员工删除成功!");
System.out.println("如需返回系统界面请按0");
int sc = input.nextInt();
if(sc == 0) {
TestEmpDao.UserWelcome();
}else {
System.out.println("系统已为你返回界面");
TestEmpDao.UserWelcome();}
}else {
System.out.println("员工删除失败!");
System.out.println("系统已为你返回界面");
TestEmpDao.UserWelcome();}
} catch (Exception e) {
e.printStackTrace();}}
public static void Modify() {
try {
Employee emp = new Employee();
Connection conn = getConnection();
Scanner input = new Scanner(System.in);
delete();
System.out.print("请输入新员工编号:");
int empno=input.nextInt();
System.out.print("请输入员工姓名:");
String ename=input.next();
System.out.print("请输入员工 职位:");
String job=input.next();
System.out.print("请输入上司编号:");
int mgr=input.nextInt();
System.out.print("请输入入职日期(yyyy-MM-dd):");
String date=input.next();
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
Date hiredate = sdf.parse(date);
System.out.print("请输入基本工资:");
double sal=input.nextDouble();
System.out.print("请输入奖金:");
double comm=input.nextDouble();
System.out.print("请输入部门编号(10,20,30):");
int deptno=input.nextInt();
emp.setEmpno(empno);
emp.setEname(ename);
emp.setJob(job);
emp.setMgr(mgr);
emp.setHiredate(hiredate);
emp.setSal(sal);
emp.setComm(comm);
emp.setDeptno(deptno);
int count = addEmp(emp);
if(count > 0) {
System.out.println("修改成功!");
System.out.println("如需返回系统界面请按0");
int sc = input.nextInt();
if(sc == 0) {
TestEmpDao.UserWelcome();
}else {
System.out.println("系统已为你返回界面");
TestEmpDao.UserWelcome();}
}else {
System.out.println("修改失败!");
System.out.println("系统已为你返回界面");
TestEmpDao.UserWelcome();}
//关闭连接
conn.close();
} catch (Exception e) {
e.printStackTrace();}}
public static int addEmp(Employee emp) {
String sql = "insert into emp values(?,?,?,?,?,?,?,?)";
Object[] params = { emp.getEmpno(),emp.getEname(), emp.getJob(), emp.getMgr(),
emp.getHiredate(),emp.getSal(),emp.getComm(),emp.getDeptno()};
return executeUpdate(sql,params);}
public static void delete() {
try {
Scanner input = new Scanner(System.in);
Connection conn = getConnection();
String ename=input.nextLine();
String sql="delete from emp where ename = ?";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, ename);
pstmt.executeUpdate();
} catch (Exception e) {
e.printStackTrace();}}}
数据用户:
package com.lifeng.entity;
import java.util.Date;
public class Employee {
private int empno;
private String ename;
private String job;
private int mgr;
private Date hiredate;
private double sal;
private double comm;
private int deptno;
public int getEmpno() {
return empno;}
public void setEmpno(int 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;}}
数据用户:
package com.lifeng.entity;
import com.lifeng.jdbc.JdbcUtil;
public class User {
private String username;
private String pwd;
public String getUsername() {
return username;}
public void setUsername(String username) {
this.username = username;}
public String getPwd() {
return pwd;}
public void setPwd(String pwd) {
this.pwd = pwd;}}
工具类:
package com.lifeng.jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class JdbcUtil {
private static String driver =
"com.mysql.cj.jdbc.Driver";// 数据库驱动字符串
//连接数据库的字符串
private static String url = "jdbc:mysql://localhost:3306/mydb?useSSL=false&serverTimezone=UTC";
// jdbc协议+数据库协议+主机地址+端口+连接的数据库 //用户名
private static String user = "root"; //密码
private static String password = "root";
//建立连接
public static Connection getConnection() {
Connection conn = null;// 数据连接对象
if(conn==null){
// 获取连接并捕获异常
try {
Class.forName(driver);
conn = DriverManager.getConnection(url, user, password);
} catch (Exception e) {
e.printStackTrace();// 异常处理}}
return conn;// 返回连接对象}
/**
* 关闭数据库连接。
* @param conn 数据库连接
* @param stmt Statement对象
* @param rs 结果集
*/
public static void closeAll(Connection conn, Statement stmt,
ResultSet rs) {
// 若结果集对象不为空,则关闭
if (rs != null) {
try {
rs.close();
} catch (Exception e) {
e.printStackTrace();}}
// 若Statement对象不为空,则关闭
if (stmt != null) {
try {
stmt.close();
} catch (Exception e) {
e.printStackTrace();}}
// 若数据库连接对象不为空,则关闭
if (conn != null) {
try {
conn.close();
} catch (Exception e) {
e.printStackTrace();}}}
public static int executeUpdate(String sql,Object[] param){
Connection conn = null;// 数据连接对象
PreparedStatement pstmt = null;
int num = 0;
conn = getConnection();
try {
pstmt = conn.prepareStatement(sql);
if (param != null) {
for (int i = 0; i < param.length; i++) {
pstmt.setObject(i + 1, param[i]); // 为预编译sql设置参数}}
num = pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally{
closeAll(conn, pstmt, null);}
return num;}}
最近学习感悟颇深,学的越深入越觉得有意思,激励我不断往前,我也越来越相信越努力越幸运;
上了大学,找准自己的方向之后不断专注往前的路上,我越来越觉得专注做一件事有多么重要,会让你后面学习越来越游刃有余吧,目前对于CSDN大神来说,我还是不算什么的,但是我还在努力中,希望自己有一天也能达到很高的境界,加油!