サーブレットで2つのパラメーターを受け取ります。これらのパラメーターは、で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>
フォームが問題を引き起こしていますか?