尝试使用Java和提交按钮更新MySQL数据库中的记录

时间:2019-01-30 00:16:54

标签: java html mysql sql jsp

我正在寻求更新记录,但找不到记录无法正常工作的原因。相同的方法可以很好地将记录添加到MySQL数据库。没有错误发生,页面刷新,但记录未在数据库中更新。在下面,您将看到代码。任何帮助将不胜感激。

JSP页面

<%@page import="Basic.DBManager"%>
<%@page import="Model.Test"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page language="java" import="java.sql.*" %>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html lang="en">
    <div class="container">
        <div class="row">
            <div class="col-lg-12">
               <h4>Pending Variation Requests</h4>
                    <div class="row">
                       <form class="center" name="updaterequest" id="form" action="updateRequestServlet" method="POST">
                          <table class="table">
                            <tbody>
                             <%
                              String clientId=session.getAttribute("clientId").toString();
                              DBManager dbmgr = new DBManager();
                              Connection connection = dbmgr.getConnection();

                              Statement statement = null;
                              ResultSet resultSet = null;
                              %>

                              <tr>
                              </tr>
                              <tr>
                              <td><b>Date</b></td>
                              <td><b>Request ID</b></td>
                              <td><b>Title</b></td>
                              <td><b>Relevant Stage</b></td>
                              <td><b>Request information</b></td>
                              <td><b>Quote</b></td>
                              <td><b>Status</b></td>
                              <td><b>Update</b></td>
                              </tr>
                              <%
                              try{ 
                                statement=connection.createStatement();
                                String sql ="SELECT * FROM request WHERE client_id= " + clientId + " and request_status='Pending'";

                                resultSet = statement.executeQuery(sql);
                                                                while(resultSet.next()){
                              %>

                              <tr>

                              <td><%=resultSet.getString("request_date") %></td>
                              <td><%=resultSet.getString("request_id") %></td>
                              <td><%=resultSet.getString("request_title") %></td>
                              <td><%=resultSet.getString("request_stage") %></td>
                              <td><%=resultSet.getString("request_info") %></td>
                              <td><%=resultSet.getString("request_quotation")%></td>
                              <td><%=resultSet.getString("request_status") %></td>
                              <td><select name="status" >
                              <option value="Approved">Approve</option>
                              <option value="Rejected">Reject</option>
                              </select></td>

                              </tr>
                              <% 
                              }
                              connection.close();
                              } catch (Exception e) {
                                 e.printStackTrace();
                              }
                              %>
                              </tbody>
                              </table>
                              <div class="col-sm-6 col-lg-6">
                              <div class="wrap" align="right">
                              <button type="submit"> Submit </button>
            </div>
         </div>
     </form>
    </div>
    </div>
    </div>
    </div>

调用updateRequestServlet.java

package Servlets;

import Manager.RequestManager;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


public class updateRequestServlet extends HttpServlet {

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        HttpSession session = request.getSession();

        String clientId =session.getAttribute("clientId").toString();
        String status = request.getParameter("status");


            RequestManager rMgr = new RequestManager();
            rMgr.updateRequest(clientId, status);


            RequestDispatcher rd = request.getRequestDispatcher("/Requests.jsp");
                    rd.forward(request, response);

    }

}

然后转到RequestManager.java

package Servlets;

import Manager.RequestManager;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


public class updateRequestServlet extends HttpServlet {

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        HttpSession session = request.getSession();

        String clientId =session.getAttribute("clientId").toString();
        String status = request.getParameter("status");


            RequestManager rMgr = new RequestManager();
            rMgr.updateRequest(clientId, status);


            RequestDispatcher rd = request.getRequestDispatcher("/Requests.jsp");
                    rd.forward(request, response);

    }
}

最后在Re​​questDAO.java中调用updateRequest

package DAO;

import Model.Request;
import Basic.DBManager;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;

public class RequestDAO {

     //inserting a new request into the request table
    public void newRequest(String clientId, String requestname, String stage, String info) {
        String stmtNewRequest = "INSERT INTO request(client_id, request_title, request_stage, request_info)\n VALUES('" + clientId + "', '" + requestname + "', '" + stage + "', '" + info + "')";
        DBManager dbm = new DBManager();
        Connection con = dbm.getConnection();
        executeSqlScript(con, stmtNewRequest);
    }

     //inserting an update request into the request table
    public void updateRequest(String clientId, String status) {
        String updateRequest = "UPDATE request SET request_status=" + status + "WHERE clientId= "+ clientId ;
        DBManager dbm = new DBManager();
        Connection con = dbm.getConnection();
        executeSqlScript(con, updateRequest);
    }

    public void executeSqlScript(Connection conn, String stmtRequest) {

        Logger logger = Logger.getLogger(DBManager.class.getName());

        // Loop through the SQL  statements 
        Statement currentStatement = null;

        // Get statement 
        String rawStatement = stmtRequest;
        try {
            // Execute statement
            currentStatement = conn.createStatement();
            currentStatement.execute(rawStatement);
        } catch (SQLException sqlExcept) {
            logger.log(Level.SEVERE, null, sqlExcept);
        }

    }
}

0 个答案:

没有答案