📄 imaildaoimpl.java
字号:
package com.lovo.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.lovo.myutil.DBManager;
import com.lovo.po.Mail;
import com.lovo.po.MailVo;
public class IMailDaoImpl implements IMailDao
{
/* (非 Javadoc)
* @see com.lovo.dao.IMailDao#sendMail(com.lovo.po.Mail)
*/
public boolean sendMail(Mail mail)
{
/** 存数据库连接对象 */
Connection conn = null;
/** 存预编译语句 */
PreparedStatement state = null;
/** 存查询返回结果集 */
ResultSet rs = null;
String sql = "insert into t_mail(mailStatus, mailTitle, mailContent," +
" userId, whoSended, whoReceive) values(?, ?, ?, ?, ?, ?)";//添加邮件语句
/** 获取数据库连接对象 */
conn = DBManager.getConnection();
int result = -1;//存储添加邮件后的返回信息
try
{
/** 创建sql的预编译语句 */
state = conn.prepareStatement(sql);
/** 为sql设置邮件状态参数 */
state.setInt(1, mail.getMailStatus());
/** 为sql设置邮件标题参数 */
state.setString(2, mail.getMailTitle());
/** 为sql设置邮件内容参数 */
state.setString(3, mail.getMailContent());
/** 为sql设置邮件所属参数 */
state.setInt(4, mail.getUserId());
/** 为sql设置发件人参数 */
state.setInt(5, mail.getWhoSended());
/** 为sql设置收件人参数 */
state.setInt(6, mail.getWhoReceive());
/** 执行添加 */
result = state.executeUpdate();
}
catch (SQLException e)
{
e.printStackTrace();
}
finally
{
/** 关闭所有的数据库连接对象 */
DBManager.closeAll(conn, state, rs);
}
return result == 1 ? true : false;
}
/* (非 Javadoc)
* @see com.lovo.dao.IMailDao#queryMail(int, int)
*/
public List<MailVo> queryMail(int userId, int mailStatus)
{
/** 存数据库连接对象 */
Connection conn = null;
/** 存预编译语句 */
PreparedStatement state = null;
/** 存查询返回结果集 */
ResultSet rs = null;
List<MailVo> list = null;//存查询结果
String sql = "select mailId, mailTitle, mailContent, whoSended, " +
"u1.emailName as whoSendedName, whoReceive, " +
"u2.emailName as whoReceiveName " +
"from t_mail, t_user u1, t_user u2 " +
"where t_mail.whoSended = u1.userId " +
"and t_mail.whoReceive = u2.userId " +
"and mailStatus = ? " +
"and t_mail.userId = ?";//查询邮件语句
/** 获取数据库连接对象 */
conn = DBManager.getConnection();
try
{
/** 创建sql的预编译语句 */
state = conn.prepareStatement(sql);
/** 为sql设置邮件状态参数 */
state.setInt(1, mailStatus);
/** 为sql设置邮件所属参数 */
state.setInt(2, userId);
/** 执行查询 */
rs = state.executeQuery();
MailVo mailVo = null;//用于封装对象以便存入list
boolean flag = true;//如果有结果集,那么用它来作为创ArrayList的开关
/**
* 有结果集的话,循环封装成Mail对象并添加到list里
*/
while(rs.next())
{
/**
* 有结果集,那么在第一次的时候创建一个ArrayList对象
*/
if(flag)
{
/** 创建一个ArrayList对象用于存放Mail对象 */
list = new ArrayList<MailVo>();
/** 关闭开关,不再创建新的ArrayList,以免覆盖之前的 */
flag = false;
}
/** 创建一个Mail对象,用来封装当前邮件信息 */
mailVo = new MailVo();
/** 封装MailId */
mailVo.setMailId(rs.getInt("mailId"));
/** 封装MailStatus */
mailVo.setMailStatus(mailStatus);
/** 封装MailTitle */
mailVo.setMailTitle(rs.getString("mailTitle"));
/** 封装MailContent */
mailVo.setMailContent(rs.getString("mailContent"));
/** 封装UserId */
mailVo.setUserId(userId);
/** 封装WhoSended */
mailVo.setWhoSended(rs.getInt("whoSended"));
/** 封装WhoReceive */
mailVo.setWhoReceive(rs.getInt("whoReceive"));
/** 封装WhoSendedName */
mailVo.setWhoSendedName(rs.getString("whoSendedName"));
/** 封装WhoReceiveName */
mailVo.setWhoReceiveName(rs.getString("whoReceiveName"));
/** 将封装好所有信息的Mail对象添加到list */
list.add(mailVo);
}
}
catch (SQLException e)
{
e.printStackTrace();
}
finally
{
/** 关闭所有的数据库连接对象 */
DBManager.closeAll(conn, state, rs);
}
return list;
}
/* (非 Javadoc)
* @see com.lovo.dao.IMailDao#deleteMail(int[])
*/
public int deleteMail(int[] mailIds)
{
/** 存数据库连接对象 */
Connection conn = null;
/** 存预编译语句 */
PreparedStatement state = null;
/** 存查询返回结果集 */
ResultSet rs = null;
int result = 0;//存储删除邮件后的返回信息
String sql = "delete from t_mail where mailId = ?";//删除邮件语句
/** 获取数据库连接对象 */
conn = DBManager.getConnection();
try
{
/** 创建sql的预编译语句 */
state = conn.prepareStatement(sql);
for(int i = 0; i < mailIds.length; i++)
{
/** 为sql设置要删除的邮件的mailId */
state.setInt(1, mailIds[i]);
/** 执行删除 */
result += state.executeUpdate();
}
}
catch (SQLException e)
{
e.printStackTrace();
}
finally
{
/** 关闭所有的数据库连接对象 */
DBManager.closeAll(conn, state, rs);
}
return result;
}
/* (非 Javadoc)
* @see com.lovo.dao.IMailDao#updateMail(com.lovo.po.Mail)
*/
public boolean updateMail(Mail mail)
{
/** 存数据库连接对象 */
Connection conn = null;
/** 存预编译语句 */
PreparedStatement state = null;
/** 存查询返回结果集 */
ResultSet rs = null;
int result = -1;//存储更新邮件后的返回信息
String sql = "update t_mail set mailStatus = ?, mailTitle = ?, " +
"mailContent = ?, userId = ?, whoSended = ?, whoReceive = ? " +
"where mailId = ?";//更新邮件语句
/** 获取数据库连接对象 */
conn = DBManager.getConnection();
try
{
/** 创建sql的预编译语句 */
state = conn.prepareStatement(sql);
/** 设置参数 */
state.setInt(1, mail.getMailStatus());
state.setString(2, mail.getMailTitle());
state.setString(3, mail.getMailContent());
state.setInt(4, mail.getUserId());
state.setInt(5, mail.getWhoSended());
state.setInt(6, mail.getWhoReceive());
state.setInt(7, mail.getMailId());
/** 执行更新 */
result = state.executeUpdate();
}
catch (SQLException e)
{
e.printStackTrace();
}
finally
{
/** 关闭所有的数据库连接对象 */
DBManager.closeAll(conn, state, rs);
}
return result == 1 ? true : false;
}
public int moveToDeleted(int[] mailIds)
{
/** 存数据库连接对象 */
Connection conn = null;
/** 存预编译语句 */
PreparedStatement state = null;
/** 存查询返回结果集 */
ResultSet rs = null;
int result = 0;//存储更新邮件后的返回信息
String sql = "update t_mail set mailStatus = 4 where mailId = ?";//更新邮件状态语句
/** 获取数据库连接对象 */
conn = DBManager.getConnection();
try
{
/** 创建sql的预编译语句 */
state = conn.prepareStatement(sql);
for(int i = 0; i < mailIds.length; i++)
{
/** 为sql设置要更新的邮件的mailId */
state.setInt(1, mailIds[i]);
/** 执行删除 */
result += state.executeUpdate();
}
}
catch (SQLException e)
{
e.printStackTrace();
}
finally
{
/** 关闭所有的数据库连接对象 */
DBManager.closeAll(conn, state, rs);
}
return result;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -