在项目中建立四个包,分别是com.wu.JavaBean、com.wuJavaDao、com.wu.JavaService、com.wu.JavaView
数据库表结构
学生表只有四个属性:学生姓名、学生性别、学生学号(主键)、学生班级
管理员表只有两个属性:管理员用户名(主键)、管理员密码
这里笔者为了简单,学生表只写了四个属性,管理员表只写了两个属性。
在JavaBean新建Student和Root类,如下:
Student.java:
package com.wu.JavaBean;
/**
*
* @date 2020年12月15日下午9:49:51
* @author 一夜星尘
*/
public class Student {
private String name;
private String gender;
private String id;
private String team;
public Student() {
}
public Student(String name,String gender,String id,String team) {
this.name = name;
this.id = id;
this.team = team;
this.gender = gender;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getTeam() {
return team;
}
public void setTeam(String team) {
this.team = team;
}
}
Root.java:
package com.wu.JavaBean;
/**
*
* @date 2020年12月15日下午9:50:30
* @author 一夜星尘
*/
public class Root {
private String username; // 账号
private String password; // 密码
private String superroot ; // 超级管理员身份 唯一一个
public Root(String username) {
this.username = username;
}
public Root(String username,String password,String superroot) {
this.username = username;
this.password = password;
this.superroot = superroot;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public boolean isSuperRoot() {
return superroot.equals("1"); // 1代表超级管理员
}
}
建立数据库连接DAO层,即在JavaDao包下建立JDBC.java,该程序只是控制数据库的连接:
package com.wu.JavaDao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
/**
* @date 2020年12月15日下午9:58:11
* @author 一夜星尘
*/
public class JDBC {
private Connection sqllink = null;
/**
* 获取数据库连接对象
* @return
* @throws Exception
*/
public Connection getConnection() throws Exception{
String DATABASE_DRIVER = "com.mysql.cj.jdbc.Driver";
String DATABASE_URL = "jdbc:mysql://127.0.0.1:3306/jdbc_db"+
"?charcterEncoding=utf-8&useSSL=false&serverTimezone=UTC&allowPublicKeyRetrieval=true";
String DATABASE_USER = "root";
String DATABASE_PASSWORD = "root";
try {
Class.forName(DATABASE_DRIVER); // 注册驱动
sqllink = DriverManager.getConnection(DATABASE_URL,DATABASE_USER,DATABASE_PASSWORD); // 连接数据库
return this.sqllink;
}catch(SQLException e) {
e.printStackTrace();
System.out.println("连接数据库异常"); // 错误信息显示到控制台
return this.sqllink;
}
}
/**
* 关闭数据库连接对象
* @throws Exception
*/
public void closeConnection() throws Exception{
try {
if(this.sqllink != null) {
this.sqllink.close();
}
}catch(SQLException e) {
System.out.println("关闭数据库连接异常");
}
}
}
com.wu.JavaBean和com.wu.JavaDao已经全部完成了,接下来就是完成业务逻辑JavaService包下的实现
对于增添数据的业务方法Add.java:
package com.wu.JavaService;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import com.wu.JavaBean.Root;
import com.wu.JavaBean.Student;
import com.wu.JavaDao.JDBC;
/**
* @date 2020年12月15日下午9:59:09
* @author 一夜星尘
*/
public class Add {
/**
* 添加信息
* @param element 学生或者管理员
* @return
* @throws Exception
*/
public static boolean add(Object element) throws Exception{
// 多态
// 获取数据库对象
JDBC jdbc = new JDBC();
Connection sqllink = jdbc.getConnection();
PreparedStatement sqlaction = null; // 创建一个数据库操作对象
if(element instanceof Student) {
String sql = "insert into student(name,gender,id,team) values(?,?,?,?)"; // mysql插入语句
Student student = (Student) element; // 向下转型
try {
sqlaction = sqllink.prepareStatement(sql); // 操作对象
sqlaction.setString(1,student.getName());
sqlaction.setString(2,student.getGender());
sqlaction.setString(3,student.getId());
sqlaction.setString(4,student.getTeam());
int count = sqlaction.executeUpdate(); // 执行操作
return (count == 1) ? true : false;
}catch(SQLException e) {
return false;
}finally{
jdbc.closeConnection(); // 关闭数据库连接
if(sqlaction != null) {
sqlaction.close();
}
}
}else if(element instanceof Root) {
String sql = "insert into root(username,password,superroot) values(?,?,0)"; // mysql插入语句
Root root = (Root) element; // 向下转型
// 超级管理员权限
if(!root.isSuperRoot()) {
return false;
}
try {
sqlaction = sqllink.prepareStatement(sql); // 操作对象
sqlaction.setString(1,root.getUsername());
sqlaction.setString(2,root.getPassword());
int count = sqlaction.executeUpdate(); // 执行操作
return (count == 1) ? true : false;
}catch(SQLException e) {
return false;
}finally{
jdbc.closeConnection(); // 关闭数据库连接
if(sqlaction != null) {
sqlaction.close();
}
}
}else {
System.out.println("对象传入错误");
return false;
}
}
}
对于删除Remove.java:
package com.wu.JavaService;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import com.wu.JavaBean.Root;
import com.wu.JavaBean.Student;
import com.wu.JavaDao.JDBC;
/**
* @date 2020年12月15日下午10:00:30
* @author 一夜星尘
*/
public class Remove {
/**
* 移除学生信息
* @param student 待移除的学生
* @param pos 移除方式
* @return
* @throws Exception
*/
public static boolean removeStudent(Student student ,String username,int pos) throws Exception{
// 部分修改或者全部修改
// 权限判断 只有超级管理员才能实现全部学生删除
if (pos == 0 && !Find.getAccess(username).equals("1")) {
return false;
}
// 获取数据库对象
JDBC jdbc = new JDBC();
Connection sqllink = jdbc.getConnection();
PreparedStatement sqlaction = null; // 创建一个数据库操作对象
String sql = "";
String[] info = new String[4];
/**
* 0代表删除所有学生
* 1代表删除所有姓名为name的学生
* 2代表删除所有性别为gender的学生
* 3代表删除一个学号为id的学生
* 4代表删除所有班级为team的学生
* 5代表删除所有姓名为name性别为gender的学生
* 6代表删除一个学号为id姓名为name的学生
* 7代表删除所有姓名为name的班级为team的学生
* 8代表删除性别为gender学号为id的一个学生
* 9代表删除所有性别为gender班级为team的学生
* 10代表删除一个学号为id班级为team的学生
* 11代表删除一个姓名为name性别为gender学号为id的学生
* 12代表删除所有姓名为name性别为gender班级为team的学生
* 13代表删除删除一个姓名为name学号为id班级为team的学生
* 14代表删除一个性别为gender学号为id班级为team的学生
* 15代表删除一个姓名为name性别为gender学号为id班级为team的学生
*/
switch(pos) {
case 0:
sql = "delete from student";
try {
sqlaction = sqllink.prepareStatement(sql);
sqlaction.executeUpdate();
return true;
}catch(SQLException e) {
e.printStackTrace();
return false;
}finally {
jdbc.closeConnection();
if(sqlaction != null) {
sqlaction.close();
}
}
case 1:
sql = "delete from student where name = ?";
info[0] = student.getName();
break;
case 2:
sql = "delete from student where gender = ?";
info[0] = student.getGender();
break;
case 3:
sql = "delete from student where id = ?";
info[0] = student.getId();
break;
case 4:
sql = "delete from student where team = ?";
info[0] = student.getTeam();
break;
case 5:
sql = "delete from student where name = ? and gender = ?";
info[0] = student.getName();
info[1] = student.getGender();
break;
case 6:
sql = "delete from student where name = ? and id = ?";
info[0] = student.getName();
info[1] = student.getId();
break;
case 7:
sql = "delete from student where name = ? and team = ?";
info[0] = student.getName();
info[1] = student.getTeam();
break;
case 8:
sql = "delete from student where gender = ? and id = ?";
info[0] = student.getGender();
info[1] = student.getId();
break;
case 9:
sql = "delete from student where gender = ? and team = ?";
info[0] = student.getId();
info[1] = student.getTeam();
break;
case 10:
sql = "delete from student where id = ? and team = ?";
info[0] = student.getName();
info[1] = student.getGender();
break;
case 11:
sql = "delete from student where name = ? and gender = ? and id = ?";
info[0] = student.getName();
info[1] = student.getGender();
info[2] = student.getId();
break;
case 12:
sql = "delete from student where name = ? and gender = ? and team = ?";
info[0] = student.getName();
info[1] = student.getGender();
info[2] = student.getTeam();
break;
case 13:
sql = "delete from student where name = ? and id = ? and team = ?";
info[0] = student.getName();
info[1] = student.getId();
info[2] = student.getTeam();
break;
case 14:
sql = "delete from student where gender = ? and id = ? and team = ?";
info[0] = student.getGender();
info[1] = student.getId();
info[2] = student.getTeam();
break;
case 15:
sql = "delete from student where name = ? and gender = ? and id = ? and team = ?";
info[0] = student.getName();
info[1] = student.getGender();
info[2] = student.getId();
info[3] = student.getTeam();
}
try {
sqlaction = sqllink.prepareStatement(sql);
switch(pos) {
case 1:
case 2:
case 3:
case 4:
sqlaction.setString(1, info[0]);
break;
case 5:
case 6:
case 7:
case 8:
case 9:
case 10:
sqlaction.setString(1, info[0]);
sqlaction.setString(2, info[1]);
break;
case 11:
case 12:
case 13:
case 14:
sqlaction.setString(1, info[0]);
sqlaction.setString(2, info[1]);
sqlaction.setString(3, info[2]);
break;
case 15:
sqlaction.setString(1, info[0]);
sqlaction.setString(2, info[1]);
sqlaction.setString(3, info[2]);
sqlaction.setString(4, info[3]);
break;
}
sqlaction.executeUpdate();
return true;
}catch(SQLException e) {
e.printStackTrace();
return false;
}finally {
jdbc.closeConnection();
if(sqlaction != null) {
sqlaction.close();
}
}
}
/**
* 删除管理员信息
* @param root 待删除管理员
* @return
* @throws Exception
*/
public static boolean removeRoot(Root root) throws Exception{
// 完全删除
// 权限判断
if(!root.isSuperRoot()) {
return false;
}
// 获取数据库对象
JDBC jdbc = new JDBC();
Connection sqllink = jdbc.getConnection();
PreparedStatement sqlaction = null; // 创建一个数据库操作对象
String sql = "delete from root where username = ? ";
try {
sqlaction = sqllink.prepareStatement(sql);
sqlaction.setString(1,root.getUsername());
int count = sqlaction.executeUpdate();
return count == 1?true : false;
}catch(SQLException e) {
e.printStackTrace();
return false;
}finally {
jdbc.closeConnection();
if(sqlaction != null) {
sqlaction.close