用Java做一个简易的员工管理系统
本文章适合学完javase,数组,面向对象,mysql数据库,JDBC
核心功能:JDBC连接数据库及项目结构实现员工管理系统
MySQL :: Download MySQL Community Server mysql下载地址
MySQL :: Download Connector/J JDBC下载地址
打开项目结构
找到JDBC jar包并导入
数据库创建如图所示
public class Student {
private String sno;
private String sname;
private Integer sage;
private String ssex;
private String dept;
private int is_del;
public String getSno() {
return sno;
}
public void setSno(String sno) {
this.sno = sno;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public Integer getSage() {
return sage;
}
public void setSage(Integer sage) {
this.sage = sage;
}
public String getSsex() {
return ssex;
}
public void setSsex(String ssex) {
this.ssex = ssex;
}
public String getDept() {
return dept;
}
public void setDept(String dept) {
this.dept = dept;
}
public int getIs_del() {
return is_del;
}
public void setIs_del(int is_del) {
this.is_del = is_del;
}
@Override
public String toString() {
return "Student{" +
"sno='" + sno + '\'' +
", sname='" + sname + '\'' +
", sage=" + sage +
", ssex='" + ssex + '\'' +
", dept='" + dept + '\'' +
", is_del=" + is_del +
'}';
}
}
package pojo;
public class Sc{
private String Sno;
private String Cno;
private int Grade;
public String getSno() {
return Sno;
}
public void setSno(String sno) {
Sno = sno;
}
public String getCno() {
return Cno;
}
public void setCno(String cno) {
Cno = cno;
}
public int getGrade() {
return Grade;
}
public void setGrade(int grade) {
Grade = grade;
}
@Override
public String toString() {
return "Sc{" +
"Sno='" + Sno + '\'' +
", Cno='" + Cno + '\'' +
", Grade=" + Grade +
'}';
}
}
代码如下:
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
public class JdbcUtil {
private static final String URL="jdbc:mysql://localhost:3306/s_t";
private static final String USER="root";
private static final String PWD="333";
static {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
private static Connection getConn(){
Connection conn=null;
try {
conn= DriverManager.getConnection(URL,USER,PWD);
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
public static int test(String sql,Object...objs){
Connection conn=getConn();
PreparedStatement pstmt=null;
int resurt=0;
try {
pstmt=conn.prepareStatement(sql);
for(int i=0;i List executeQuery(String test,RowMap rowmap,Object...objs) {
List list = new ArrayList<>();
Connection conn = getConn();
PreparedStatement pstmt = null;
ResultSet resultSet = null;
try {
pstmt = conn.prepareStatement(test);
if (objs!= null) {
for(int i=0;i T queryOne(String test,RowMap rowmap,Object...objs) {
T t=null;
Connection conn = getConn();
PreparedStatement pstmt = null;
ResultSet resultSet = null;
try {
pstmt = conn.prepareStatement(test);
if (objs!= null) {
for(int i=0;i
RowMap层
import pojo.Student;
import java.sql.ResultSet;
public interface RowMap {
T rowMapping(ResultSet rs);
}
StudentDao接口
import pojo.Student;
import java.util.List;
public interface StudentDao {
List getAll();
Student getOne(String sno);
int insert(Student student);
int updateOne(Student result);
int del(String sno);
int delTest(String sno);
}
StudentDapImpl实现StudentDao
import pojo.Sc;
import pojo.Student;
import util.JdbcUtil;
import java.sql.SQLException;
import java.util.List;
public class StudentDapImpl implements StudentDao{
@Override
public List getAll() {
return JdbcUtil.executeQuery("select * from student where is_del=1", rs-> {
Student s=new Student();
try {
s.setSno(rs.getString("Sno"));
s.setSname(rs.getString("Sname"));
s.setSsex(rs.getString("Ssex"));
s.setSage(rs.getInt("Sage"));
s.setDept(rs.getString("Dept"));
s.setIs_del(rs.getInt("is_del"));
} catch (SQLException e) {
e.printStackTrace();
}
return s;
}, null);
}
@Override
public Student getOne(String sno) {
return JdbcUtil.queryOne("select * from student where sno=? and is_del=1", rs-> {
Student s=new Student();
try {
s.setSno(rs.getString("Sno"));
s.setSname(rs.getString("Sname"));
s.setSsex(rs.getString("Ssex"));
s.setSage(rs.getInt("Sage"));
s.setDept(rs.getString("Dept"));
} catch (SQLException e) {
e.printStackTrace();
}
return s;
},sno);
}
@Override
public int insert(Student student) {
return JdbcUtil.test("insert into student(sno,sname,sage,ssex,dept) values(?,?,?,?,?)",student.getSno(),student.getSname(),student.getSage(),student.getSsex(),student.getDept());
}
@Override
public int updateOne(Student result) {
return JdbcUtil.test("update student set Sname=?,Ssex=?,Sage=?,Dept=? where sno=? and is_del=1",result.getSname(),result.getSsex(),result.getSage(),result.getDept(),result.getSno());
}
@Override
public int del(String sno) {
return JdbcUtil.test("delete from student where sno=? and is_del=1",sno);
}
@Override
public int delTest(String sno) {
return JdbcUtil.test("update student set is_del=0 where sno=?",sno);
}
}
StudentService接口
import pojo.Student;
import java.util.List;
public interface StudentService {
void getAll();
void add();
public void update();
public void del();
public void del1();
}
StudentServiecMpl实现StudentService
import dao.StudentDao;
import dao.StudentDapImpl;
import pojo.Student;
import view.StuView;
import java.util.List;
import java.util.Scanner;
public class StudentServiecMpl implements StudentService{
private static StudentDao dao=new StudentDapImpl();
private static Scanner input = new Scanner(System.in);
public void getAll() {
// 进行数据库中数据的查询,数据库操作的逻辑放到view层
// 项目分层:dao:数据传输层,jdbc逻辑的书写
List all = dao.getAll();
System.out.println("学生信息是:");
for (Student s:all
) {
System.out.println(s);
}
StuView.showMenu();
}
public void add() {
// 添加时 学号不能重复,输入了学号后,进行判断,学号是否存在
// 如果存在,重新输入,如果不存在,可以添加
// 可以根据学号查找学生信息,根据null进行判断
System.out.println("输入要添加的学生的学号(学号需要输入七位)");
String sno = input.next();
if (sno.length()==7){
Student one = dao.getOne(sno);
if(one==null ){
// 可以将其他的数据进行继续输入
System.out.println("输入姓名");
String sname = input.next();
System.out.println("年龄");
int sage = input.nextInt();
System.out.println("性别(男,女)");
String ssex = input.next();
System.out.println("所在系");
String dept = input.next();
// 添加到数据库 dao
Student stu = new Student();
stu.setSname(sname);
stu.setSage(sage);
stu.setSsex(ssex);
stu.setDept(dept);
stu.setSno(sno);
int i = dao.insert(stu);
System.out.println(i);
if (i>0){
System.out.println("添加成功");
}else {
System.out.println("添加失败");
}
StuView.showMenu();
}else{
System.out.println("学号已存在,重新输入");
add();
}
}else{
System.out.println("学号位数有误,重新输入");
add();
}
}
public void del1(){
System.out.println("输入要删除的学号信息");
String sno = input.next();
Student one = dao.getOne(sno);
if (one!=null){
int s = dao.delTest(sno);
if (s>0){
System.out.println("删除成功");
}else{
System.out.println("删除失败");
}
StuView.showMenu();
}else{
System.out.println("学号不存在,不能删除");
del1();
}
}
public void del() {//物理删除
// 删除,判断学生信息是否存在,如果不存在,不能删除
// 否则,可以删除
// 现在网络上,数据有价值,大量的数据放到一起,进行数据的分析
// 项目书写,数据库中的数据不会轻易的删除,
// 数据不会直接再数据库中删除,
// 数据还是会存在于别人的数据库中,添加一个字段,进行数据有效性的标识--------》逻辑删除
// 标识性字段上的值进行修改=========》修改列上的值
// 直接把数据库中数据删除==========》物理删除
System.out.println("输入要删除的学号信息");
String sno = input.next();
Student one = dao.getOne(sno);
if (one!=null){
int s = dao.del(sno);
if (s>0){
System.out.println("删除成功");
}else{
System.out.println("删除失败");
}
StuView.showMenu();
}else {
System.out.println("学号不存在,不能删除");
del();
}
}
public void update() {
// 修改:当前输入的学号是否存在
// 学号存在,可以修改,学号不存在,不能修改
// 是否要修改某个列上的数据信息
// 要修改的学生信息的组合,对象进行创建
Student result = new Student();
System.out.println("需要修改的学号信息");
String sno = input.next();
Student one = dao.getOne(sno);
if (one!=null){
result.setSno(sno);
System.out.println("输入要修改的学生的姓名(如果不修改 输入exit)");
String sname = input.next();
if("exit".equals(sname)){
result.setSname(one.getSname());
}else{
result.setSname(sname);
}
System.out.println("输入要修改的学生的年龄(如果不修改 输入0)");
int sage = input.nextInt();
if (sage==0){
result.setSage(one.getSage());
}else{
result.setSage(sage);
}
System.out.println("输入要修改的学生的性别(如果不修改 输入exit)");
String ssex = input.next();
if("exit".equals(ssex)){
result.setSsex(one.getSsex());
}else{
result.setSsex(ssex);
}
System.out.println("输入要修改的学生的所在系(如果不修改 输入exit)");
String dept = input.next();
if("exit".equals(dept)){
result.setDept(one.getDept());
}else{
result.setDept(dept);
}
int i = dao.updateOne(result);
if (i>0){
System.out.println("修改成功");
}else{
System.out.println("修改失败");
}
StuView.showMenu();
}else{
System.out.println("学号有误,重新输入");
update();
}
}
}
import com.sun.security.jgss.GSSUtil;
import dao.StudentDao;
import dao.StudentDapImpl;
import pojo.Student;
import service.StudentService;
import service.StudentServiecMpl;
import util.JdbcUtil;
import java.util.List;
import java.util.Scanner;
public class StuView {
private static StudentService service = new StudentServiecMpl();
private static StudentDao dao = new StudentDapImpl();
private static Scanner input = new Scanner(System.in);
public 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("其他数字 退出系统");
int i = input.nextInt();
switch (i) {
case 1:
service.getAll();
case 2:
service.add();
case 3:
service.update();
case 4:
service.del1();
default:
System.exit(-10);
break;
}
}
}
import pojo.Sc;
import util.JdbcUtil;
import view.StuView;
import java.sql.SQLException;
import java.util.List;
public class Demo {
public static void main(String[] args) {
StuView.showMenu();
}
}
本文章适合学完JDBC后用来练手的项目,希望各位能对各位同学有所帮助