From 8a5428bec1574fe766dc3c1d9f30890003b20d04 Mon Sep 17 00:00:00 2001 From: sjm <2431685932@qq.com> Date: Tue, 4 Jul 2023 20:28:07 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=AD=A3=E6=A0=B9=E6=8D=AE=E6=96=87?= =?UTF-8?q?=E7=AB=A0id=E6=9F=A5=E6=89=BEusername=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/com/hellogithub/dao/articleDao.java | 8 +++++--- src/com/hellogithub/service/articleService.java | 15 +++++++++++++-- .../servlet/article/selectByArticleId.java | 8 ++++---- 3 files changed, 22 insertions(+), 9 deletions(-) diff --git a/src/com/hellogithub/dao/articleDao.java b/src/com/hellogithub/dao/articleDao.java index f962329..cc9ea63 100644 --- a/src/com/hellogithub/dao/articleDao.java +++ b/src/com/hellogithub/dao/articleDao.java @@ -104,9 +104,10 @@ public class articleDao { articleEntity entity = null; try{ conn = JdbcUtils.getConnection(); + preparedStatement.setInt(1, id); preparedStatement = conn.prepareStatement(sql); rs = preparedStatement.executeQuery(); - preparedStatement.setInt(1, id); + while(rs.next()){ int articleid = rs.getInt(1); int userid = rs.getInt(2); @@ -122,8 +123,9 @@ public class articleDao { entity.setReadCount(readcount); } return entity; - } catch (SQLException e) { - throw new RuntimeException(e); + } catch (Exception e) { + e.printStackTrace(); + return entity; } } diff --git a/src/com/hellogithub/service/articleService.java b/src/com/hellogithub/service/articleService.java index c79af34..8ab3a27 100644 --- a/src/com/hellogithub/service/articleService.java +++ b/src/com/hellogithub/service/articleService.java @@ -65,8 +65,19 @@ public class articleService { return JSONObject.toJSONString(dataMap1); } - public articleEntity selectByArticleId(int id){ - return articleDao.selectByArticleId(id); + public String selectByArticleId(int id){ + Map dataMap = new HashMap<>(); + articleEntity articleEntity = articleDao.selectByArticleId(id); + String username = userDao.selectNameById(id); + dataMap.put("ArticleContent",articleEntity.getArticleContent()); + dataMap.put("ArticleId",articleEntity.getArticleId()); + dataMap.put("ArticleTitle",articleEntity.getArticleTitle()); + dataMap.put("IsValid",articleEntity.getIsValid()); + dataMap.put("ReadCount",articleEntity.getReadCount()); + dataMap.put("username",username); + dataMap.put("publishtime",articleEntity.getPublishTime()); + dataMap.put("articleico",articleEntity.getArticleIco()); + return JSONObject.toJSONString(dataMap); } public articleEntity updateReadCount(int id){ diff --git a/src/com/hellogithub/servlet/article/selectByArticleId.java b/src/com/hellogithub/servlet/article/selectByArticleId.java index 5678db3..c86b24a 100644 --- a/src/com/hellogithub/servlet/article/selectByArticleId.java +++ b/src/com/hellogithub/servlet/article/selectByArticleId.java @@ -35,14 +35,14 @@ public class selectByArticleId extends HttpServlet { PrintWriter writer = resp.getWriter(); - String articleid = req.getParameter("articleId"); + String articleid = req.getParameter("id"); int id = Integer.parseInt(articleid); - articleEntity entity = articleservice.selectByArticleId(id); - if(entity == null){ + String jsonString = articleservice.selectByArticleId(id); + if(jsonString == null){ setResultError("该文章id不存在或已失效", writer); }else{ - String jsonString = JSONObject.toJSONString(entity); writer.println(jsonString); + writer.close(); } } public void setResult(Integer code, String msg, PrintWriter writer) {