2016-06-03 2 views
0

안녕하세요 저는 학교 프로젝트 용 페이지를 만들려고합니다. 전체 주제는 휴가 목적지를 생성, 삭제, 검색, 업데이트하는 것입니다. 레코드를 삭제하는 데 문제가 있습니다. 삭제하려는 대상의 이름을 받으려면 양식이있는 html 페이지를 만들었습니다. 다음으로 내가 만든 자바 페이지의 코드가 있습니다. 뭐가 잘못 보이니? 왜냐하면 내가하려는 것은 기록이 삭제되지 않기 때문입니다. 감사합니다JDBC에서 레코드를 삭제할 수 없습니다.

HTML 페이지

<html> 
    <head> 
     <title>Delete</title> 
     <meta charset="UTF-8"> 
     <meta name="viewport" content="width=device-width, initial-scale=1.0"> 
    </head> 
    <body> 
     <h1 align="center">Insert the destination you want to delete</h1> 

     <form action="delete.jsp" method="post"> 
      <input type="text" name="delete"> 
      <BR> 
      <INPUT TYPE="SUBMIT" value="Delete!"> 
     </form> 





    </body> 
</html> 

JAVA 페이지 :

<%@page contentType="text/html" pageEncoding="UTF-8"%> 
<%@page import="java.sql.*" %> 
<!DOCTYPE html> 
<html> 
    <head> 
     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
     <title>Delete</title> 
    </head> 
    <body> 


     <% 

      String name=request.getParameter("name"); 
      Class.forName("com.mysql.jdbc.Driver"); 
java.sql.Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/vac", 
"user","pass"); 

Statement myStatement=con.createStatement(); 
String SQLstring="DELETE FROM dest WHERE name= '" +name+ "'"; 
myStatement.executeUpdate(SQLstring); 
myStatement.close(); 
con.close(); 
out.println("Destination deleted!"); 

     %> 
    </body> 
</html> 
+0

html에는 "name"매개 변수가 어디에 있습니까? – OldProgrammer

+1

또한 코드는 SQL 주입 공격에 취약합니다. [여기를 읽으십시오] (http://bobby-tables.com)에 대한 정보와 해당 정보를 방지하는 방법 – Barranka

답변

3

나는 매개 변수 이름은 양식 입력 이름에 따라, 더 "이름"을 "삭제할"라고 생각합니다.

감사합니다.

+0

오 하나님 감사합니다 ..... : D –

0

Antonio Martinez의 답변에 따르면 매개 변수 이름이 잘못되었습니다 (name이 아니지만 delete). 코드가 보여주는 SQL 주입 위험을 지적하기 위해이 대답을 게시해야한다고 생각합니다.

은 가짜 코드 삽입을 허용 할 수 있기 때문에 외부 쿼리 매개 변수를 사용하여 쿼리를 작성해야합니다. 다시

String sqlString = "delete from dest where name=?"; 
/* The question-mark is a place holder for the parameter. 
    Notice that you don't need to enclose it in quotes, 
    the prepared statement will take care about that. */ 
PreparedStatement ps = con.prepareStatement(sqlString); 
/* Notice that nothing is executed here: you're only preparing the 
    statement using the SQL string (which includes the place-holder(s) 
    for the parameter(s). */ 
ps.setString(1, delete) 
/* Here you assign the parameter(s) value(s) to the prepared statement. 
    The parameters are numbered starting from one, and ordered 
    the way they appear in your SQL string. 
    The setXXX() methods of the prepared statement allow you to 
    pass the correct value to the query. Strings, in this case, are 
    properly handled, so any rogue code the user might try to inject will 
    not pass as "executable code", but simply as a string. */ 
ps.execute(); 

, 내가 SQL 주입 공격에 대해 배울 당신에게 read here 조언 : 그들이 무엇을, 어떻게 방지하기 위해 그들과로 인한 위험은 무엇인가 당신은 항상 사용은 사용자의 입력을 처리하는 제표를 작성한다 그들.

관련 문제