📄 departmentbean.java
字号:
package to.model.Department;
import java.sql.PreparedStatement;
import java.util.Collection;
import java.util.Vector;
import to.model.*;
import java.sql.Connection;
import java.sql.ResultSet;
public class DepartmentBean
{
Connection con=null;
ResultSet rs=null;
PreparedStatement ps=null;
DBConnection db=new DBConnection("CollegePool",500);
public String insertDepartment(DepartmentItem di)
{
try
{
con=db.getConnection();
ps=con.prepareStatement("insert into kjc_Department values(?,?,?,?)");
ps.setString(1,di.getDepartmentName());
ps.setString(2,di.getOfficeTel());
int Principal=Integer.parseInt(di.getPrincipal());
ps.setInt(3,Principal);
ps.setInt(4,di.getCollegeID());
ps.executeUpdate();
return "添加成功";
}//end block try
catch(Exception e)
{
e.printStackTrace();
return "添加失败";
}//end block catch
}//end method insertDepartment
public String updateDepartment(DepartmentItem di)
{
try
{
con=db.getConnection();
ps=con.prepareStatement("update kjc_Department set DepartmentName=?,OfficeTel=?,Principal=?,CollegeID=? where DepartmentID=?");
ps.setString(1,di.getDepartmentName());
ps.setString(2,di.getOfficeTel());
System.out.println(di.getPrincipal());
ps.setInt(3,Integer.parseInt( di.getPrincipal()));
ps.setInt(4,di.getCollegeID());
ps.setInt(5,di.getDepartmentID());
ps.executeUpdate();
return "修改成功";
}//end block try
catch(Exception e)
{
e.printStackTrace();
return "修改失败";
}//end block catch
}//end block updateDepartment
public String deleteDepartment(int id)
{
try
{
con=db.getConnection();
ps=con.prepareStatement("delete kjc_Department where DepartmentID=?");
ps.setInt(1,id);
ps.executeUpdate();
return "删除成功";
}//end block try
catch(Exception e)
{
e.printStackTrace();
return "删除失败";
}//end block catch
}//end method deleteDepartment
public Collection selectDepartment()
{
Vector v=new Vector();
try
{
con=db.getConnection();
ps=con.prepareStatement("select * from kjc_Department");
rs=ps.executeQuery();
while(rs.next())
{
DepartmentItem di=new DepartmentItem();
di.setDepartmentID(rs.getInt("DepartmentID"));
di.setDepartmentName(rs.getString("DepartmentName"));
di.setOfficeTel(rs.getString("OfficeTel"));
v.add(di);
}
return v;
}//end block try
catch(Exception e)
{
e.printStackTrace();
return null;
}//end block catch
}//end method selectDepartment()
public DepartmentItem getDepartmentbyID(int id)
{
try
{
con=db.getConnection();
ps=con.prepareStatement("select * from kjc_Department where DepartmentID='"+id+"'");
rs=ps.executeQuery();
DepartmentItem di=new DepartmentItem();
while(rs.next())
{
di.setDepartmentID(rs.getInt("DepartmentID"));
di.setDepartmentName(rs.getString("DepartmentName"));
di.setOfficeTel(rs.getString("OfficeTel"));
di.setPrincipal(rs.getString("Principal"));
di.setCollegeID(rs.getInt("CollegeID"));
}
return di;
}//end block try
catch(Exception e)
{
e.printStackTrace();
return null;
}//end block catch
}//end method getDepartmentbyID
public DepartmentItem FindDepartment(int id)
{
try
{
con=db.getConnection();
ps=con.prepareStatement("select kjc_Department.DepartmentID,kjc_Department.DepartmentName,kjc_Department.OfficeTel,kjc_People.PeopleName,kjc_College.CollegeName from kjc_Department,kjc_People,kjc_College where kjc_Department.Principal=kjc_People.PeopleID and kjc_Department.DepartmentID='"+id+"'");
rs=ps.executeQuery();
DepartmentItem di=new DepartmentItem();
while(rs.next())
{
di.setDepartmentID(rs.getInt("DepartmentID"));
di.setDepartmentName(rs.getString("DepartmentName"));
di.setOfficeTel(rs.getString("OfficeTel"));
di.setPrincipal(rs.getString("PeopleName"));
}
return di;
}//end block try
catch(Exception e)
{
e.printStackTrace();
return null;
}//end block catch
}//end method FindDepartment(id)
public ResultSet getCollege()
{
try
{
con=db.getConnection();
ps=con.prepareStatement("select * from kjc_College");
rs=ps.executeQuery();
return rs;
}//end block try
catch(Exception e)
{
e.printStackTrace();
return null;
}//end block catch
}//end method getCollege
}//end class DepartmentBean
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -