📄 icontactpersondaoimpl.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.ContactPerson;
import com.lovo.po.ContactPersonVo;
public class IContactPersonDaoImpl implements IContactPersonDao
{
/* (非 Javadoc)
* @see com.lovo.dao.IContactPersonDao#addContactPerson(java.lang.String, int)
*/
public boolean addContactPerson(ContactPerson contactPerson)
{
/** 存数据库连接对象 */
Connection conn = null;
/** 存预编译语句 */
PreparedStatement state = null;
/** 存查询返回结果集 */
ResultSet rs = null;
String sql = "insert into t_contactperson(nickname, contactId, userId) " +
"values(?, ?, ?)";//添加联系人Sql语句
/** 获取数据库连接对象 */
conn = DBManager.getConnection();
int result = 0;//存储添加联系人后的返回信息
try
{
/** 创建sql的预编译语句 */
state = conn.prepareStatement(sql);
/** 为sql设置联系人昵称参数 */
state.setString(1, contactPerson.getNickname());
/** 为sql设置联系人帐号id参数 */
state.setInt(2, contactPerson.getContactId());
/** 为sql设置联系人表所属用户id参数 */
state.setInt(3, contactPerson.getUserId());
/** 执行添加 */
result = state.executeUpdate();
}
catch (SQLException e)
{
e.printStackTrace();
}
finally
{
/** 关闭所有的数据库连接对象 */
DBManager.closeAll(conn, state, rs);
}
return result == 1 ? true : false;
}
/* (非 Javadoc)
* @see com.lovo.dao.IContactPersonDao#deleteContactPerson(int, int)
*/
public boolean deleteContactPerson(int contactId, int userId)
{
/** 存数据库连接对象 */
Connection conn = null;
/** 存预编译语句 */
PreparedStatement state = null;
/** 存查询返回结果集 */
ResultSet rs = null;
String sql = "delete from t_contactPerson where userId = ? " +
"and contactId = ?";//删除联系人sql语句
/** 获取数据库连接对象 */
conn = DBManager.getConnection();
int result = 0;//存储删除联系人后的返回信息
try
{
/** 创建sql的预编译语句 */
state = conn.prepareStatement(sql);
/** 为sql设置联系人所属的帐号id参数 */
state.setInt(1, userId);
/** 为sql设置联系人帐号id参数 */
state.setInt(2, contactId);
/** 执行删除 */
result = state.executeUpdate();
}
catch (SQLException e)
{
e.printStackTrace();
}
finally
{
/** 关闭所有的数据库连接对象 */
DBManager.closeAll(conn, state, rs);
}
return result == 1 ? true : false;
}
/* (非 Javadoc)
* @see com.lovo.dao.IContactPersonDao#getContactPersonEmailName(int)
*/
public List<String> getContactPersonEmailName(int userId)
{
/** 存数据库连接对象 */
Connection conn = null;
/** 存预编译语句 */
PreparedStatement state = null;
/** 存查询返回结果集 */
ResultSet rs = null;
String sql = "select emailName from t_contactPerson, t_user " +
"where t_contactPerson.contactId = t_user.userId " +
"and t_contactPerson.userId = ?";//查询联系人帐号的sql语句
/** 获取数据库连接对象 */
conn = DBManager.getConnection();
List<String> emailNames = new ArrayList<String>();//存查到的所有联系人帐号名
try
{
/** 创建sql的预编译语句 */
state = conn.prepareStatement(sql);
/** 为sql设置联系人所属的帐号id参数 */
state.setInt(1, userId);
/** 执行查询 */
rs = state.executeQuery();
/** 将查询结果添加到List */
while(rs.next())
{
emailNames.add(rs.getString("emailName"));
}
}
catch (SQLException e)
{
e.printStackTrace();
}
finally
{
/** 关闭所有的数据库连接对象 */
DBManager.closeAll(conn, state, rs);
}
return emailNames;
}
/* (非 Javadoc)
* @see com.lovo.dao.IContactPersonDao#isContactPersonIdExist(int)
*/
public boolean isContactPersonIdExist(int contactId, int userId)
{
/** 存数据库连接对象 */
Connection conn = null;
/** 存预编译语句 */
PreparedStatement state = null;
/** 存查询返回结果集 */
ResultSet rs = null;
String sql = "select autoId from t_contactPerson " +
"where userId = ? and contactId = ?";//查询当前用户的当前联系人的数量sql语句
int count = 0;//存当前联系人的数量
/** 获取数据库连接对象 */
conn = DBManager.getConnection();
try
{
/** 创建sql的预编译语句 */
state = conn.prepareStatement(sql);
/** 为sql设置联系人所属的帐号id参数 */
state.setInt(1, userId);
/** 为sql设置联系人id参数 */
state.setInt(2, contactId);
/** 执行查询 */
rs = state.executeQuery();
while(rs.next())
{
count++;
}
}
catch (SQLException e)
{
e.printStackTrace();
}
finally
{
/** 关闭所有的数据库连接对象 */
DBManager.closeAll(conn, state, rs);
}
return count == 0 ? false : true;
}
public List<ContactPersonVo> listContactPerson(int userId)
{
/** 存数据库连接对象 */
Connection conn = null;
/** 存预编译语句 */
PreparedStatement state = null;
/** 存查询返回结果集 */
ResultSet rs = null;
String sql = "select autoId, nickname, contactId, emailName " +
"from t_contactPerson, t_user " +
"where t_contactPerson.contactId = t_user.userId " +
"and t_contactPerson.userId = ?";//查询联系人所有信息sql语句
/** 获取数据库连接对象 */
conn = DBManager.getConnection();
List<ContactPersonVo> vo = new ArrayList<ContactPersonVo>();//存查到的所有联系人vo
try
{
/** 创建sql的预编译语句 */
state = conn.prepareStatement(sql);
/** 为sql设置联系人所属的帐号id参数 */
state.setInt(1, userId);
/** 执行查询 */
rs = state.executeQuery();
/** 将查询结果添加到List */
ContactPersonVo contactPersonVo = null;
while(rs.next())
{
contactPersonVo = new ContactPersonVo();
contactPersonVo.setAutoId(rs.getInt("autoId"));
contactPersonVo.setContactId(rs.getInt("contactId"));
contactPersonVo.setEmailName(rs.getString("emailName"));
contactPersonVo.setNickname(rs.getString("nickname"));
contactPersonVo.setUserId(userId);
vo.add(contactPersonVo);
}
}
catch (SQLException e)
{
e.printStackTrace();
}
finally
{
/** 关闭所有的数据库连接对象 */
DBManager.closeAll(conn, state, rs);
}
return vo;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -