package com.zhiyou100.dao.impl;
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.sun.crypto.provider.RSACipher;
import com.zhiyou100.dao.NoticeDao;
import com.zhiyou100.entity.NoticeDo;
import com.zhiyou100.util.DBUtil;
import com.zhiyou100.util.NoticeSearchTypeName;
public class NoticeDaoImpl implements NoticeDao {
@Override
public List<NoticeDo> listNotice(long pageIndex) {
List<NoticeDo> list = new ArrayList<NoticeDo>();
try (Connection connection = DBUtil.INSTANCE.getConnection();) {
String sql = "SELECT n.id,u.name,n.title,d.name,n.content,n.begin_time,n.end_time FROM notice as n,user as u,department as d "
+ "WHERE n.is_delete =0 AND n.user_id =u.id AND n.department_id=d.id LIMIT ?,5";
try (PreparedStatement prepareStatement = connection.prepareStatement(sql);) {
prepareStatement.setLong(1, pageIndex);
try (ResultSet executeQuery = prepareStatement.executeQuery();) {
while (executeQuery.next()) {
NoticeDo noticeDo = new NoticeDo();
noticeDo.setId(executeQuery.getLong(1));
noticeDo.setUserName(executeQuery.getString(2));
noticeDo.setContent(executeQuery.getString(3));
noticeDo.setDepartmentName(executeQuery.getString(4));
noticeDo.setContent(executeQuery.getString(5));
noticeDo.setBeginTime(executeQuery.getDate(6));
noticeDo.setEndTime(executeQuery.getDate(7));
list.add(noticeDo);
}
}
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return list;
}
@Override
public long countNotice() {
long count = 0;
try (Connection connection = DBUtil.INSTANCE.getConnection();) {
String sql = "SELECT COUNT(*) FROM notice as n, user as u, department as d WHERE n.is_delete = 0 AND n.user_id = u.id AND n.department_id = d.id;";
try (PreparedStatement prepareStatement = connection.prepareStatement(sql);) {
try (ResultSet executeQuery = prepareStatement.executeQuery();) {
while (executeQuery.next()) {
count = executeQuery.getInt(1);
}
}
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return count;
}
@Override
public List<NoticeDo> listNotice(String keyword, NoticeSearchTypeName type, long pageIndex) {
ArrayList<NoticeDo> list = new ArrayList<NoticeDo>();
try (Connection connection = DBUtil.INSTANCE.getConnection();) {
String sql = null;
switch (type) {
case NOTICE_TITLE:
sql = "SELECT n.id, u.name, n.title, d.name, n.content, n.begin_time, n.end_time FROM notice as n, user as u, department as d WHERE n.is_delete = 0 AND n.title like ? AND n.user_id = u.id AND n.department_id = d.id LIMIT ?, 5;";
break;
case NOTICE_CONTENT:
sql = "SELECT n.id, u.name, n.title, d.name, n.content, n.begin_time, n.end_time FROM notice as n, user as u, department as d WHERE n.is_delete = 0 AND n.content like ? AND n.user_id = u.id AND n.department_id = d.id LIMIT ?, 5;";
break;
case USER_NAME:
sql = "SELECT n.id, u.name, n.title, d.name, n.content, n.begin_time, n.end_time FROM notice as n, user as u, department as d WHERE n.is_delete = 0 AND u.name like ? AND n.user_id = u.id AND n.department_id = d.id LIMIT ?, 5;";
break;
case DEPARTMENT_NAME:
sql = "SELECT n.id, u.name, n.title, d.name, n.content, n.begin_time, n.end_time FROM notice as n, user as u, department as d WHERE n.is_delete = 0 AND d.name like ? AND n.user_id = u.id AND n.department_id = d.id LIMIT ?, 5;";
break;
default:
break;
}
try (PreparedStatement preparedStatement = connection.prepareStatement(sql);) {
preparedStatement.setString(1, "%" + keyword + "%");
preparedStatement.setLong(2, pageIndex);
try (ResultSet executeQuery = preparedStatement.executeQuery();) {
while (executeQuery.next()) {
NoticeDo noticeDo = new NoticeDo();
noticeDo.setId(executeQuery.getLong(1));
noticeDo.setUserName(executeQuery.getString(2));
noticeDo.setTitle(executeQuery.getString(3));
noticeDo.setDepartmentName(executeQuery.getString(4));
noticeDo.setContent(executeQuery.getString(5));
noticeDo.setBeginTime(executeQuery.getDate(6));
noticeDo.setEndTime(executeQuery.getDate(7));
list.add(noticeDo);
}
}
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return list;
}
@Override
public long countNotice(String keyword, NoticeSearchTypeName type) {
long count = 0;
try (Connection connection = DBUtil.INSTANCE.getConnection();) {
String sql = "";
switch (type) {
case NOTICE_CONTENT:
sql = "SELECT COUNT(*) FROM notice as n, user as u, department as d WHERE n.is_delete = 0 AND n.title like ? AND n.user_id = u.id AND n.department_id = d.id;";
break;
case NOTICE_TITLE:
sql = "SELECT COUNT(*) FROM notice as n, user as u, department as d WHERE n.is_delete = 0 AND n.title like ? AND n.user_id = u.id AND n.department_id = d.id;";
break;
case USER_NAME:
sql = "SELECT COUNT(*) FROM notice as n, user as u, department as d WHERE n.is_delete = 0 AND n.title like ? AND n.user_id = u.id AND n.department_id = d.id;";
break;
case DEPARTMENT_NAME:
sql = "SELECT COUNT(*) FROM notice as n, user as u, department as d WHERE n.is_delete = 0 AND n.title like ? AND n.user_id = u.id AND n.department_id = d.id;";
break;
default:
break;
}
try (PreparedStatement prepareStatement = connection.prepareStatement(sql);) {
prepareStatement.setString(1, "%" + keyword + "%");
try (ResultSet executeQuery = prepareStatement.executeQuery();) {
while (executeQuery.next()) {
count = executeQuery.getLong(1);
}
}
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return count;
}
@Override
public NoticeDo getNoticeById(long id) {
NoticeDo noticeDo = null;
try (Connection connection = DBUtil.INSTANCE.getConnection();) {
String sql = "SELECT n.id, u.name, n.title, d.name, n.content, n.begin_time, n.end_time, d.id FROM notice as n, user as u, department as d WHERE n.is_delete = 0 AND n.id = ? AND n.user_id = u.id AND n.department_id = d.id;";
try (PreparedStatement prepareStatement = connection.prepareStatement(sql);) {
prepareStatement.setLong(1, id);
try (ResultSet executeQuery = prepareStatement.executeQuery();) {
while (executeQuery.next()) {
noticeDo = new NoticeDo();
noticeDo.setId(executeQuery.getLong(1));
noticeDo.setUserName(executeQuery.getString(2));
noticeDo.setTitle(executeQuery.getString(3));
noticeDo.setDepartmentName(executeQuery.getString(4));
noticeDo.setContent(executeQuery.getString(5));
noticeDo.setBeginTime(executeQuery.getDate(6));
noticeDo.setEndTime(executeQuery.getDate(7));
noticeDo.setDepartmentId(executeQuery.getLong(8));
}
}
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return noticeDo;
}
@Override
public void addNotice(NoticeDo notice) {
try (Connection connection = DBUtil.INSTANCE.getConnection();) {
String sql = "INSERT INTO notice(user_id, title, department_id, content, begin_time, end_time) VALUES (?, ?, ?, ?, ?, ?);";
try (PreparedStatement prepareStatement = connection.prepareStatement(sql);) {
prepareStatement.setLong(1, notice.getUserId());
prepareStatement.setString(2, notice.getTitle());
prepareStatement.setLong(3, notice.getDepartmentId());
prepareStatement.setString(4, notice.getContent());
prepareStatement.setDate(5, notice.getBeginTime());
prepareStatement.setDate(6, notice.getEndTime());
prepareStatement.executeUpdate();
c++服务器开发
- 粉丝: 3181
- 资源: 4461
最新资源
- 基于java+ssm+mysql+微信小程序的食堂校园预约就餐小程序 源码+数据库+论文(高分毕业设计).zip
- 基于java+ssm+mysql+微信小程序的食堂线上订餐小程序 源码+数据库+论文(高分毕业设计).zip
- springboot-vue-民谣网站的设计与实现-源码工程-29页从零开始全套图文详解-32页设计论文-21页答辩ppt-全套开发环境工具、文档模板、电子教程、视频教学资源分享
- 基于python编写的视频合成代码
- T型三电平并网逆变器Matlab Simulink仿真模型,采用双闭环控制策略,并网电流外环,电容电流有源阻尼内环,电流波形质量完美, THD不到2%,采用三电平SVPWM算法,大扇区小扇区判断 报
- 河南地下粮仓工艺与设备设计
- 基于java+ssm+mysql+微信小程序的童装购买平台 源码+数据库+论文(高分毕业设计).zip
- 基于java+ssm+mysql+微信小程序的投票评选系统 源码+数据库+论文(高分毕业设计).zip
- 基于java+ssm+mysql+微信小程序的外卖点餐系统 源码+数据库+论文(高分毕业设计).zip
- Javascript数据类型转换规则电脑资料
- 基于java+ssm+mysql+微信小程序的微信评分小程序 源码+数据库+论文(高分毕业设计).zip
- 梯形图转HEX 51plc方案5.6.4.2版本,低成本plc方案,支持温湿度传感器,支持ds18b20.,支持无线联网,支持数码管按钮,最近发现软件在个别系统运行不良,(w764位95%可以用)
- java程序员辞职报告
- 多状态挠曲电结构电性能不确定性分析与仿真
- 微信小程序源码-大学生闲置物品交易平台的分析与设计-微信端-毕业设计源码-期末大作业.zip
- 微信小程序源码-大学生心理健康服务-微信端-毕业设计源码-期末大作业.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈