PreparedStatement
에는 setInt(1, parameter)
을 사용할 수 있도록 서블릿에 문자열로 넣어야하는 두 개의 매개 변수가 있습니다.서블릿 java.lang.NumberFormatException
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
어떻게 이런 일이 발생하고 내가 그것을 어떻게 해결할 수 :
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);
}
}
}
}
그러나, 다음과 같은 예외를 throw?
편집 : 매개 변수가 전송되는 양식의 코드를 넣을 것입니다.
<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'이라는 요청 매개 변수가 없다는 것입니다. 구문 분석하려고하면 null이며 따라서 'NumberFormatException'을 throw합니다. –
디버거를 사용하여 인수가 'null'인지 확인하십시오. –