利用JDBC连接数据库创建一个员工信息系统并实现基本的增删改除
时间: 2024-03-20 15:41:46 浏览: 46
JDBC数据库连接及增删查改
好的,我可以为您提供相关的代码示例来实现您所描述的员工信息系统的基本增删改查功能。以下是一个简单的示例代码:
```java
import java.sql.*;
public class EmployeeSystem {
// 数据库连接信息
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://localhost:3306/employee";
static final String USER = "root";
static final String PASS = "password";
// 增加员工信息
public static void addEmployee(int id, String name, String position, double salary) {
Connection conn = null;
Statement stmt = null;
try {
Class.forName(JDBC_DRIVER);
conn = DriverManager.getConnection(DB_URL, USER, PASS);
stmt = conn.createStatement();
String sql = "INSERT INTO employees(id, name, position, salary) VALUES(" + id + ",'" + name + "','" + position + "'," + salary + ")";
stmt.executeUpdate(sql);
} catch (SQLException se) {
se.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (stmt != null) stmt.close();
} catch (SQLException se2) {
}
try {
if (conn != null) conn.close();
} catch (SQLException se) {
se.printStackTrace();
}
}
}
// 删除员工信息
public static void deleteEmployee(int id) {
Connection conn = null;
Statement stmt = null;
try {
Class.forName(JDBC_DRIVER);
conn = DriverManager.getConnection(DB_URL, USER, PASS);
stmt = conn.createStatement();
String sql = "DELETE FROM employees WHERE id=" + id;
stmt.executeUpdate(sql);
} catch (SQLException se) {
se.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (stmt != null) stmt.close();
} catch (SQLException se2) {
}
try {
if (conn != null) conn.close();
} catch (SQLException se) {
se.printStackTrace();
}
}
}
// 更新员工信息
public static void updateEmployee(int id, String name, String position, double salary) {
Connection conn = null;
Statement stmt = null;
try {
Class.forName(JDBC_DRIVER);
conn = DriverManager.getConnection(DB_URL, USER, PASS);
stmt = conn.createStatement();
String sql = "UPDATE employees SET name='" + name + "',position='" + position + "',salary=" + salary + " WHERE id=" + id;
stmt.executeUpdate(sql);
} catch (SQLException se) {
se.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (stmt != null) stmt.close();
} catch (SQLException se2) {
}
try {
if (conn != null) conn.close();
} catch (SQLException se) {
se.printStackTrace();
}
}
}
// 查询员工信息
public static void queryEmployee() {
Connection conn = null;
Statement stmt = null;
try {
Class.forName(JDBC_DRIVER);
conn = DriverManager.getConnection(DB_URL, USER, PASS);
stmt = conn.createStatement();
String sql = "SELECT * FROM employees";
ResultSet rs = stmt.executeQuery(sql);
while (rs.next()) {
int id = rs.getInt("id");
String name = rs.getString("name");
String position = rs.getString("position");
double salary = rs.getDouble("salary");
System.out.println("ID: " + id + ", Name: " + name + ", Position: " + position + ", Salary: " + salary);
}
rs.close();
} catch (SQLException se) {
se.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (stmt != null) stmt.close();
} catch (SQLException se2) {
}
try {
if (conn != null) conn.close();
} catch (SQLException se) {
se.printStackTrace();
}
}
}
public static void main(String[] args) {
// 增加员工信息
addEmployee(1, "Tom", "Engineer", 5000.0);
addEmployee(2, "Jerry", "Manager", 8000.0);
queryEmployee();
// 更新员工信息
updateEmployee(2, "Jerry Smith", "Senior Manager", 10000.0);
queryEmployee();
// 删除员工信息
deleteEmployee(1);
queryEmployee();
}
}
```
以上代码演示了如何使用JDBC连接MySQL数据库,并实现了基本的增删改查功能。您可以根据实际需求进行修改和扩展。
阅读全文