2022/06/24-22:43--update

This commit is contained in:
zyx 2022-06-24 22:48:07 +08:00
parent bd151d8ab7
commit 2480bbbf62
15 changed files with 558 additions and 20 deletions

7
.idea/sqldialects.xml generated Normal file
View File

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="SqlDialectMappings">
<file url="file://$PROJECT_DIR$/sql/v1.0.0.sql" dialect="MySQL" />
<file url="PROJECT" dialect="MySQL" />
</component>
</project>

View File

@ -38,6 +38,11 @@
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.26</version>
</dependency>
</dependencies>
<build>

20
sql/v1.0.0.sql Normal file
View File

@ -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 '班级'
);

View File

@ -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();
}

View File

@ -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());
}
}

View File

@ -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 + '\'' +
'}';
}
}

View File

@ -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 + '\'' +
'}';
}
}

View File

@ -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<T> {
private Class<T> clazz = null;
//获取当前BaseDAO的子类继承的父类中的泛型
{//只能用非静态静态代码块内不能调用非静态
//获取当前对象的父类的泛型
//此处this对象是子类的对象
Type genericSuperclass = this.getClass().getGenericSuperclass();
ParameterizedType paramType = (ParameterizedType) genericSuperclass;
//获取了父类的泛型参数
Type[] typeArguments = paramType.getActualTypeArguments();
//泛型的第一个参数
clazz = (Class<T>) 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<T>返回查询结果记录
*/
public List<T> 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<T> 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> 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;
}
}

View File

@ -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<Faculty> getFacultyByName(Connection connection,String name);
}

View File

@ -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<Faculty> 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<Faculty> getFacultyByName(Connection connection, String name) {
//language=MySQL
String sql = "";
return null;
}
}

View File

@ -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();
}
}
}
}

View File

@ -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;

View File

@ -1,16 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.control.Button?>
<VBox alignment="CENTER" spacing="20.0" xmlns:fx="http://javafx.com/fxml"
fx:controller="cn.czyx007.eas_gui.HelloController">
<padding>
<Insets bottom="20.0" left="20.0" right="20.0" top="20.0"/>
</padding>
<Label fx:id="welcomeText"/>
<Button text="Hello!" onAction="#onHelloButtonClick"/>
</VBox>

View File

@ -0,0 +1,53 @@
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.text.*?>
<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<VBox alignment="CENTER" spacing="20.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="cn.czyx007.eas_gui.HelloController">
<padding>
<Insets bottom="20.0" left="20.0" right="20.0" top="20.0" />
</padding>
<children>
<GridPane>
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" maxWidth="94.0" minWidth="10.0" prefWidth="69.0" />
<ColumnConstraints hgrow="SOMETIMES" maxWidth="140.0" minWidth="10.0" prefWidth="131.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<PasswordField fx:id="password" prefHeight="30.0" prefWidth="121.0" GridPane.columnIndex="1" GridPane.rowIndex="1" />
<TextField GridPane.columnIndex="1" />
<Text strokeType="OUTSIDE" strokeWidth="0.0" text=" 账号:" wrappingWidth="70.0" />
<Text strokeType="OUTSIDE" strokeWidth="0.0" text=" 密码:" wrappingWidth="70" GridPane.rowIndex="1" />
</children>
</GridPane>
<Label fx:id="welcomeText" />
<GridPane>
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<Button onAction="#onLoginButtonClick" prefHeight="30.0" prefWidth="51.0" text="登录" GridPane.columnIndex="1">
<GridPane.margin>
<Insets left="50.0" />
</GridPane.margin>
</Button>
<Button onAction="#onRegistryButtonClick" prefHeight="30.0" prefWidth="50.0" text="注册">
<GridPane.margin>
<Insets left="25.0" />
</GridPane.margin>
</Button>
</children>
</GridPane>
</children>
</VBox>

View File

@ -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