From 2480bbbf62d56f1f44558ab17de7e3a5e4accc85 Mon Sep 17 00:00:00 2001 From: zyx <1029606625@qq.com> Date: Fri, 24 Jun 2022 22:48:07 +0800 Subject: [PATCH] 2022/06/24-22:43--update --- .idea/sqldialects.xml | 7 + pom.xml | 5 + sql/v1.0.0.sql | 20 +++ .../cn/czyx007/eas_gui/HelloApplication.java | 4 +- .../cn/czyx007/eas_gui/HelloController.java | 13 +- .../java/cn/czyx007/eas_gui/bean/Faculty.java | 75 +++++++++++ .../java/cn/czyx007/eas_gui/bean/Student.java | 86 ++++++++++++ .../java/cn/czyx007/eas_gui/dao/BaseDAO.java | 122 ++++++++++++++++++ .../cn/czyx007/eas_gui/dao/FacultyDAO.java | 24 ++++ .../eas_gui/dao/impl/FacultyDAOImpl.java | 49 +++++++ .../cn/czyx007/eas_gui/dbc/JDBCUtils.java | 99 ++++++++++++++ src/main/java/module-info.java | 1 + .../cn/czyx007/eas_gui/hello-view.fxml | 16 --- .../cn/czyx007/eas_gui/login-view.fxml | 53 ++++++++ src/main/resources/jdbc.properties | 4 + 15 files changed, 558 insertions(+), 20 deletions(-) create mode 100644 .idea/sqldialects.xml create mode 100644 sql/v1.0.0.sql create mode 100644 src/main/java/cn/czyx007/eas_gui/bean/Faculty.java create mode 100644 src/main/java/cn/czyx007/eas_gui/bean/Student.java create mode 100644 src/main/java/cn/czyx007/eas_gui/dao/BaseDAO.java create mode 100644 src/main/java/cn/czyx007/eas_gui/dao/FacultyDAO.java create mode 100644 src/main/java/cn/czyx007/eas_gui/dao/impl/FacultyDAOImpl.java create mode 100644 src/main/java/cn/czyx007/eas_gui/dbc/JDBCUtils.java delete mode 100644 src/main/resources/cn/czyx007/eas_gui/hello-view.fxml create mode 100644 src/main/resources/cn/czyx007/eas_gui/login-view.fxml create mode 100644 src/main/resources/jdbc.properties diff --git a/.idea/sqldialects.xml b/.idea/sqldialects.xml new file mode 100644 index 0000000..e22923a --- /dev/null +++ b/.idea/sqldialects.xml @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/pom.xml b/pom.xml index 50edfef..15b0e6e 100644 --- a/pom.xml +++ b/pom.xml @@ -38,6 +38,11 @@ ${junit.version} test + + mysql + mysql-connector-java + 8.0.26 + diff --git a/sql/v1.0.0.sql b/sql/v1.0.0.sql new file mode 100644 index 0000000..1a7c5f0 --- /dev/null +++ b/sql/v1.0.0.sql @@ -0,0 +1,20 @@ +create database if not exists education_administration_system default character set utf8mb4 collate utf8mb4_0900_ai_ci; + +use education_administration_system; + +create table if not exists Faculty( + id char(15) primary key comment '职工号', + name varchar(15) not null comment '姓名', + password varchar(40) not null comment '密码(SHA加密)', + department varchar(30) not null comment '部门/所在系', + title varchar(15) comment '职称' +); + +create table if not exists Student( + id char(15) primary key comment '学号', + name varchar(15) not null comment '姓名', + password varchar(40) not null comment '密码(SHA加密)', + department varchar(30) not null comment '所在系', + grade int not null comment '年级', + stuClass varchar(20) not null comment '班级' +); diff --git a/src/main/java/cn/czyx007/eas_gui/HelloApplication.java b/src/main/java/cn/czyx007/eas_gui/HelloApplication.java index a5cbea3..50299e4 100644 --- a/src/main/java/cn/czyx007/eas_gui/HelloApplication.java +++ b/src/main/java/cn/czyx007/eas_gui/HelloApplication.java @@ -10,9 +10,9 @@ import java.io.IOException; public class HelloApplication extends Application { @Override public void start(Stage stage) throws IOException { - FXMLLoader fxmlLoader = new FXMLLoader(HelloApplication.class.getResource("hello-view.fxml")); + FXMLLoader fxmlLoader = new FXMLLoader(HelloApplication.class.getResource("login-view.fxml")); Scene scene = new Scene(fxmlLoader.load(), 320, 240); - stage.setTitle("Hello!"); + stage.setTitle("教务信息管理系统"); stage.setScene(scene); stage.show(); } diff --git a/src/main/java/cn/czyx007/eas_gui/HelloController.java b/src/main/java/cn/czyx007/eas_gui/HelloController.java index 0e40e63..0d961a0 100644 --- a/src/main/java/cn/czyx007/eas_gui/HelloController.java +++ b/src/main/java/cn/czyx007/eas_gui/HelloController.java @@ -1,14 +1,23 @@ package cn.czyx007.eas_gui; +import javafx.event.ActionEvent; import javafx.fxml.FXML; import javafx.scene.control.Label; +import javafx.scene.control.PasswordField; public class HelloController { + @FXML PasswordField password; + @FXML private Label welcomeText; @FXML - protected void onHelloButtonClick() { - welcomeText.setText("Welcome to JavaFX Application!"); + protected void onLoginButtonClick() { + welcomeText.setText("尝试登录"); + } + + @FXML + protected void onRegistryButtonClick() { + welcomeText.setText("输入的密码为:" + password.getText()); } } \ No newline at end of file diff --git a/src/main/java/cn/czyx007/eas_gui/bean/Faculty.java b/src/main/java/cn/czyx007/eas_gui/bean/Faculty.java new file mode 100644 index 0000000..b795550 --- /dev/null +++ b/src/main/java/cn/czyx007/eas_gui/bean/Faculty.java @@ -0,0 +1,75 @@ +package cn.czyx007.eas_gui.bean; + +/** + * @author : 张宇轩 + * @createTime : 2022/6/24 - 22:05 + */ +public class Faculty { + private String id;//职工号 + private String name;//姓名 + private String password;//密码 + private String department;// 部门 / 所在系 + private String title;//职称 + + public Faculty() { + } + + public Faculty(String id, String name, String password, String department, String title) { + this.id = id; + this.name = name; + this.password = password; + this.department = department; + this.title = title; + } + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getPassword() { + return password; + } + + public void setPassword(String password) { + this.password = password; + } + + public String getDepartment() { + return department; + } + + public void setDepartment(String department) { + this.department = department; + } + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } + + @Override + public String toString() { + return "Faculty{" + + "id='" + id + '\'' + + ", name='" + name + '\'' + + ", password='" + password + '\'' + + ", department='" + department + '\'' + + ", title='" + title + '\'' + + '}'; + } +} diff --git a/src/main/java/cn/czyx007/eas_gui/bean/Student.java b/src/main/java/cn/czyx007/eas_gui/bean/Student.java new file mode 100644 index 0000000..2e7981b --- /dev/null +++ b/src/main/java/cn/czyx007/eas_gui/bean/Student.java @@ -0,0 +1,86 @@ +package cn.czyx007.eas_gui.bean; + +/** + * @author : 张宇轩 + * @createTime : 2022/6/24 - 22:07 + */ +public class Student { + private String id;//学号 + private String name;//姓名 + private String password;//密码 + private String department;//所在系 + private Integer grade;//年级 + private String stuClass;//班级 + + public Student() { + } + + public Student(String id, String name, String password, String department, Integer grade, String stuClass) { + this.id = id; + this.name = name; + this.password = password; + this.department = department; + this.grade = grade; + this.stuClass = stuClass; + } + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getPassword() { + return password; + } + + public void setPassword(String password) { + this.password = password; + } + + public String getDepartment() { + return department; + } + + public void setDepartment(String department) { + this.department = department; + } + + public Integer getGrade() { + return grade; + } + + public void setGrade(Integer grade) { + this.grade = grade; + } + + public String getStuClass() { + return stuClass; + } + + public void setStuClass(String stuClass) { + this.stuClass = stuClass; + } + + @Override + public String toString() { + return "Student{" + + "id='" + id + '\'' + + ", name='" + name + '\'' + + ", password='" + password + '\'' + + ", department='" + department + '\'' + + ", grade=" + grade + + ", stuClass='" + stuClass + '\'' + + '}'; + } +} diff --git a/src/main/java/cn/czyx007/eas_gui/dao/BaseDAO.java b/src/main/java/cn/czyx007/eas_gui/dao/BaseDAO.java new file mode 100644 index 0000000..bc8d943 --- /dev/null +++ b/src/main/java/cn/czyx007/eas_gui/dao/BaseDAO.java @@ -0,0 +1,122 @@ +package cn.czyx007.eas_gui.dao; + +import cn.czyx007.eas_gui.dbc.JDBCUtils; + +import java.lang.reflect.Field; +import java.lang.reflect.ParameterizedType; +import java.lang.reflect.Type; +import java.sql.*; +import java.util.ArrayList; +import java.util.List; +import java.sql.Connection; +import java.sql.PreparedStatement; + +/** + * Data Access Object + * 封装了针对于数据表的通用操作 + * + * @author : 张宇轩 + * @createTime : 2022/4/13 - 21:52 + */ +public abstract class BaseDAO { + private Class clazz = null; + + //获取当前BaseDAO的子类继承的父类中的泛型 + {//只能用非静态,静态代码块内不能调用非静态 + //获取当前对象的父类的泛型 + //此处this对象是子类的对象 + Type genericSuperclass = this.getClass().getGenericSuperclass(); + ParameterizedType paramType = (ParameterizedType) genericSuperclass; + //获取了父类的泛型参数 + Type[] typeArguments = paramType.getActualTypeArguments(); + //泛型的第一个参数 + clazz = (Class) typeArguments[0]; + } + + public static int update(Connection connection, String sql, Object... args) { + PreparedStatement ps = null; + try { + ps = connection.prepareStatement(sql); + for (int i = 0; i < args.length; i++) { + ps.setObject(i + 1, args[i]); + } + //返回增删改操作影响的行数 + return ps.executeUpdate(); + } catch (Exception e) { + e.printStackTrace(); + } finally { + JDBCUtils.closeResource(null, ps); + } + return 0; + } + + /** + * 针对不同的表的通用的查询操作,返回所有记录 + * ver2.0 考虑到数据库事务 + * + * @param sql 要执行的sql语句 + * @param args 填充占位符 + * @return List返回查询结果记录 + */ + public List search(Connection connection, String sql, Object... args) { + PreparedStatement ps = null; + ResultSet resultSet = null; + try { + ps = connection.prepareStatement(sql); + for (int i = 0; i < args.length; i++) { + ps.setObject(i + 1, args[i]); + } + + resultSet = ps.executeQuery(); + + ResultSetMetaData metaData = resultSet.getMetaData(); + int columnCount = metaData.getColumnCount(); + + ArrayList results = new ArrayList<>(); + + while (resultSet.next()) { + T t = clazz.newInstance(); + for (int i = 0; i < columnCount; i++) { + Object columnValue = resultSet.getObject(i + 1); + + //获取列的别名(没有起别名时默认就是列名) + String columnLabel = metaData.getColumnLabel(i + 1); + + Field field = t.getClass().getDeclaredField(columnLabel); + field.setAccessible(true); + field.set(t, columnValue); + } + results.add(t); + } + + return results; + + } catch (Exception e) { + e.printStackTrace(); + } finally { + JDBCUtils.closeResource(null, ps, resultSet); + } + return null; + } + + //用于查询特殊值的通用方法 + public E getValue(Connection connection,String sql,Object ...args) { + PreparedStatement ps = null; + ResultSet rs = null; + try { + ps = connection.prepareStatement(sql); + for (int i = 0; i < args.length; i++) { + ps.setObject(i+1,args[i]); + } + rs = ps.executeQuery(); + if (rs.next()){ + return (E) rs.getObject(1); + } + } catch (SQLException e) { + e.printStackTrace(); + } finally { + JDBCUtils.closeResource(null,ps,rs); + } + return null; + } +} diff --git a/src/main/java/cn/czyx007/eas_gui/dao/FacultyDAO.java b/src/main/java/cn/czyx007/eas_gui/dao/FacultyDAO.java new file mode 100644 index 0000000..21d25e8 --- /dev/null +++ b/src/main/java/cn/czyx007/eas_gui/dao/FacultyDAO.java @@ -0,0 +1,24 @@ +package cn.czyx007.eas_gui.dao; + +import cn.czyx007.eas_gui.bean.Faculty; + +import java.sql.Connection; +import java.util.List; +import java.util.Map; + +/** + * @author : 张宇轩 + * @createTime : 2022/6/24 - 22:28 + */ +public interface FacultyDAO { + //添加一名教职工 + boolean addFaculty(Connection connection,Faculty faculty); + //根据职工号删除一名教职工 + boolean deleteFaculty(Connection connection,String id); + //根据职工号更新一名教职工的信息 + boolean updateFaculty(Connection connection,String id); + //根据职工号查找一名教职工 + Faculty getFacultyById(Connection connection,String id); + //根据姓名查找匹配的教职工 + List getFacultyByName(Connection connection,String name); +} diff --git a/src/main/java/cn/czyx007/eas_gui/dao/impl/FacultyDAOImpl.java b/src/main/java/cn/czyx007/eas_gui/dao/impl/FacultyDAOImpl.java new file mode 100644 index 0000000..46c2487 --- /dev/null +++ b/src/main/java/cn/czyx007/eas_gui/dao/impl/FacultyDAOImpl.java @@ -0,0 +1,49 @@ +package cn.czyx007.eas_gui.dao.impl; + +import cn.czyx007.eas_gui.bean.Faculty; +import cn.czyx007.eas_gui.dao.BaseDAO; +import cn.czyx007.eas_gui.dao.FacultyDAO; + +import java.sql.Connection; +import java.util.List; + +/** + * @author : 张宇轩 + * @createTime : 2022/6/24 - 22:29 + */ +public class FacultyDAOImpl extends BaseDAO implements FacultyDAO { + @Override + public boolean addFaculty(Connection connection, Faculty faculty) { + //language=MySQL + String sql = "insert into education_administration_system.faculty values (?,?,sha1(?),?,?)"; + return update(connection, sql, faculty.getId(), faculty.getName(), faculty.getPassword(), faculty.getDepartment(), faculty.getTitle()) > 0; + } + + @Override + public boolean deleteFaculty(Connection connection, String id) { + //language=MySQL + String sql = ""; + return false; + } + + @Override + public boolean updateFaculty(Connection connection, String id) { + //language=MySQL + String sql = ""; + return false; + } + + @Override + public Faculty getFacultyById(Connection connection, String id) { + //language=MySQL + String sql = ""; + return null; + } + + @Override + public List getFacultyByName(Connection connection, String name) { + //language=MySQL + String sql = ""; + return null; + } +} diff --git a/src/main/java/cn/czyx007/eas_gui/dbc/JDBCUtils.java b/src/main/java/cn/czyx007/eas_gui/dbc/JDBCUtils.java new file mode 100644 index 0000000..6eac802 --- /dev/null +++ b/src/main/java/cn/czyx007/eas_gui/dbc/JDBCUtils.java @@ -0,0 +1,99 @@ +package cn.czyx007.eas_gui.dbc; + +import java.io.InputStream; +import java.sql.*; +import java.util.Properties; + +/** + * @author : 张宇轩 + * @createTime : 2022/6/24 - 22:09 + */ +public class JDBCUtils { + /** + * 获取数据库的连接 + * @return 数据库连接 + * @throws Exception 获取数据库连接失败 + */ + public static Connection getConnection() throws Exception{ + //读取配置文件中的4个基本信息 + Properties pros = new Properties(); + InputStream is = JDBCUtils.class.getClassLoader().getResourceAsStream("jdbc.properties"); + pros.load(is); + + String user = pros.getProperty("user"); + String password = pros.getProperty("password"); + String url = pros.getProperty("url"); + String driverClass = pros.getProperty("driverClass"); + + //加载驱动 + Class.forName(driverClass); + + //获取连接 + return DriverManager.getConnection(url, user, password); + } + + /** + * 关闭数据库连接 + * @param connection 待关闭的connection + */ + public static void closeResource(Connection connection){ + if (connection != null){ + try { + connection.close(); + }catch (SQLException e){ + e.printStackTrace(); + } + } + } + + /** + * 关闭数据库连接和Statement的操作 + * @param connection 待关闭的connection + * @param ps 待关闭的ps + */ + public static void closeResource(Connection connection, PreparedStatement ps){ + if (ps != null) { + try { + ps.close(); + } catch (SQLException e) { + e.printStackTrace(); + } + } + if (connection != null) { + try { + connection.close(); + } catch (SQLException e) { + e.printStackTrace(); + } + } + } + + /** + * 关闭数据库连接和PreparedStatement的操作以及结果集 + * @param connection 待关闭的connection + * @param ps 待关闭的ps + */ + public static void closeResource(Connection connection, PreparedStatement ps, ResultSet rs){ + if (ps != null) { + try { + ps.close(); + } catch (SQLException e) { + e.printStackTrace(); + } + } + if (connection != null) { + try { + connection.close(); + } catch (SQLException e) { + e.printStackTrace(); + } + } + if (rs != null){ + try { + rs.close(); + } catch (SQLException e) { + e.printStackTrace(); + } + } + } +} diff --git a/src/main/java/module-info.java b/src/main/java/module-info.java index d29b321..b217d79 100644 --- a/src/main/java/module-info.java +++ b/src/main/java/module-info.java @@ -1,6 +1,7 @@ module cn.czyx007.eas_gui { requires javafx.controls; requires javafx.fxml; + requires java.sql; opens cn.czyx007.eas_gui to javafx.fxml; diff --git a/src/main/resources/cn/czyx007/eas_gui/hello-view.fxml b/src/main/resources/cn/czyx007/eas_gui/hello-view.fxml deleted file mode 100644 index 8b60590..0000000 --- a/src/main/resources/cn/czyx007/eas_gui/hello-view.fxml +++ /dev/null @@ -1,16 +0,0 @@ - - - - - - - - - - - - - diff --git a/src/main/resources/jdbc.properties b/src/main/resources/jdbc.properties new file mode 100644 index 0000000..2a27495 --- /dev/null +++ b/src/main/resources/jdbc.properties @@ -0,0 +1,4 @@ +user=root +password=zyx007 +url=jdbc:mysql://localhost:3306/education_administration_system?characterEncoding=utf8&useSSL=false&serverTimezone=GMT%2B8&rewriteBatchedStatements=true +driverClass=com.mysql.cj.jdbc.Driver \ No newline at end of file