修正根据文章id查找username问题
This commit is contained in:
@@ -99,14 +99,19 @@ public class articleDao {
|
|||||||
public articleEntity selectByArticleId(int id){
|
public articleEntity selectByArticleId(int id){
|
||||||
Connection conn = null;
|
Connection conn = null;
|
||||||
PreparedStatement preparedStatement = null;
|
PreparedStatement preparedStatement = null;
|
||||||
|
PreparedStatement preparedStatement1 = null;
|
||||||
ResultSet rs = null;
|
ResultSet rs = null;
|
||||||
String sql = "SELECT * FROM `article` WHERE articleId = ? AND isValid = 1;";
|
String sql = "SELECT * FROM `article` WHERE articleId = ? AND isValid = 1;";
|
||||||
|
String sql1 = "UPDATE article SET readCount = readCount+1 where articleId = ?";
|
||||||
articleEntity entity = null;
|
articleEntity entity = null;
|
||||||
try{
|
try{
|
||||||
conn = JdbcUtils.getConnection();
|
conn = JdbcUtils.getConnection();
|
||||||
preparedStatement.setInt(1, id);
|
preparedStatement.setInt(1, id);
|
||||||
preparedStatement = conn.prepareStatement(sql);
|
preparedStatement = conn.prepareStatement(sql);
|
||||||
|
preparedStatement1.setInt(1, id);
|
||||||
|
preparedStatement1 = conn.prepareStatement(sql);
|
||||||
rs = preparedStatement.executeQuery();
|
rs = preparedStatement.executeQuery();
|
||||||
|
preparedStatement1.executeQuery();
|
||||||
|
|
||||||
while(rs.next()){
|
while(rs.next()){
|
||||||
int articleid = rs.getInt(1);
|
int articleid = rs.getInt(1);
|
||||||
@@ -115,12 +120,16 @@ public class articleDao {
|
|||||||
String datetime = rs.getString(4);
|
String datetime = rs.getString(4);
|
||||||
int isvalid = rs.getInt(5);
|
int isvalid = rs.getInt(5);
|
||||||
int readcount = rs.getInt(6);
|
int readcount = rs.getInt(6);
|
||||||
|
String articleTitle = rs.getString(7);
|
||||||
|
String articleIco = rs.getString(8);
|
||||||
entity.setArticleId(articleid);
|
entity.setArticleId(articleid);
|
||||||
entity.setUserId(userid);
|
entity.setUserId(userid);
|
||||||
entity.setArticleContent(articlecontent);
|
entity.setArticleContent(articlecontent);
|
||||||
entity.setPublishTime(datetime);
|
entity.setPublishTime(datetime);
|
||||||
entity.setIsValid(isvalid);
|
entity.setIsValid(isvalid);
|
||||||
entity.setReadCount(readcount);
|
entity.setReadCount(readcount);
|
||||||
|
entity.setArticleTitle(articleTitle);
|
||||||
|
entity.setArticleIco(articleIco);
|
||||||
}
|
}
|
||||||
return entity;
|
return entity;
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
@@ -128,40 +137,4 @@ public class articleDao {
|
|||||||
return entity;
|
return entity;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public articleEntity updateReadCount(int id){
|
|
||||||
Connection conn = null;
|
|
||||||
PreparedStatement preparedStatement = null;
|
|
||||||
PreparedStatement preparedStatement1 = null;
|
|
||||||
ResultSet rs = null;
|
|
||||||
String sql = "UPDATE article SET readCount = readCount+1 where articleId = ?";
|
|
||||||
String sql2 = "SELECT * FROM `article` WHERE articleId = ? AND isValid = 1;";
|
|
||||||
articleEntity entity = null;
|
|
||||||
try{
|
|
||||||
conn = JdbcUtils.getConnection();
|
|
||||||
preparedStatement = conn.prepareStatement(sql);
|
|
||||||
preparedStatement1 = conn.prepareStatement(sql2);
|
|
||||||
preparedStatement.setInt(1, id);
|
|
||||||
preparedStatement.executeQuery();
|
|
||||||
preparedStatement1.setInt(1, id);
|
|
||||||
rs = preparedStatement1.executeQuery();
|
|
||||||
while(rs.next()){
|
|
||||||
int articleid = rs.getInt(1);
|
|
||||||
int userid = rs.getInt(2);
|
|
||||||
String articlecontent = rs.getString(3);
|
|
||||||
String datetime = rs.getString(4);
|
|
||||||
int isvalid = rs.getInt(5);
|
|
||||||
int readcount = rs.getInt(6);
|
|
||||||
entity.setArticleId(articleid);
|
|
||||||
entity.setUserId(userid);
|
|
||||||
entity.setArticleContent(articlecontent);
|
|
||||||
entity.setPublishTime(datetime);
|
|
||||||
entity.setIsValid(isvalid);
|
|
||||||
entity.setReadCount(readcount);
|
|
||||||
}
|
|
||||||
return entity;
|
|
||||||
} catch (SQLException e) {
|
|
||||||
throw new RuntimeException(e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
@@ -80,7 +80,4 @@ public class articleService {
|
|||||||
return JSONObject.toJSONString(dataMap);
|
return JSONObject.toJSONString(dataMap);
|
||||||
}
|
}
|
||||||
|
|
||||||
public articleEntity updateReadCount(int id){
|
|
||||||
return articleDao.updateReadCount(id);
|
|
||||||
}
|
|
||||||
}
|
}
|
@@ -1,58 +0,0 @@
|
|||||||
package com.hellogithub.servlet.article;
|
|
||||||
|
|
||||||
import com.alibaba.fastjson.JSONObject;
|
|
||||||
import com.hellogithub.entity.articleEntity;
|
|
||||||
import com.hellogithub.service.articleService;
|
|
||||||
import jakarta.servlet.ServletException;
|
|
||||||
import jakarta.servlet.annotation.WebServlet;
|
|
||||||
import jakarta.servlet.http.HttpServlet;
|
|
||||||
import jakarta.servlet.http.HttpServletRequest;
|
|
||||||
import jakarta.servlet.http.HttpServletResponse;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.io.PrintWriter;
|
|
||||||
import java.util.HashMap;
|
|
||||||
|
|
||||||
@WebServlet("/updateReadCount")
|
|
||||||
public class updateReadCount extends HttpServlet {
|
|
||||||
protected articleService articleservice = new articleService();
|
|
||||||
@Override
|
|
||||||
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
|
||||||
doPost(req, resp);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
|
||||||
resp.setCharacterEncoding("UTF-8");
|
|
||||||
// 设置响应头允许ajax跨域访问
|
|
||||||
String curOrigin = req.getHeader("Origin");
|
|
||||||
resp.setHeader("Access-Control-Allow-Origin", curOrigin == null ? "true" : curOrigin);
|
|
||||||
resp.setHeader("Access-Control-Allow-Credentials", "true");
|
|
||||||
resp.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE, HEAD");
|
|
||||||
resp.setHeader("Access-Control-Max-Age", "3600");
|
|
||||||
resp.setHeader("Access-Control-Allow-Headers", "access-control-allow-origin, authority, content-type, version-info, X-Requested-With");
|
|
||||||
resp.setContentType("application/json;charset=UTF-8");
|
|
||||||
|
|
||||||
PrintWriter writer = resp.getWriter();
|
|
||||||
String articleid = req.getParameter("articleId");
|
|
||||||
int id = Integer.parseInt(articleid);
|
|
||||||
articleEntity entity = articleservice.selectByArticleId(id);
|
|
||||||
if(entity == null){
|
|
||||||
setResultError("该文章id不存在或已失效", writer);
|
|
||||||
}else{
|
|
||||||
String jsonString = JSONObject.toJSONString(entity);
|
|
||||||
writer.println(jsonString);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
public void setResult(Integer code, String msg, PrintWriter writer) {
|
|
||||||
HashMap<String, Object> result = new HashMap<>();
|
|
||||||
result.put("code", code);
|
|
||||||
result.put("msg", msg);
|
|
||||||
String jsonString = JSONObject.toJSONString(result);
|
|
||||||
writer.println(jsonString);
|
|
||||||
writer.close();
|
|
||||||
}
|
|
||||||
public void setResultError(String msg, PrintWriter writer) {
|
|
||||||
setResult(500, msg, writer);
|
|
||||||
}
|
|
||||||
}
|
|
Reference in New Issue
Block a user