📄 empdao.java
字号:
package com.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;
import com.bean.DepBean;
import com.bean.EmpBean;
import com.bean.EmpPopedomBean;
import com.bean.PopedomBean;
import com.db.DBConnection;
import com.struts.form.EmpForm;
import com.struts.form.EmpPopedomForm;
import com.struts.form.PopedomForm;
public class EmpDao implements IDao {
public boolean delete(String id) {
DBConnection dbconn = DBConnection.getDBConnection();
Connection conn = dbconn.getConn();
String sql = "delete from emp where id=?";
PreparedStatement ps = null;
EmpPopedomDao epdao = new EmpPopedomDao();
//查询该员工是否有权限,有则删除
Vector vc=epdao.findByEid(id);
if(vc!=null)
{
epdao.delete(id);
}
try {
ps = conn.prepareStatement(sql);
ps.setString(1, id);
int row = ps.executeUpdate();
if (row > 0) {
return true;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
if (ps != null) {
ps.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return false;
}
public Object findById(String id) {
DBConnection dbconn = DBConnection.getDBConnection();
Connection conn = dbconn.getConn();
String sql = "select * from vwemp where id=?";
PreparedStatement ps = null;
ResultSet rst = null;
EmpPopedomDao epd = new EmpPopedomDao();
try {
ps = conn.prepareStatement(sql);
ps.setString(1, id);
rst = ps.executeQuery();
if (rst.next()) {
//EmpBean eb = new EmpBean();
EmpForm eb=new EmpForm();
eb.setId(rst.getString(1));
eb.setName(rst.getString(2));
eb.setPass(rst.getString(3));
eb.setSex(rst.getString(4));
eb.setAge(rst.getString(5));
eb.setDepid(rst.getString(6));
eb.setSchool(rst.getString(7));
eb.setResume(rst.getString(8));
eb.setPicture(rst.getString(9));
eb.setDepname(rst.getString(10));
// 查此员工的权限
Vector vcp = epd.findByEid(eb.getId());
if (vcp != null) {
//设置emppopedom表中pid
String[] pid=new String[vcp.size()];
for (int i = 0; i < pid.length; i++) {
//PopedomBean pb=(PopedomBean) vcp.get(i);
PopedomForm pb=(PopedomForm) vcp.get(i);
pid[i]=pb.getId();
}
eb.setPopedomid(pid);
eb.setPopedoms(vcp);
}
return eb;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
if (rst != null) {
rst.close();
}
if (ps != null) {
ps.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return null;
}
public boolean save(Object ob) {
DBConnection dbconn = DBConnection.getDBConnection();
Connection conn = dbconn.getConn();
String sql = "insert emp values(?,?,?,?,?,?,?,?)";
//EmpBean eb = (EmpBean) ob;
EmpForm eb=(EmpForm) ob;
PreparedStatement ps = null;
EmpPopedomDao epdao = new EmpPopedomDao();
try {
ps = conn.prepareStatement(sql);
ps.setString(1, eb.getName());
ps.setString(2, eb.getPass());
ps.setString(3, eb.getSex());
ps.setString(4, eb.getAge());
ps.setString(5, eb.getDepid());
ps.setString(6, eb.getSchool());
ps.setString(7, eb.getResume());
ps.setString(8, eb.getPicture());
int row = ps.executeUpdate();
if (row > 0) {
// 找出最大员工编号
String sql1 = "select max(id) from emp";
ps = conn.prepareStatement(sql1);
ResultSet rst = ps.executeQuery();
String[] popedoms = eb.getPopedomid();
if (rst.next()) {
int eid = rst.getInt(1);
if (popedoms != null) {
for (int i = 0; i < popedoms.length; i++) {
//EmpPopedomBean epb = new EmpPopedomBean();
EmpPopedomForm epb=new EmpPopedomForm();
epb.setEid(String.valueOf(eid));
epb.setPid(popedoms[i]);
epdao.save(epb);
}
}
}
return true;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
if (ps != null) {
ps.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return false;
}
public boolean update(Object ob) {
DBConnection dbconn = DBConnection.getDBConnection();
Connection conn = dbconn.getConn();
String sql = "update emp set name=?,pass=?,sex=?,age=?,depid=?,school=?,resume=?,picture=? where id=?";
//EmpBean eb = (EmpBean) ob;
EmpForm eb=(EmpForm) ob;
PreparedStatement ps = null;
EmpPopedomDao epdao = new EmpPopedomDao();
//查询该员工是否有权限,有则删除
Vector vc=epdao.findByEid(eb.getId());
if(vc!=null)
{
epdao.delete(eb.getId());
}
try {
ps = conn.prepareStatement(sql);
ps.setString(1, eb.getName());
ps.setString(2, eb.getPass());
ps.setString(3, eb.getSex());
ps.setString(4, eb.getAge());
ps.setString(5, eb.getDepid());
ps.setString(6, eb.getSchool());
ps.setString(7, eb.getResume());
ps.setString(8, eb.getPicture());
ps.setString(9, eb.getId());
int row = ps.executeUpdate();
if (row > 0) {
String[] popedoms = eb.getPopedomid();
if (popedoms != null) {
for (int i = 0; i < popedoms.length; i++) {
//EmpPopedomBean epb = new EmpPopedomBean();
EmpPopedomForm epb=new EmpPopedomForm();
epb.setEid(eb.getId());
epb.setPid(popedoms[i]);
epdao.save(epb);
}
}
return true;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
if (ps != null) {
ps.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return false;
}
public Vector findAll() {
DBConnection dbconn = DBConnection.getDBConnection();
Connection conn = dbconn.getConn();
String sql = "select * from vwemp";
PreparedStatement ps = null;
ResultSet rst = null;
Vector vc = new Vector();
EmpPopedomDao epd = new EmpPopedomDao();
try {
ps = conn.prepareStatement(sql);
rst = ps.executeQuery();
while (rst.next()) {
EmpBean eb = new EmpBean();
eb.setId(rst.getString(1));
eb.setName(rst.getString(2));
eb.setPass(rst.getString(3));
eb.setSex(rst.getString(4));
eb.setAge(rst.getString(5));
eb.setDepid(rst.getString(6));
eb.setSchool(rst.getString(7));
eb.setResume(rst.getString(8));
eb.setPicture(rst.getString(9));
eb.setDepname(rst.getString(10));
// 查此员工的权限
Vector vcp = epd.findByEid(eb.getId());
if (vcp != null) {
eb.setPopedoms(vcp);
}
vc.add(eb);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
if (rst != null) {
rst.close();
}
if (ps != null) {
ps.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return vc;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -