我在 servlet 上收到两个参数,我需要将它们作为字符串放入,以便PreparedStatement
在setInt(1, parameter)
.
public class rate extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
response.setContentType("text/html;charset=UTF-8");
PreparedStatement pstmt = null;
Connection con = null;
//FileItem f1;
String id = request.getParameter("idbook");
String opcoes = request.getParameter("voto");
int idlivro=Integer.parseInt(id);
int opcao = Integer.parseInt(opcoes);
String updateString = "Update rating set livros_rate = ? where productId = ?";
if (idlivro != 0)
{
try {
//connect to DB
con = login.ConnectionManager.getConnection();
pstmt = con.prepareStatement(updateString);
pstmt.setInt(1, opcao);
pstmt.setInt(2, idlivro);
pstmt.executeUpdate();
}
catch (Exception e){
}finally{
try {
pstmt.close();
con.close();
} catch (SQLException ex) {
Logger.getLogger(rate.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
但是,它会引发以下异常:
java.lang.NumberFormatException: null
at java.lang.Integer.parseInt(Integer.java:454)
at java.lang.Integer.parseInt(Integer.java:527)
at counter.rate.doPost(rate.java:45)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728
这是如何引起的,我该如何解决?
编辑:我将把发送参数的表单代码。
<div id="templatemo_content_right">
<div class="templatemo_product_box">
<h1><%=rs.getString(3)%> <span>(<%=rs.getString(7)%>)</span></h1>
<img src="<%=rs.getString(13)%>"/>
<div class="product_info">
<p><%=rs.getString(10)%></p>
<div><form action="rate" method="POST">
<imput type="hidden" name="idbook" value="<%=rs.getString(1)%>"/>
<select name="voto">
<option value="0">Did not like</option>
<option value="1">Ok</option>
<option value="2" selected="selected">Liked</option>
<option value="3">Loved!</option>
</select>
<input type="submit" value="Votar!"/>
</form></div>
是导致问题的形式吗?