【发布时间】:2018-08-30 00:37:50
【问题描述】:
解决方案:只是在 HTML 参数中添加了 null 默认值。好像解决了!
所以我有一个包含两种表单的管理页面 (HTML)。第一种形式是添加到 SQL 数据库,有 4 个输入框,第二种形式是从 SQL 数据库中删除,有 1 个输入框。如果我使用第一种形式,一切正常。但是,如果我将第一个表单留空并只为第二个表单输入一个值,我会得到一个 String = "" 的 NumberFormatException 我只能假设,因为第一个表单是空的。我试图让用户填写第一个表格并将第二个表格留空,反之亦然。任何关于如何更好地解决这个问题的建议都会很棒。
这是我的 HTML 页面
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Admin</title>
<link rel="stylesheet" href="myStyle.css">
</head>
<body>
<h1>Welcome to the admin page.</h1>
<h2>Please enter the new product to be added:</h2>
<form action="eShop" method="Post">
<table class="tableBox">
<tr>
<td align="left"><b>Item Code:</b></td>
<td align="left"><input type="number" name="code"></td>
</tr>
<tr>
<td align="left"><b>Item Name:</b></td>
<td align="left"><input type="text" name="name"></td>
</tr>
<tr>
<td align="left"><b>Item Price:</b></td>
<td align="left"><input type="number" name="price"></td>
</tr>
<tr>
<td align="left"><b>Is Taxable:</b></td>
<td align="left"><input type="text" name="taxable"></td>
<tr>
<td align="center" colspan="2"><input type="submit" name="action" value="Update!"></td>
</tr>
</table>
</form>
<h2>Enter the code of the product you want to delete:</h2>
<form action="eShop" method="Post">
<table class="tableBox">
<tr>
<td align="left"><b>Item Code:</b></td>
<td align="left"><input type="number" name="code"></td>
</tr>
<tr>
<td align="center" colspan="2"><input type="submit" name="action" value="Delete!"></td>
</tr>
</table>
</form>
</body>
</html>
这是我的 servlet 中获取参数的部分
else if(request.getParameter("action").equals("Delete!")) {
DataAccessImpl temp = new DataAccessImpl();
int code = Integer.parseInt(request.getParameter("code"));
temp.deleteItem(code);
}
这是 servlet 中使用的 DataAccessImpl 类,特别是删除行的方法。
public void deleteItem(int code) {
connect();
String deleteStm = ("DELETE FROM ProductCatalogue WHERE code = " + code);
PreparedStatement pstm = null;
System.out.println("Beginning to delete product from database...");
try {
pstm = conn.prepareStatement(deleteStm);
System.out.println("Delete complete!");
}
catch(SQLException e) {
e.printStackTrace();
}
}
【问题讨论】:
标签: sql html servlets jakarta-ee