📄 outboxdao.java
字号:
package com.oa.lp.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import com.oa.lp.model.DraftBox;
import com.oa.lp.model.OutBox;
import com.oa.lp.model.OutBoxAnnex;
import com.oa.lp.util.DTOPopulator;
import com.oa.lp.util.PageList;
import com.oa.lp.util.Pages;
public class OutboxDAO {
private Connection conn;
public Connection getConn() {
return conn;
}
public void setConn(Connection conn) {
this.conn = conn;
}
//添加发邮件,和附件
public void addOutbox(OutBox outbox,List annexList) throws SQLException{
int outboxId=0;
//添加发邮件
String sql = "insert into OUTBOX(EMP_ID,SEND_PERSON_ID,SEND_PERSON_NAME,ACCEPT_PERSON_ID,ACCEPT_PERSON_NAME,TITLE,CONTENT,SEND_TIME)" +
"values(?,?,?,?,?,?,?,getDate()) select SCOPE_IDENTITY()";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setInt(1,outbox.getEmpId());
pstmt.setString(2,outbox.getSendPersonId());
pstmt.setString(3,outbox.getSendPersonName());
pstmt.setString(4,outbox.getAcceptPersonId());
pstmt.setString(5,outbox.getAcceptPersonName());
pstmt.setString(6,outbox.getTitle());
pstmt.setString(7,outbox.getContent());
ResultSet rs = pstmt.executeQuery();
if(rs.next()){
outboxId = rs.getInt(1);
}
rs.close();
pstmt.close();
//添加附件
if(annexList!=null){
sql = "insert into OUTBOX_ANNEX(OUT_ID,NAME,PATH)values(?,?,?)";
pstmt = conn.prepareStatement(sql);
for(int i=0;i<annexList.size();i++){
OutBoxAnnex annex=(OutBoxAnnex)annexList.get(i);
pstmt.setInt(1,outboxId);
pstmt.setString(2,annex.getName());
pstmt.setString(3,annex.getPath());
pstmt.executeUpdate();
}
}
rs.close();
pstmt.close();
}
/*
* 删除
*/
public void delOutBox(int outId) throws SQLException{
//先删除附件
String sql = "delete from OUTBOX_ANNEX where OUT_ID=?";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setInt(1,outId);
pstmt.executeUpdate();
pstmt.close();
//再删除发件
sql = "delete from OUTBOX where OUT_ID=?";
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1,outId);
pstmt.executeUpdate();
pstmt.close();
}
/*
* 通过empId 返回发件列表
*/
public PageList listAllOutBox(Pages page,int empId) throws Exception{
PageList pageList = null;
//总录数
page.setAllRecord(countDraft());
page.doPage();
StringBuffer sql = new StringBuffer();
sql.append("select * from (");
sql.append("select top "+page.getPageSize()+" * from (");
sql.append("select top "+(page.getPageSize()*page.getCPage())+" * from ");
sql.append("OUTBOX order by OUT_ID desc) t1 order by OUT_ID asc) t2 order by OUT_ID desc");
sql.append("where EMP_ID=?");
PreparedStatement pstmt = conn.prepareStatement(sql.toString());
pstmt.setInt(1,empId);
ResultSet rs = pstmt.executeQuery();
//将结果集中的每一行记录封装成一个对象,再放进集合返回
List list = DTOPopulator.populate(rs, OutBox.class);
pageList.setPage(page);
pageList.setObjectList(list);
pstmt.close();
return pageList;
}
/*
* 通过inBoxId返回发件
*/
public OutBox getById(int outId) throws Exception{
OutBox outbox =null;
String sql = "select * from OUTBOX where OUT_ID=?";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setInt(1,outId);
ResultSet rs = pstmt.executeQuery();
//将结果集中的每一行记录封装成一个对象,再放进集合返回
List list = DTOPopulator.populate(rs, OutBox.class);
//取集合中的第一个元素返回
if(list.size()>0){
outbox=(OutBox)list.get(0);
}
rs.close();
pstmt.close();
return outbox;
}
/*
* 通过outId返回发件箱附件列表
*/
public List getAnnexById(int outId) throws Exception{
List list = null;
String sql = "select * from OUTBOX_ANNEX where OUT_ID=?";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setInt(1,outId);
ResultSet rs = pstmt.executeQuery();
list=DTOPopulator.populate(rs, OutBoxAnnex.class);
rs.close();
pstmt.close();
return list;
}
/**
* 收件总记录数
* @return
* @throws SQLException
*/
public int countDraft() throws SQLException{
int count = 0;
String sql = "select count(*) from INBOX";
PreparedStatement pstmt = conn.prepareStatement(sql);
ResultSet rs = pstmt.executeQuery();
if(rs.next()){
count = rs.getInt(1);
}
pstmt.close();
return count;
}
/*
* 通过empId 返回发件列表,不分页
*/
public List listAllOutBox(int empId) throws Exception{
List list=null;
String sql="select *from OUTBOX where EMP_ID=?";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setInt(1,empId);
ResultSet rs = pstmt.executeQuery();
//将结果集中的每一行记录封装成一个对象,再放进集合返回
list=DTOPopulator.populate(rs, OutBox.class);
rs.close();
pstmt.close();
return list;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -