📄 deptbean.java
字号:
package com.xdf.supermarket.service;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import com.xdf.supermarket.db.DBConnection;
import com.xdf.supermarket.dto.DeptDTO;
import com.xdf.supermarket.util.Constant;
import com.xdf.supermarket.util.Tools;
public class DeptBean extends BaseBean{
/**
* 得到所有的部门信息
*/
public ArrayList getAllDept(){
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
ArrayList list = new ArrayList();
try {
conn = DBConnection.getConnection();
stmt = conn.createStatement();
String sql = "select * from table_dept order by dept_id";
rs = stmt.executeQuery(sql);
while (rs.next()){
DeptDTO v = new DeptDTO();
v.setDept_id(rs.getString("dept_id"));
v.setDept_name(rs.getString("dept_name"));
v.setDept_descreption(rs.getString("dept_descreption"));
list.add(v);
}
} catch (Exception e) {
e.printStackTrace();
}finally{
close(rs);
close(stmt);
close(conn);
}
return list;
}
/**
* 增加部门
*/
public boolean addDept(DeptDTO dd){
Connection conn = null;
Statement stmt = null;
boolean flag = false;
try {
conn = DBConnection.getConnection();
stmt = conn.createStatement();
String sql = "insert into table_dept values(seq_dept_id.nextval,'"+
Tools.dan(dd.getDept_name())+"','"+Tools.dan(dd.getDept_descreption())+"')";
int t = stmt.executeUpdate(sql);
if (t==1)
flag = true;
} catch (Exception e) {
e.printStackTrace();
}finally{
close(stmt);
close(conn);
}
return flag;
}
/**
* 修改部门
*/
public boolean updateDept(DeptDTO dd){
Connection conn = null;
Statement stmt = null;
boolean flag = false;
try {
conn = DBConnection.getConnection();
stmt = conn.createStatement();
String sql = "update table_dept set dept_name='"+
Tools.dan(dd.getDept_name())+"',dept_descreption='"+Tools.dan(dd.getDept_descreption())+"' " +
"where dept_id="+dd.getDept_id();
int t = stmt.executeUpdate(sql);
if (t==1)
flag = true;
} catch (Exception e) {
e.printStackTrace();
}finally{
close(stmt);
close(conn);
}
return flag;
}
/**
* 得到总页数
*/
public int getDeptTotalPage(){
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
int total = 0;
try {
conn = DBConnection.getConnection();
//设置结果集可以滚动,不受其他更新影响,只读
stmt = conn.createStatement();
rs = stmt.executeQuery("select count(*) c from table_dept");
rs.next();
int num = rs.getInt("c");
total = (num+Constant.DEPT_PAGE_SIZE-1)/Constant.DEPT_PAGE_SIZE;
} catch (Exception e) {
e.printStackTrace();
}finally{
close(rs);
close(stmt);
close(conn);
}
return total;
}
/**
* 得到一页部门
*/
public ArrayList getOnePageDept(int pageno){
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
ArrayList list = new ArrayList();
try {
conn = DBConnection.getConnection();
stmt = conn.createStatement();
int pagesize = Constant.DEPT_PAGE_SIZE;
int first = (pageno-1)*pagesize+1;
int last = pageno*pagesize;
String sql =
"select * from " +
"(select s.*,rownum r from " +
"(select * from table_dept order by dept_id) s " +
"where rownum<="+last+") " +
"where r>="+first;
rs = stmt.executeQuery(sql);
while (rs.next()){
DeptDTO v = new DeptDTO();
v.setDept_id(rs.getString("dept_id"));
v.setDept_name(rs.getString("dept_name"));
v.setDept_descreption(rs.getString("dept_descreption"));
list.add(v);
}
} catch (Exception e) {
e.printStackTrace();
}finally{
close(rs);
close(stmt);
close(conn);
}
return list;
}
/**
* 根据id得到一个部门
*/
public DeptDTO getOneDept(String dept_id){
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
DeptDTO v = new DeptDTO();
try {
conn = DBConnection.getConnection();
stmt = conn.createStatement();
String sql = "select * from table_dept where dept_id="+dept_id;
rs = stmt.executeQuery(sql);
if (rs.next()){
v.setDept_id(rs.getString("dept_id"));
v.setDept_name(rs.getString("dept_name"));
v.setDept_descreption(rs.getString("dept_descreption"));
}
} catch (Exception e) {
e.printStackTrace();
}finally{
close(rs);
close(stmt);
close(conn);
}
return v;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -