- 论坛徽章:
- 0
|
下面是实体类代码
package demo;
public class Employee {
private int e_id;
private String name;
private String password;
private String email;
public int getE_id() {
return e_id;
}
public void setE_id(int e_id) {
this.e_id = e_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 getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
|
下面是数据库访问部分
package demo;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class EmployeeManager {
public Employee createEmployee(Employee emp) {
Connection conn = null;
PreparedStatement pstmt = null;
String insertSql = "insert into employee(name,password,email) values(?,?,?)";
try {
conn = ConnectionManager.getConnection();
pstmt = conn.prepareStatement(insertSql);
pstmt.setString(1, emp.getName());
pstmt.setString(2, emp.getPassword());
pstmt.setString(3, emp.getEmail());
pstmt.executeUpdate();
} catch (SQLException e) {
}
return emp;
}
public Employee updateEmployee(Employee emp) {
Connection conn = null;
PreparedStatement pstmt = null;
String insertSql = "update employee set name=? , password=? , email=? where e_id = ?";
try {
conn = ConnectionManager.getConnection();
pstmt = conn.prepareStatement(insertSql);
pstmt.setString(1, emp.getName());
pstmt.setString(2, emp.getPassword());
pstmt.setString(3, emp.getEmail());
pstmt.setInt(4, emp.getE_id());
pstmt.executeUpdate();
} catch (SQLException e) {
}
return emp;
}
public Employee getEmployee(int id) {
Connection conn = null;
PreparedStatement pstmt = null;
Employee emp = null;
ResultSet rs = null;
String insertSql = "select * from employee where e_id = ?";
try {
conn = ConnectionManager.getConnection();
pstmt = conn.prepareStatement(insertSql);
pstmt.setInt(1, id);
rs = pstmt.executeQuery();
while (rs.next()) {
emp = new Employee();
emp.setE_id(rs.getInt("e_id"));
emp.setName(rs.getString("name"));
emp.setPassword(rs.getString("password"));
emp.setEmail(rs.getString("email"));
}
} catch (SQLException e) {
}
return emp;
}
public void removeEmployee(int id) {
Connection conn = null;
PreparedStatement pstmt = null;
String insertSql = "delete from employee where e_id = ?";
try {
conn = ConnectionManager.getConnection();
pstmt = conn.prepareStatement(insertSql);
pstmt.setInt(1, id);
pstmt.executeUpdate();
} catch (SQLException e) {
}
}
} |
|
|