Servlet无法从数据库中删除记录

时间:2015-01-08 00:15:09

标签: database jsp servlets record delete-record

我有一个应该从我的数据库中删除记录的servlet。 我从带有删除按钮的jsp页面中获取所选行,并使用javascript。 问题是tomcat显示HTTP状态405 - 该URL不支持HTTP方法GET,在页面上:http://localhost:8084/DeleteRecord?i=35(35是要删除的选定行)。

这是我的Servlet:

package package_ergasia;

import java.sql.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.ArrayList;

public class DeleteRecord extends HttpServlet 
{
   @Override
   public void doPost(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException 
   {
    response.setContentType("text/html");    
    Connection connection= null;    
    String url = "jdbc:mysql://localhost:3306/";
    String dbName = "ergasia";    
    String user = "root";
    String password = "password"; 
    PreparedStatement deleteProtein = null;
    ResultSet resultSet = null;  
    ArrayList al = null;
    PrintWriter out = response.getWriter();
    int i;

        try {
            connection = DriverManager.getConnection(url + dbName, user, password);
            i = Integer.parseInt(request.getParameter("i"));           
            deleteProtein = connection.prepareStatement("DELETE FROM protein WHERE i = '"+i+"'");            
            resultSet = deleteProtein.executeQuery();

            RequestDispatcher view = request.getRequestDispatcher("http://localhost:8084/secured/all_proteins.jsp");
            view.forward(request, response);          

        }
        catch(Exception e){
            e.printStackTrace();
            System.out.println("Error!");
        }

   }

    @Override
    public String getServletInfo() {
        return "info";
    }
}  

和all_proteins.jsp,用户选择要删除的记录:

<%@ page language="java" import="java.sql.*,java.util.* "%>


<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> 
        <link rel="stylesheet" type="text/css" href="../CSS/mystyle.css">
        <title>Database management</title>       
    </head>
    <body class="menu">
          <h1>Welcome to KSPO Database, the Database of Porins with known 3D Structure</h1>           
          <script type="text/javascript">
              function deleteRecord(i){
                  url = "DeleteRecord";
                  window.location.href = "http://localhost:8084/"+url+"?i="+i;
              }
              function editRecord(i){
                  url = "EditRecord";
                  window.location.href = "http://localhost:8084/"+url+"?i="+i;
              }
          </script>

        <table border="1">             
<% 


                int count = 0;    
                int i=-1;
                if (request.getAttribute("protein_data") != null) {
                    ArrayList al1 = (ArrayList) request.getAttribute("protein_data");
                    request.getAttribute("protein_data");


                   Iterator itr = al1.iterator();

                    while (itr.hasNext()) {
                        count++;
                        i++;
                        ArrayList pList = (ArrayList) itr.next();
%>                


    <tr>
                <td><%=pList.get(0)%></td>
                <td><%=pList.get(1)%></td>
                <td><%=pList.get(2)%></td>   
                <td><%=pList.get(3)%></td>
                <td><input type="submit" value="Edit" name="edit" onclick="editRecord(<%=pList.get(4)%>);"></td>
                <td><input type="submit" value="Delete" name="delete" onclick="deleteRecord(<%=pList.get(4)%>);"></td>
    </tr>
 <%
                   }
                }  
%>
</table>
</body>         
</html>

有什么想法吗?

2 个答案:

答案 0 :(得分:0)

修正了它,servlet需要这段代码:

statement = connection.createStatement();
deleteProtein = "DELETE FROM protein WHERE i = '"+i+"'";            
int j = statement.executeUpdate(deleteProtein);

答案 1 :(得分:0)

正如我所知,您正在使用GET请求(window.location.href = "http://localhost:8084/"+url+"?i="+i;)调用servlet。因此,为了使其正常工作,您应该在Servlet中实现doGet()方法。

请参阅此doGet and doPost in Servlets,了解如何在Servlet中使用doGet和doPost

另请查看此https://stackoverflow.com/tags/servlets/info以获取有关Servlet和正确用法的更多信息。