我在servlet上收到兩個參數,我需要把它們放在字符串中,以便在PreparedStatement
中我可以使用setInt(1, parameter)
。Servlet java.lang.NumberFormatException
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>
是造成問題的形式?
最有可能發生的事情是您沒有名爲'idbook'的請求參數。當你試圖解析它時,它是空的,因此拋出'NumberFormatException'。 – 2013-02-19 15:56:03
請使用調試器來檢查是否有任何參數爲空。 – 2013-02-19 15:56:46