2015-01-08 53 views
-1

我有一個應該從我的數據庫中刪除記錄的servlet。 我從一個帶有刪除按鈕的jsp頁面中獲取選定的行,並使用javascript。 問題是,tomcat顯示HTTP狀態405 - 此方法不支持HTTP方法GET,頁上:http://localhost:8084/DeleteRecord?i=35(35是選定的行將被刪除)。Servlet無法刪除數據庫中的記錄

這裏是我的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> 

任何想法?

+0

http://www.unixwiz.net/techtips/sql-injection.html – BalusC

回答

0

固定它,需要該代碼在servlet:

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

您可以通過點擊綠色的勾號來接受您自己的答案.... –