📄 employeebean.java
字号:
package com.jspdev.ch17;
import java.sql.*;
import java.util.*;
public class EmployeeBean
{
private Connection conn ;
public EmployeeBean()throws Exception
{
this.conn=DatabaseConn.getConnection();
}
public void addEmployee(Employee employee)throws Exception
{
PreparedStatement pstmt = conn.prepareStatement("insert into employee values(?,?,?,?,?)");
pstmt.setString(1, employee.getId());
pstmt.setString(2, employee.getName());
pstmt.setFloat(3, employee.getSalary());
pstmt.setInt(4, employee.getAge());
pstmt.setString(5, employee.getDepartment());
try {
pstmt.execute();
}
catch (Exception e) {
e.printStackTrace();
}
}
public Collection getNoDeptEmployees()throws Exception
{
return this.getEmployeeHelper("select * from Employee where department='temp'");
}
public Collection getDepartmentDemployees(String deptId)throws Exception
{
return this.getEmployeeHelper("select * from employee where department='"+deptId+"'");
}
public void deleteEmployeeFromDept(String employId,String deptId)throws Exception
{
try
{
this.executeHelper("update employee set department='temp' where id='"+employId+"'");
}
catch(Exception e)
{
e.printStackTrace();
throw e;
}
}
public void resetEmploy(String employId,String deptId)throws Exception
{
try
{
this.executeHelper("update employee set department='"+deptId+"' where id='"+employId+"'");
}
catch(Exception e)
{
e.printStackTrace();
throw e;
}
}
public Collection getEmployees() throws Exception
{
return this.getEmployeeHelper("select * from Employee");
}
private Collection getEmployeeHelper(String sql)throws Exception
{
Statement stmt=conn.createStatement();
ResultSet rst=stmt.executeQuery(sql);
Collection ret=new ArrayList();
while(rst.next())
{
Employee temp=new Employee();
temp.setId(rst.getString("id"));
temp.setName(rst.getString("name"));
temp.setAge(rst.getInt("age"));
temp.setSalary(rst.getFloat("salary"));
temp.setDepartment(rst.getString("department"));
ret.add(temp);
}
return ret;
}
private void executeHelper(String sql)throws Exception
{
Statement stmt=conn.createStatement();
stmt.executeUpdate(sql);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -