【问题标题】:JSP: How to increase a value of a database in JSP? Error when i click the buttonJSP:如何在 JSP 中增加数据库的值?单击按钮时出错
【发布时间】:2018-12-22 03:04:01
【问题描述】:

-------- 2 天前 --------

“我只为网站的“喜欢”做了一个数据库。数据库有一个表(喜欢),里面有几个字段(Like1,Like2,等等......)

在一个 JSP 文件中,我建立了连接并显示了一个字段的值(Like1)。

但我想做的是:通过点击某个文本,值加一,将数据库中的值从 Like1 更新为 Like1 + 1。”

--- 编辑 ---

当我点击按钮时,给我看这个:

ERROR

当我刷新时,数据不会改变。所以我认为如果我没有显示错误,它会起作用。

我想要的是,当你按下按钮时,数据库数据增加 1。

结构:

Structure

index.jsp:

<body>
    <div class="col-lg-12">
        <form method="post" action="likeCount">
            <button type="submit" name="click"><b style="color:#ff5858;font-size: 24px;">❤</b></button>
            <% try {
                Connection conn = DBConnect.connect();
                PreparedStatement pstmt = conn.prepareStatement("SELECT Like1 from Likes");
                ResultSet rs = pstmt.executeQuery();
                while (rs.next()) {%>
                <input type="text" hidden name="counts" value="<%=rs.getInt("Like1")%>" />

        </form>
        <span style="font-weight: bold"><%=session.getAttribute("count")==null)?rs.getInt("Like1"):(session.getAttribute("count"))%></span><% }
            } catch (SQLException ex) {
                System.out.println("Error in select: " + ex.getMessage());
                   }%>
    </div>
</body>

DBConnect.java:

package myPack;

import com.mysql.jdbc.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class DBConnect {

    public static Connection connect() throws SQLException {
        Connection conn = null;
        String url = "jdbc:mysql://localhost:3306/Likes";
        String username = "root";
        String password = "";

        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn = (Connection) DriverManager.getConnection(url, username, password);
        } catch (ClassNotFoundException | SQLException e) {
            System.out.println("Error : " + e.getMessage());
        }
        return conn;
    }
}

likeCount.java:

package myPack;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class likeCount {

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException, SQLException {
    /*check whether the button is clicked*/
    if (request.getParameter("click") != null) {
        try {
            /*getting the number of likes*/
            String x = request.getParameter("counts");
            int c = Integer.parseInt(x);
            int count = c;
            count = count + 1; //increment the value

            Connection conn = DBConnect.connect();
            PreparedStatement ps = conn.prepareStatement("UPDATE Likes SET Like1 = ?");
            ps.setInt(1, count);
            int num = ps.executeUpdate();
            if (num > 0) {
                HttpSession session = request.getSession();
                session.setAttribute("count", count);
                response.setIntHeader("Refresh", 3);
                response.sendRedirect("index.jsp");
            }

        } catch (SQLException e) {
            System.out.println("Error in update :" + e.getMessage());
            }
        }
    }
}

【问题讨论】:

标签: mysql database html jsp


【解决方案1】:

在这种情况下,您将需要 一个 servlet一个数据库连接 类。

首先,这是数据库连接类。 DBConnect.java

public class DBConnect {
    public static Connection connect() {
        Connection conn = null;
        String url = "jdbc:mysql://localhost:3306/likes";
        String username = "root";
        String password = "";

        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn = (Connection) DriverManager.getConnection(url, username, password);
        } catch (ClassNotFoundException | SQLException e) {
            System.out.println("Error : "+e.getMessage());
        }
        return conn;
    }
}

接下来,这将是 index.jsp 表单

<body>
        <div class="col-lg-2">
        <form method="post" action="likeCount">
            <button type="submit" name="click">TEXTO DONDE QUISIERA QUE INCREMENTE AL HACER CLICK.</button>
            <% try {
                    Connection conn = DBConnect.connect();
                    PreparedStatement pstmt = conn.prepareStatement("SELECT Like1 from Likes");
                    ResultSet rs = pstmt.executeQuery();
                    while (rs.next()) {%>
                    <input type="text" hidden name="counts" value="<%=rs.getInt("Like1")%>" />

        </form>
        <span style="font-weight: bold"><%=(session.getAttribute("count")==null)?rs.getInt("Like1"):(session.getAttribute("count"))%></span>
        <% }
                } catch (SQLException ex) {
                    System.out.println("Error in select: " + ex.getMessage());
                       }%>
    </div>
    </body>

最后是 servlet。 likeCount.java

@Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        /*check whether the button is clicked*/
        if (request.getParameter("click") != null) {
            try {
                /*getting the number of likes*/
                String x = request.getParameter("counts");
                int c = Integer.parseInt(x);
                int count = c;
                count = count + 1; //increment the value

                Connection conn = DBConnect.connect();
                PreparedStatement ps = conn.prepareStatement("UPDATE Likes SET Like1 = ?");
                ps.setInt(1, count);
                int num = ps.executeUpdate();
                if (num > 0) {
                    HttpSession session = request.getSession();
                    session.setAttribute("count", count);
                    response.setIntHeader("Refresh", 3);
                    response.sendRedirect("index.jsp");
                }

            } catch (SQLException e) {
                System.out.println("Error in update :" + e.getMessage());
            }
        }
    }

希望它会起作用。

【讨论】:

  • 谢谢!对我很有帮助,你是最棒的!
  • 还有更多。当我点击时,增加了,但是当我更新页面时,也增加了,如何解决?抱歉,谢谢。
  • 好的,我会检查并发送给你?。不用担心
  • @GuillermoLópez 疑问已解决。我编辑了代码。试试看
  • 不幸的是,它向我显示此错误:HTTP 状态 404 - 未找到类型:状态报告消息:未找到描述:请求的资源不可用。我检查了所有名称和地址是否正确,所有文件是否都在文件夹中等等,但错误仍然存​​在。我知道这是一个容易解决的错误,但我找不到它。如果您知道导致该错误的原因,请告诉我,我会尽力解决。谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-21
  • 2014-05-13
相关资源
最近更新 更多