Java JDBC 员工管理系统:创建数据库表、添加测试数据及实现增删改查功能
Java JDBC 员工管理系统:创建数据库表、添加测试数据及实现增删改查功能
本教程将使用 Java JDBC 技术,实现一个简单的员工管理系统,包含数据库表创建、测试数据添加,以及查询部门员工、办理员工入职、调整员工薪资、员工离职、分页查询员工等功能。
1. 创建员工表
首先,使用以下 SQL 语句创建名为 'employee' 的员工表,包含员工编号 ('eno')、姓名 ('ename')、薪资 ('salary')、部门 ('dname') 和入职日期 ('hiredate') 等字段:
/*
Navicat Premium Data Transfer
Source Server : myconn
Source Server Type : MySQL
Source Server Version : 80031
Source Host : localhost:3306
Source Schema : xja
Target Server Type : MySQL
Target Server Version : 80031
File Encoding : 65001
Date: 06/08/2023 22:24:23
*/
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for employee
-- ----------------------------
DROP TABLE IF EXISTS `employee`;
CREATE TABLE `employee` (
`eno` int(0) NOT NULL,
`ename` varchar(64) CHARACTER SET utf8mb4 NOT NULL,
`salary` float(10, 2) NOT NULL,
`dname` varchar(64) CHARACTER SET utf8mb4 NOT NULL,
`hiredate` varchar(20) NULL DEFAULT NULL,
PRIMARY KEY (`eno`) USING BTREE
) ENGINE=InnoDB CHARACTER SET=utf8mb4 ROW_FORMAT=Dynamic;
-- ----------------------------
-- Records of employee
-- ----------------------------
INSERT INTO `employee` VALUES (1000, '员工1000', 5000.00, '市场部', '1992-03-04');
INSERT INTO `employee` VALUES (1001, '员工1001', 3500.00, '市场部', '1988-02-18');
INSERT INTO `employee` VALUES (1002, '员工1002', 4000.00, '市场部', '1996-02-01');
INSERT INTO `employee` VALUES (1003, '员工1003', 4000.00, '市场部', '2001-08-21');
INSERT INTO `employee` VALUES (1004, '员工1004', 4000.00, '市场部', '2003-01-02');
INSERT INTO `employee` VALUES (1005, '员工1005', 4000.00, '市场部', '1996-07-28');
INSERT INTO `employee` VALUES (1006, '员工1006', 4000.00, '市场部', '1999-12-30');
INSERT INTO `employee` VALUES (1007, '员工1007', 4000.00, '市场部', '2009-05-30');
INSERT INTO `employee` VALUES (1008, '员工1008', 4000.00, '市场部', '1984-05-30');
INSERT INTO `employee` VALUES (1009, '员工1009', 4000.00, '市场部', '2004-05-07');
INSERT INTO `employee` VALUES (3308, '张三', 6000.00, '研发部', '2011-05-08');
INSERT INTO `employee` VALUES (3420, '李四', 8700.00, '研发部', '2006-11-11');
INSERT INTO `employee` VALUES (3610, '王五', 4550.00, '市场部', '2009-10-01');
SET FOREIGN_KEY_CHECKS = 1;
2. 实现员工管理系统功能
以下代码展示了使用 Java JDBC 实现员工管理系统的示例代码,包含查询部门员工、办理员工入职、调整员工薪资、员工离职和分页查询员工等功能。
2.1 查询部门员工
import java.sql.*;
public class EmployeeManagementSystem {
public static void main(String[] args) {
String dname = "市场部"; // 部门名称
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
// 1. 加载驱动程序
Class.forName("com.mysql.cj.jdbc.Driver");
// 2. 建立数据库连接
String url = "jdbc:mysql://localhost:3306/xja?useSSL=false&serverTimezone=UTC";
String user = "root";
String password = "123456";
conn = DriverManager.getConnection(url, user, password);
// 3. 创建Statement对象
stmt = conn.createStatement();
// 4. 执行SQL语句
String sql = "SELECT * FROM employee WHERE dname='" + dname + "'";
rs = stmt.executeQuery(sql);
// 5. 处理结果集
while (rs.next()) {
int eno = rs.getInt("eno");
String ename = rs.getString("ename");
float salary = rs.getFloat("salary");
String hiredate = rs.getString("hiredate");
System.out.println("员工编号:" + eno + ",姓名:" + ename + ",薪资:" + salary + ",入职日期:" + hiredate);
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
// 6. 释放资源
try {
if (rs != null) {
rs.close();
}
if (stmt != null) {
stmt.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
2.2 办理员工入职
import java.sql.*;
public class EmployeeManagementSystem {
public static void main(String[] args) {
int eno = 1010; // 员工编号
String ename = "员工1010"; // 姓名
float salary = 5500.00f; // 薪资
String dname = "市场部"; // 部门名称
String hiredate = "2021-08-01"; // 入职日期
Connection conn = null;
Statement stmt = null;
try {
// 1. 加载驱动程序
Class.forName("com.mysql.cj.jdbc.Driver");
// 2. 建立数据库连接
String url = "jdbc:mysql://localhost:3306/xja?useSSL=false&serverTimezone=UTC";
String user = "root";
String password = "123456";
conn = DriverManager.getConnection(url, user, password);
// 3. 创建Statement对象
stmt = conn.createStatement();
// 4. 执行SQL语句
String sql = "INSERT INTO employee VALUES (" + eno + ", '" + ename + "', " + salary + ", '" + dname + "', '" + hiredate + "')";
int rows = stmt.executeUpdate(sql);
// 5. 处理结果
if (rows > 0) {
System.out.println("员工入职成功!");
} else {
System.out.println("员工入职失败!");
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
// 6. 释放资源
try {
if (stmt != null) {
stmt.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
2.3 调整员工薪资
import java.sql.*;
public class EmployeeManagementSystem {
public static void main(String[] args) {
int eno = 1000; // 员工编号
float newsalary = 5500.00f; // 新薪资
Connection conn = null;
Statement stmt = null;
try {
// 1. 加载驱动程序
Class.forName("com.mysql.cj.jdbc.Driver");
// 2. 建立数据库连接
String url = "jdbc:mysql://localhost:3306/xja?useSSL=false&serverTimezone=UTC";
String user = "root";
String password = "123456";
conn = DriverManager.getConnection(url, user, password);
// 3. 创建Statement对象
stmt = conn.createStatement();
// 4. 执行SQL语句
String sql = "UPDATE employee SET salary=" + newsalary + " WHERE eno=" + eno;
int rows = stmt.executeUpdate(sql);
// 5. 处理结果
if (rows > 0) {
System.out.println("员工薪资调整成功!");
} else {
System.out.println("员工薪资调整失败!");
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
// 6. 释放资源
try {
if (stmt != null) {
stmt.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
2.4 员工离职
import java.sql.*;
public class EmployeeManagementSystem {
public static void main(String[] args) {
int eno = 1010; // 员工编号
Connection conn = null;
Statement stmt = null;
try {
// 1. 加载驱动程序
Class.forName("com.mysql.cj.jdbc.Driver");
// 2. 建立数据库连接
String url = "jdbc:mysql://localhost:3306/xja?useSSL=false&serverTimezone=UTC";
String user = "root";
String password = "123456";
conn = DriverManager.getConnection(url, user, password);
// 3. 创建Statement对象
stmt = conn.createStatement();
// 4. 执行SQL语句
String sql = "DELETE FROM employee WHERE eno=" + eno;
int rows = stmt.executeUpdate(sql);
// 5. 处理结果
if (rows > 0) {
System.out.println("员工离职成功!");
} else {
System.out.println("员工离职失败!");
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
// 6. 释放资源
try {
if (stmt != null) {
stmt.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
2.5 分页查询员工
import java.sql.*;
public class EmployeeManagementSystem {
public static void main(String[] args) {
int currentPage = 2; // 当前页码
int pageSize = 3; // 每页显示记录数
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
// 1. 加载驱动程序
Class.forName("com.mysql.cj.jdbc.Driver");
// 2. 建立数据库连接
String url = "jdbc:mysql://localhost:3306/xja?useSSL=false&serverTimezone=UTC";
String user = "root";
String password = "123456";
conn = DriverManager.getConnection(url, user, password);
// 3. 创建Statement对象
stmt = conn.createStatement();
// 4. 执行SQL语句
int start = (currentPage - 1) * pageSize;
String sql = "SELECT * FROM employee LIMIT " + start + ", " + pageSize;
rs = stmt.executeQuery(sql);
// 5. 处理结果集
while (rs.next()) {
int eno = rs.getInt("eno");
String ename = rs.getString("ename");
float salary = rs.getFloat("salary");
String dname = rs.getString("dname");
String hiredate = rs.getString("hiredate");
System.out.println("员工编号:" + eno + ",姓名:" + ename + ",薪资:" + salary + ",部门名称:" + dname + ",入职日期:" + hiredate);
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
// 6. 释放资源
try {
if (rs != null) {
rs.close();
}
if (stmt != null) {
stmt.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
3. 总结
本教程展示了如何使用 Java JDBC 技术实现一个简单的员工管理系统,包含数据库表创建、测试数据添加,以及查询部门员工、办理员工入职、调整员工薪资、员工离职、分页查询员工等功能。 通过学习本教程,您可以了解如何使用 JDBC 连接数据库、执行 SQL 语句,以及处理结果集。
注意:
- 请将代码中的数据库连接信息(URL、用户名、密码)替换成您自己的数据库信息。
- 以上代码仅供参考,您可以根据实际需求进行修改和扩展。
- 建议您使用预处理语句(PreparedStatement)来执行 SQL 语句,以防止 SQL 注入攻击。
原文地址: https://www.cveoy.top/t/topic/oxWi 著作权归作者所有。请勿转载和采集!