📄 deptinfodao.java
字号:
package com.soft.deptmgr.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import com.soft.pagecut.PageableResultSet;
import com.soft.pagecut.pageable;
import com.soft.util.DBConn;
import com.soft.vo.DepartmentInfo;
public class DeptInfoDAO {
private DBConn tj = new DBConn();
private Connection conn = null;
private Statement st = null;
private PreparedStatement ps = null;
private ResultSet rs = null;
private pageable res=null;
int PageCount;
int rowsCount;
int PageRowsCount;
int PageSize;
int CurPage;
public int getPageCount() {
return PageCount;
}
public int getPageRowsCount() {
return PageRowsCount;
}
public int getPageSize() {
return PageSize;
}
public void setPageSize(int pageSize) {
PageSize = pageSize;
}
public int getCurPage() {
return CurPage;
}
public int getRowsCount() {
return rowsCount;
}
public void setCurPage(int curPage) {
CurPage = curPage;
}
public List getAllDepts()
{
List<DepartmentInfo> lt = new ArrayList<DepartmentInfo>();
try {
conn=tj.getConnection();
conn.setAutoCommit(false);
st=conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
res=new PageableResultSet(st.executeQuery("select * from department"));//构造一个Pageable
res.setPageSize(5);//每页5个记录
System.out.println("pagetcount:"+res.getPageCount()+"rowscount:"+res.getRowsCount()+"pagesize:"+res.getPageSize());
res.gotoPage(getCurPage());//跳转到第2页
PageCount=res.getPageCount();
rowsCount=res.getRowsCount();
PageRowsCount=res.getPageRowsCount();
System.out.println("current page:"+res.getCurPage());
for(int i=0; i<res.getPageRowsCount(); i++)
{
DepartmentInfo dept = new DepartmentInfo();
int departmentid=res.getInt("departmentid");
String departmentname=res.getString("departmentname");
dept.setDepartmentid(departmentid);
dept.setDepartmentname(departmentname);
lt.add(dept);
res.next();
}
System.out.println(lt.size());
conn.commit();
} catch (SQLException e) {
try {
if(conn!=null)
{
conn.rollback();//事务回滚
}
} catch (SQLException e1) {
e1.printStackTrace();
}
e.printStackTrace();
}
finally
{
try {
if(conn!=null)
{
conn.close();
}
if(ps!=null)
{
ps.close();
}
if(rs!=null)
{
rs.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
return lt;
}
public int addNewDept(int departmentid,String departmentname,int leader,String officephone)
{
int i = 0;
int numberofpeople=0;
try {
conn = tj.getConnection();
conn.setAutoCommit(false);
System.out.println("insert into department values("+departmentid+",'"+departmentname+"',"+leader+","+numberofpeople+",'"+officephone+"')");
ps = conn.prepareStatement("insert into department values("+departmentid+",'"+departmentname+"',"+leader+","+numberofpeople+",'"+officephone+"')");
i = ps.executeUpdate();
conn.commit();
} catch (SQLException e) {
try {
if(conn!=null)
{
conn.rollback();//事务回滚
}
} catch (SQLException e1) {
e1.printStackTrace();
}
e.printStackTrace();
}
finally
{
try {
if(conn!=null)
{
conn.close();
}
if(ps!=null)
{
ps.close();
}
if(rs!=null)
{
rs.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
return i;
}
public int updateDept(int departmentid,String departmentname,int leader,String officephone )
{
int i = 0;
try {
conn = tj.getConnection();
conn.setAutoCommit(false);
ps = conn.prepareStatement("update department set departmentname=?,leader=? ,officephone=?where departmentid=?");
ps.setString(1,departmentname);
ps.setInt(2,leader);
ps.setString(3,officephone);
ps.setInt(4,departmentid);
i = ps.executeUpdate();
conn.commit();
} catch (SQLException e) {
try {
if(conn!=null)
{
conn.rollback();//事务回滚
}
} catch (SQLException e1) {
e1.printStackTrace();
}
e.printStackTrace();
}
finally
{
try {
if(conn!=null)
{
conn.close();
}
if(ps!=null)
{
ps.close();
}
if(rs!=null)
{
rs.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
return i;
}
public int delDept(int departmentid)
{
int i = 0;
try {
conn = tj.getConnection();
conn.setAutoCommit(false);
ps = conn.prepareStatement("delete from department where departmentid=?");
ps.setInt(1,departmentid);
i = ps.executeUpdate();
conn.commit();
} catch (SQLException e) {
try {
if(conn!=null)
{
conn.rollback();//事务回滚
}
} catch (SQLException e1) {
e1.printStackTrace();
}
e.printStackTrace();
}
finally
{
try {
if(conn!=null)
{
conn.close();
}
if(ps!=null)
{
ps.close();
}
if(rs!=null)
{
rs.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
return i;
}
public List searchDept(int departmentid)
{
List<DepartmentInfo> lt = new ArrayList<DepartmentInfo>();
try {
conn=tj.getConnection();
conn.setAutoCommit(false);
String sql = "select * from department where departmentid=?";
ps = conn.prepareStatement(sql);
ps.setInt(1,departmentid);
/*if(userName!=null&&!userName.equals(""))
{
sql += "and u.username like ?";
}
if(userNo!=null&&!userNo.equals(""))
{
sql +=" and u.userno=?";
}
ps = conn.prepareStatement(sql);
if(userName!=null&&!userName.equals(""))
{
ps.setString(1,"%"+userName+"%");
if(userNo!=null&&!userNo.equals(""))
{
ps.setInt(2,Integer.parseInt(userNo));
}
}
else
{
if(userNo!=null&&!userNo.equals(""))
{
ps.setInt(1,Integer.parseInt(userNo));
}
}*/
rs = ps.executeQuery();
while(rs.next())
{
DepartmentInfo dif = new DepartmentInfo();
int departmentid1=rs.getInt("departmentid");
String departmentname=rs.getString("departmentname");
int leader=rs.getInt("leader");
String officephone=rs.getString("officephone");
dif.setDepartmentid(departmentid1);
dif.setDepartmentname(departmentname);
dif.setLeader(leader);
dif.setOfficephone(officephone);
lt.add(dif);
}
System.out.println(lt.size());
conn.commit();
} catch (SQLException e) {
try {
if(conn!=null)
{
conn.rollback();//事务回滚
}
} catch (SQLException e1) {
e1.printStackTrace();
}
e.printStackTrace();
}
finally
{
try {
if(conn!=null)
{
conn.close();
}
if(ps!=null)
{
ps.close();
}
if(rs!=null)
{
rs.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
return lt;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -