【问题标题】:Delete the Entry from the list从列表中删除条目
【发布时间】:2017-11-20 19:22:37
【问题描述】:

我已成功列出数据库中的条目,但我想在复选框的帮助下删除条目。因此,当有人选中该复选框时,应删除该特定条目。

我已经在参考的帮助下为其编写了代码,但我的车辆 ID 是整数,我正在以字符串形式编写代码,但是当我尝试转换为 Interger.parseInt 时,它不起作用。

这是我的代码:

Listing.Jsp

<TR>
<th>Vehicle ID </th>
<th> EzTagCode </th>
<th> Vehicle Type </th>
<th> Plate No </th>
<th> Model Name </th>
<th> Color </th>
</TR>
<TR>
<%
int i=0; while (rs.next()) {
%>
>
<%--<td><input type="checkbox" name="check<%=i%>" --%>
<TD><%=rs.getInt(1)%></TD>
<TD><%=rs.getString(3)%></TD>
<TD><%=rs.getString(4)%></TD>
<TD><%=rs.getString(5)%></TD>
<TD><%=rs.getString(6)%></TD>
<TD><%=rs.getString(7)%></TD>
</TR><%
i++;
%>
<% } %>
<%
// close all the connections.
rs.close();
st.close();
conn.close();
} catch (Exception ex) {
%>
</font>
<font size="+3" color="red"></b>
<%
out.println("Unable to connect to database.");
}
%>
</TABLE>
<input type="submit">

删除.jsp

<%String id[]= new String[10];
for(int i=0;i<10;i++){
id[i]=request.getParameter("check"+i);
int b = Integer.parseInt(id[i]);
out.println(id[i]);

%>
<%
try {

st=conn.createStatement();
for(int a=0;a<10;a++){
out.println("hello");
String QueryString = "delete from Vehicle where VehicleID='"+id[a]+"'";
rs = st.executeQuery(QueryString);

【问题讨论】:

  • 您需要将其更改为使用参数化查询。否则,黑客可以拦截请求并将 id 更改为 ' OR 1=1 -- 之类的东西,这将始终为 true 并删除数据库中的所有车辆。
  • 哦,我很感谢你的建议,但现在我们正在做一些学校项目,所以我们以后可以改变它

标签: database jsp crud


【解决方案1】:

在这里检查 request.getParameter("check"+i) 不是 int 值,这就是不生成输出的原因。

id[i]=request.getParameter("check"+i);
int b = Integer.parseInt(id[i]);

【讨论】:

    猜你喜欢
    • 2011-12-20
    • 1970-01-01
    • 1970-01-01
    • 2013-06-27
    • 2020-11-17
    • 2011-03-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多