📄 deptdao.java
字号:
package com.j1132.dao.dept;
import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import com.j1132.bean.DeptBean;
import com.j1132.bean.IncomeBean;
import com.j1132.dao.AbstractDao;
public class DeptDao extends AbstractDao implements IdeptDao {
public List<DeptBean> dept() {
List<DeptBean> list = new ArrayList<DeptBean>();
Connection conn = getConnection();
String sql = "select d.*from dept d ";
Statement stm = null;
ResultSet rs = null;
try {
stm = conn.createStatement();
rs = stm.executeQuery(sql);
while (rs.next()) {
DeptBean db = new DeptBean();
long dept_id = rs.getLong(1);
String dept_name = rs.getString(2);
String dept_descreption = rs.getString(3);
db.setDept_id(dept_id);
db.setDept_name(dept_name);
db.setDept_descreption(dept_descreption);
list.add(db);
}
} catch (Exception e) {
System.out.println("数据库查询有误");
e.printStackTrace();
} finally {
close(rs, stm, conn);
}
return list;
}
public boolean adddept(String dept_name,String dept_description) {
boolean b = true;
String sql = "insert into dept(dept_id,dept_name,dept_descreption) values (?,?,?)";
Connection conn = getConnection();
PreparedStatement ps = null;
try{
ps = conn.prepareStatement(sql);
ps.setLong(1,System.currentTimeMillis());
ps.setString(2,dept_name);
ps.setString(3,dept_description);
ps.executeUpdate();
}catch(Exception e){
b = false;
System.out.println("添加部门出错");
e.printStackTrace();
}
return b ;
}
public DeptBean finddept(long dept_id) {
DeptBean db = new DeptBean();
Connection conn = getConnection();
String sql = "select * from dept d where d.dept_id = ?";
PreparedStatement ps = null;
ResultSet rs = null;
try{
ps = conn.prepareStatement(sql);
ps.setLong(1,dept_id);
rs = ps.executeQuery();
while(rs.next()){
String dept_name = rs.getString(2);
String description = rs.getString(3);
db.setDept_id(dept_id);
db.setDept_name(dept_name);
db.setDept_descreption(description);
}
}catch(Exception e){
System.out.println("查询部门表出错");
e.printStackTrace();
}
return db ;
}
public boolean updatedept(DeptBean db) {
boolean b = true;
Connection conn = getConnection();
String sql = "update dept SET DEPT_NAME=?,dept_descreption = ? WHERE dept_ID = ?";
PreparedStatement ps = null;
long dept_id = db.getDept_id();
String dept_name = db.getDept_name();
String dept_description = db.getDept_descreption();
try {
ps = conn.prepareStatement(sql);
ps.setString(1,dept_name);
ps.setString(2,dept_description);
ps.setLong(3,dept_id);
ps.executeUpdate();
} catch (Exception e) {
b = false;
System.out.println("更新数据有误");
e.printStackTrace();
} finally {
close(null, ps, conn);
}
return b;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -