用JDBC实现一下上述要求中修改基础信息的后端功能代码
时间: 2024-04-21 16:28:12 浏览: 12
好的,以下是一个简单的 Java 代码示例,用于实现修改基础信息的后端功能:
```java
import java.sql.*;
public class UpdateEmployeeInfo {
private Connection conn;
private PreparedStatement pstmt;
public UpdateEmployeeInfo(String driver, String url, String user, String password) {
try {
Class.forName(driver);
conn = DriverManager.getConnection(url, user, password);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
public boolean updateEmployeeInfo(String empId, String empName, String empSex, String empBirthday, String empAddress, String empDepartment, String empSalary, String empAssessment, String empOther) {
try {
String sql = "update employee_info set emp_name=?, emp_sex=?, emp_birthday=?, emp_address=?, emp_department=?, emp_salary=?, emp_assessment=?, emp_other=? where emp_id=?";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, empName);
pstmt.setString(2, empSex);
pstmt.setString(3, empBirthday);
pstmt.setString(4, empAddress);
pstmt.setString(5, empDepartment);
pstmt.setString(6, empSalary);
pstmt.setString(7, empAssessment);
pstmt.setString(8, empOther);
pstmt.setString(9, empId);
int count = pstmt.executeUpdate();
if (count > 0) {
return true;
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
pstmt.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return false;
}
public static void main(String[] args) {
String driver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/test";
String user = "root";
String password = "root";
UpdateEmployeeInfo updateEmployeeInfo = new UpdateEmployeeInfo(driver, url, user, password);
boolean result = updateEmployeeInfo.updateEmployeeInfo("001", "张三", "男", "1990-01-01", "北京市海淀区", "技术部", "10000.00", "优秀", "无");
System.out.println(result);
}
}
```
上述代码中,首先使用 JDBC 连接数据库,在 updateEmployeeInfo 方法中,使用 PreparedStatement 对象执行 SQL 更新语句,将传入的员工基础信息进行更新,最后返回更新结果。