我是 JSP 新手,我正在尝试创建一个 Web 界面,用户可以在其中输入他们想要删除的信息,并且它将在数据库表中删除。
他们应在此处输入 student_id
和 course_id
,然后应删除具有这 2 个指定 ID 的任何数据。但是,它并没有从表中删除。我遇到异常
array index out of bounds
这是我的代码:
Delete Row where Student ID: <input type="text" name="dStudent">
and Course ID: <input type="text" name="dCourse">
<input type ="submit" value="Delete">
String delCourse = request.getParameter("dCourse");
String delStudent = request.getParameter("dStudent");
if(delCourse != null && delCourse.length() > 0 && delStudent != null && delCourse.length() > 0){
statement.executeUpdate("DELETE FROM enroll WHERE student_id = '" + Integer.parseInt(delStudent) + "' AND course_id = '" + Integer.parseInt(delCourse));
}
最佳答案
您没有在 Integer.parseInt(delCourse) 之后关闭单引号,这可能是一个问题。当然,为了更安全,您应该使用PreparedStatement(http://docs.oracle.com/javase/tutorial/jdbc/basics/prepared.html)并通过setInt传递整数值。
关于java - DELETE 查询未从数据库表中删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29752414/