package yitian.learn.dao;
import yitian.learn.entity.Article;
import yitian.learn.entity.Comment;
import yitian.learn.entity.User;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
public class ArticleDaoImpl implements ArticleDao {
private Connection connection;
public ArticleDaoImpl() {
connection = JdbcUtil.getConnection();
}
@Override
public void add(Article article) {
try (PreparedStatement statement = connection.prepareStatement("INSERT INTO article(title,content,create_time,modify_time,author) VALUES(?,?,?,?,?)")) {
statement.setString(1, article.getTitle());
statement.setString(2, article.getContent());
statement.setObject(3, article.getCreateTime());
statement.setObject(4, article.getModifyTime());
statement.setInt(5, article.getAuthor().getId());
statement.executeUpdate();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
@Override
public Optional<Article> find(int id) {
Article article = null;
try (PreparedStatement statement = connection.prepareStatement("SELECT id,title,content,author,create_time,modify_time FROM article WHERE id=?")) {
statement.setInt(1, id);
ResultSet rs = statement.executeQuery();
while (rs.next()) {
article = new Article();
article.setId(rs.getInt(1));
article.setTitle(rs.getString(2));
article.setContent(rs.getString(3));
UserDao userDao = DaoFactory.getUserDao();
Optional<User> user = userDao.find(rs.getInt(4));
user.ifPresent(article::setAuthor);
article.setCreateTime(rs.getObject(5, LocalDateTime.class));
article.setModifyTime(rs.getObject(6, LocalDateTime.class));
}
} catch (SQLException e) {
throw new RuntimeException(e);
}
return Optional.ofNullable(article);
}
@Override
public void update(Article article) {
try (PreparedStatement statement = connection.prepareStatement("UPDATE article SET title=?,content=?,modify_time=? WHERE id=?")) {
statement.setString(1, article.getTitle());
statement.setString(2, article.getContent());
statement.setObject(3, article.getModifyTime());
statement.setInt(4, article.getId());
statement.executeUpdate();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
@Override
public void delete(int id) {
try (PreparedStatement statement = connection.prepareStatement("DELETE FROM article WHERE id=?")) {
statement.setInt(1, id);
statement.executeUpdate();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
@Override
public List<Article> listAllOf(User author) {
List<Article> articles = new ArrayList<>();
try (PreparedStatement statement = connection.prepareStatement("SELECT id,title,content,create_time,modify_time FROM article WHERE author=?")) {
statement.setInt(1, author.getId());
ResultSet rs = statement.executeQuery();
while (rs.next()) {
Article article = new Article();
article.setId(rs.getInt(1));
article.setTitle(rs.getString(2));
article.setContent(rs.getString(3));
article.setCreateTime(rs.getObject(4, LocalDateTime.class));
article.setModifyTime(rs.getObject(5, LocalDateTime.class));
article.setAuthor(author);
CommentDao commentDao = DaoFactory.getCommentDao();
List<Comment> comments = commentDao.listAllOf(article);
article.setComments(comments);
articles.add(article);
}
} catch (SQLException e) {
throw new RuntimeException(e);
}
return articles;
}
@Override
public List<Article> listAll() {
List<Article> articles = new ArrayList<>();
try (PreparedStatement statement = connection.prepareStatement("SELECT id,title,content,create_time,modify_time,author FROM article ")) {
ResultSet rs = statement.executeQuery();
while (rs.next()) {
Article article = new Article();
article.setId(rs.getInt(1));
article.setTitle(rs.getString(2));
article.setContent(rs.getString(3));
article.setCreateTime(rs.getObject(4, LocalDateTime.class));
article.setModifyTime(rs.getObject(5, LocalDateTime.class));
UserDao userDao = DaoFactory.getUserDao();
Optional<User> user = userDao.find(rs.getInt(6));
user.ifPresent(article::setAuthor);
CommentDao commentDao = DaoFactory.getCommentDao();
List<Comment> comments = commentDao.listAllOf(article);
article.setComments(comments);
articles.add(article);
}
} catch (SQLException e) {
throw new RuntimeException(e);
}
return articles;
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
纯Servlet和JSP实现的博客小程序
共81个文件
java:30个
jsp:17个
xml:15个
4星 · 超过85%的资源 需积分: 12 160 下载量 201 浏览量
2017-01-22
20:33:17
上传
评论 4
收藏 3.91MB ZIP 举报
温馨提示
纯Servlet和JSP实现的博客小程序,用到的第三方类库只有JSTL和MySQL驱动。功能有用户注册、登录,用户修改信息、新建、更新博客,添加评论。系统需求;JDK1.8、MySQL数据库、Tomcat8或者其他支持Java EE 7的服务器
资源推荐
资源详情
资源评论
收起资源包目录
simple-blog.zip (81个子文件)
simple-blog
src
yitian
learn
entity
Article.java 476B
User.java 558B
Comment.java 381B
filter
CharEncodingFilter.java 1KB
UserAuthenticationFilter.java 2KB
dao
UserDaoImpl.java 4KB
ArticleDaoImpl.java 6KB
CommentDao.java 308B
CommentDaoImpl.java 4KB
JdbcUtil.java 1006B
DaoFactory.java 579B
ArticleDao.java 405B
UserDao.java 294B
util
MessageType.java 218B
Validator.java 314B
test
UserDaoTest.java 2KB
ArticleDaoTest.java 3KB
CommentDaoTest.java 3KB
servlet
UpdateUserServlet.java 2KB
UserInfoServlet.java 723B
ViewUserServlet.java 3KB
LogoutServlet.java 800B
UpdateUserPasswordServlet.java 2KB
LoginServlet.java 2KB
UpdateArticleServlet.java 1KB
EditArticleServlet.java 1KB
ViewArticleServlet.java 3KB
CommentServlet.java 1KB
NewArticleServlet.java 2KB
RegisterServlet.java 3KB
init-db.sql 949B
web
fonts
glyphicons-halflings-regular.woff2 18KB
glyphicons-halflings-regular.ttf 44KB
glyphicons-halflings-regular.woff 23KB
glyphicons-halflings-regular.svg 106KB
glyphicons-halflings-regular.eot 20KB
images
404.png 636KB
_comment.jsp 125B
viewUser.jsp 5KB
_alert.jsp 748B
about.jsp 388B
_footer.jsp 111B
js
bootstrap.js 68KB
jquery.js 261KB
marked.js 28KB
_header.jsp 250B
404.jsp 511B
index.jsp 419B
jumpToIndex.jsp 855B
me.jsp 4KB
_navbar.jsp 2KB
css
bootstrap.css 143KB
bootstrap-theme.css 26KB
viewArticle.jsp 5KB
_template.jsp 303B
login.jsp 1KB
editArticle.jsp 2KB
newArticle.jsp 2KB
register.jsp 2KB
WEB-INF
lib
hamcrest-all-1.3.jar 299KB
mysql-connector-java-5.1.40.jar 968KB
jstl-impl-1.2.jar 383KB
junit-4.12.jar 308KB
javax.servlet.jsp.jstl-api-1.2.1.jar 32KB
lombok-1.16.12.jar 1.35MB
web.xml 510B
.idea
misc.xml 3KB
encodings.xml 200B
workspace.xml 80KB
sqldialects.xml 169B
uiDesigner.xml 9KB
dataSources.xml 889B
dataSources.local.xml 799B
artifacts
simple_blog_war.xml 326B
simple_blog_war_exploded.xml 1KB
inspectionProfiles
Project_Default.xml 155B
compiler.xml 247B
dataSources
871fc265-4e78-4ac6-81c2-0eb0ca052314.xml 5KB
871fc265-4e78-4ac6-81c2-0eb0ca052314
storage.xml 124B
_staging_
_src_
modules.xml 262B
simple-blog.iml 1KB
共 81 条
- 1
资源评论
- luyunix2017-11-18老哥 数据库脚本没有呢
- qq1110003332017-11-01少东西吧。。
乐百川
- 粉丝: 1540
- 资源: 10
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功