创建员工表的sql语句如下SET NAMES utf8mb4;SET FOREIGN_KEY_CHECKS = 0;-- ------------------------------ Table structure for employee-- ----------------------------DROP TABLE IF EXISTS employee;CREATE TABLE employ
以下是使用JDBC完成上述功能的示例代码:
- 查询部门员工
import java.sql.*;
public class QueryEmployeesByDepartment {
public static void main(String[] args) {
try {
// 1. 加载数据库驱动
Class.forName("com.mysql.jdbc.Driver");
// 2. 建立数据库连接
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/database_name", "username", "password");
// 3. 创建Statement对象
Statement stmt = conn.createStatement();
// 4. 执行查询语句
String sql = "SELECT * FROM employee WHERE dname = '市场部'";
ResultSet 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");
Date hiredate = rs.getDate("hiredate");
System.out.println("Employee No: " + eno);
System.out.println("Employee Name: " + ename);
System.out.println("Salary: " + salary);
System.out.println("Department Name: " + dname);
System.out.println("Hire Date: " + hiredate);
System.out.println("------------------------");
}
// 6. 关闭资源
rs.close();
stmt.close();
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
- 办理员工入职
import java.sql.*;
public class AddEmployee {
public static void main(String[] args) {
try {
// 1. 加载数据库驱动
Class.forName("com.mysql.jdbc.Driver");
// 2. 建立数据库连接
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/database_name", "username", "password");
// 3. 创建PreparedStatement对象
String sql = "INSERT INTO employee (eno, ename, salary, dname, hiredate) VALUES (?, ?, ?, ?, ?)";
PreparedStatement pstmt = conn.prepareStatement(sql);
// 4. 设置参数
pstmt.setInt(1, 1234);
pstmt.setString(2, "新员工");
pstmt.setFloat(3, 5000.00f);
pstmt.setString(4, "市场部");
pstmt.setDate(5, Date.valueOf("2023-03-01"));
// 5. 执行插入操作
int rows = pstmt.executeUpdate();
if (rows > 0) {
System.out.println("员工入职办理成功!");
} else {
System.out.println("员工入职办理失败!");
}
// 6. 关闭资源
pstmt.close();
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
- 调整员工薪资
import java.sql.*;
public class UpdateEmployeeSalary {
public static void main(String[] args) {
try {
// 1. 加载数据库驱动
Class.forName("com.mysql.jdbc.Driver");
// 2. 建立数据库连接
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/database_name", "username", "password");
// 3. 创建PreparedStatement对象
String sql = "UPDATE employee SET salary = ? WHERE eno = ?";
PreparedStatement pstmt = conn.prepareStatement(sql);
// 4. 设置参数
pstmt.setFloat(1, 6000.00f);
pstmt.setInt(2, 44);
// 5. 执行更新操作
int rows = pstmt.executeUpdate();
if (rows > 0) {
System.out.println("员工薪资调整成功!");
} else {
System.out.println("员工薪资调整失败!");
}
// 6. 关闭资源
pstmt.close();
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
- 员工离职
import java.sql.*;
public class DeleteEmployee {
public static void main(String[] args) {
try {
// 1. 加载数据库驱动
Class.forName("com.mysql.jdbc.Driver");
// 2. 建立数据库连接
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/database_name", "username", "password");
// 3. 创建PreparedStatement对象
String sql = "DELETE FROM employee WHERE eno = ?";
PreparedStatement pstmt = conn.prepareStatement(sql);
// 4. 设置参数
pstmt.setInt(1, 1234);
// 5. 执行删除操作
int rows = pstmt.executeUpdate();
if (rows > 0) {
System.out.println("员工离职办理成功!");
} else {
System.out.println("员工离职办理失败!");
}
// 6. 关闭资源
pstmt.close();
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
- 分页查询员工
import java.sql.*;
public class PaginationQueryEmployees {
public static void main(String[] args) {
try {
// 1. 加载数据库驱动
Class.forName("com.mysql.jdbc.Driver");
// 2. 建立数据库连接
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/database_name", "username", "password");
// 3. 创建PreparedStatement对象
String sql = "SELECT * FROM employee LIMIT ?, ?";
PreparedStatement pstmt = conn.prepareStatement(sql);
// 4. 设置参数
int pageNo = 2;
int pageSize = 5;
pstmt.setInt(1, (pageNo - 1) * pageSize);
pstmt.setInt(2, pageSize);
// 5. 执行查询语句
ResultSet rs = pstmt.executeQuery();
// 6. 处理查询结果
while (rs.next()) {
int eno = rs.getInt("eno");
String ename = rs.getString("ename");
float salary = rs.getFloat("salary");
String dname = rs.getString("dname");
Date hiredate = rs.getDate("hiredate");
System.out.println("Employee No: " + eno);
System.out.println("Employee Name: " + ename);
System.out.println("Salary: " + salary);
System.out.println("Department Name: " + dname);
System.out.println("Hire Date: " + hiredate);
System.out.println("------------------------");
}
// 7. 关闭资源
rs.close();
pstmt.close();
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
请注意将上述代码中的 "database_name"、"username" 和 "password" 替换为您的实际数据库信息
原文地址: http://www.cveoy.top/t/topic/hAHc 著作权归作者所有。请勿转载和采集!