How do I implement the Like feature in Servlet, Java, and PostgreSQL?

Asked 2 years ago, Updated 2 years ago, 133 views

I want to create a program that shows the total number of likes per article by pressing the likes link, but running jsp doesn't work.

Enter a description of the image here

test.jsp

<%@pagelanguage="java" contentType="text/html;charset=UTF-8"
pageEncoding="UTF-8"%>
<%@pageimport="model.Goods"%>
<%Goods goods=newGoods();%>
<!DOCTYPE html>
<html>
<head>
<metacharset="UTF-8">
<title> Test Screen </title>
</head>
<body>
<a href="/gotoFukuoka/GoodAdd?user_id=<%=rs.getString("user_id")%>?article_id=<%= 
rs.getString("article_id")%>>Like</a><br>
<p>Number of likes: <%=goods.getGoodCnt()%>/p>
</body>
</html>

Goods.java(JavaBeans)

package model;

import java.io.Serializable;

public class Goods implements Serializable {

    private String article_id;
    private String user_id;
    private int goodCnt;

    public int getGoodCnt(){
        return goodCnt;
    }
    public void setGoodCnt(int goodCnt){
        This.goodCnt=goodCnt;
    }
    
    public Goods(){}
    public Goods(String article_id, String user_id){
        this.article_id =article_id;
        this.user_id = user_id;
    }

    public String getArticle_id(){
        return article_id;
    }

    public void setArcticle_id(String article_id){
        this.article_id =article_id;
    }

    public String getUser_id(){
        return user_id;
    }

    public void setUser_id(String user_id){
        this.user_id = user_id;
    }


}

GoodAdd.java(Servlet)

package servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.announcement.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import model.GetGoodLogic;
import model.GoodLogic;
import model.Goods;

/**
 * Like Additional Controllers
 */
@ WebServlet ("/GoodAdd")
public class GoodAddends HttpServlet{
    private static final long serialVersionUID = 1L;
    protected void doGet(HttpServletRequest request, HttpServletResponse response)rowsServletException, IOException {
        // Retrieve search criteria from request parameters, (get user_id, article_id from JSP)
            request.setCharacterEncoding("UTF-8");
            String user_id = request.getParameter("user_id");
            String article_id = request.getParameter("article_id");

            // Put the data into the Goods() constructor

            Goods goods=new Goods(user_id,article_id);
            GoodLogic goodLogic=new GoodLogic();
            goodLogic.goodPlus(goods);

            request.setAttribute("goods", goods);

    }

}

GoodLogic.java(model)

package model;

import dao.GoodsDAO;

public class GoodLogic {

    public void goodPlus (Goods goods) {

        GoodsDAO dao = new GoodsDAO();
        dao.insert(goods);
    }
}

GetGoodLogic.java(model)

package model;

import dao.GoodsDAO;

public class GetGoodLogic {
    public Goods execute (Goods goods) {

        GoodsDAO dao = new GoodsDAO();
        Goods good=dao.select();
        return good;
    }
}

GoodsDAO

package dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import model.Goods;

/*
 * DAO class on likes
 */
public class GoodsDAO {

     private final String url = "jdbc:postgresql://localhost:5432/Goods";
        private final String user="postgres";
        private final String passWord = "test";

        /********************************************************************************
         * Find the article ID from the Like table, aggregate the likes, and return the search results.
         ********************************************************************************/
        publicResultSetselect(Stringarticle_id){
            Connection con=null;
            PreparedStatementst=null;
            ResultSetters = null;


            try{
                 /* Define JDBC Driver*/
                 Class.forName("org.postgresql.Driver");

                 /* Connecting to PostgreSQL*/
                 con=DriverManager.getConnection(url,user,passWord);

                 /* Prepare SELECT statements*/
                 String sql = "select article_id, count(*)";
                 sql+="from Goods";
                 sql+="group by article_id";
                 sql+="order by count(*)DESC;";
                 st=con.prepareStatement(sql);


                 /* Execute SELECT statements*/
                 rs=st.executeQuery();


            } catch(Exceptione){
                US>System.out.println("An error occurred while accessing DB.");
                e.printStackTrace();
            } US>finally
                 /* Disconnect from PostgreSQL*/
                if(rs!=null){
                    try{
                        rs.close();
                    } catch(SQLExceptione){}
                }

                if(st!=null){
                    try{
                        st.close();
                    } catch(SQLExceptione){}
                }

                if(con!=null){
                    try{
                        con.close();
                    } catch(SQLExceptione){}
                }
            }

            returns;
        }

        /********************************************************************************
         * Add likes to the Like table.
         ********************************************************************************/

        public int insert (Goods goods) {
            Connection con=null;
            PreparedStatementst=null;
            intrs = 0; // Number of updates

            try{
                 /* Define JDBC Driver*/
                 Class.forName("org.postgresql.Driver");

                 /* Connecting to PostgreSQL*/
                 con=DriverManager.getConnection(url,user,passWord);

                 /* INSERT Statement Preparation*/
                 String sql="";
                 sql="INSERT INTO Goods(user_id,article_id)";
                 sql+="VALUES(?,?);";

                 st=con.prepareStatement(sql);
                 st.setString(1,goods.getUser_id());
                 st.setString(2,goods.getArticle_id());

                 /* Execute SELECT statements*/
                 rs=st.executeUpdate();

            } catch(Exceptione){
                US>System.out.println("An error occurred while accessing DB.");
                e.printStackTrace();
            } US>finally
                /* Disconnect from PostgreSQL*/
                if(st!=null){
                    try{
                        st.close();
                    } catch(SQLExceptione){}
                }

                if(con!=null){
                    try{
                        con.close();
                    } catch(SQLExceptione){}
                }
            }


            returns;
        }

SQL

CREATE TABLE GOODS(
  user_id VARCHAR(30) NOT NULL,
  article_id SERIAL NOT NULL,
  good NUMERIC DEFAULT 0,
  PRIMARY KEY(article_id));

java postgresql jsp servlet

2022-09-30 15:43

1 Answers

There may be other things that are wrong, but at least the ones that are clearly wrong are the following lines:

<a href="/gotofukuoka/GoodAdd?user_id=<%=rs.getString("user_id")%>?article_id=<%= 
rs.getString("article_id")%>>Like</a><br>

The second query parameter is ?article_id=, so request.getParameter("article_id") should be null, so you need to change it to &article_id=.


2022-09-30 15:43

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.