Below is my code where i am using JDBC and to make the query run faster am replacing Statement with PrepareStatement but the page is not woking.what is the wrong i am doing??
code:
<select name="city" onchange="Consituteshow(this.value)" required="required">
<option value="">Select State</option>
<%
PreparedStatement stmt=null;
DBconnection db=new DBconnection();
Connection con=db.dbConn();
try{
stmt = (PreparedStatement)con.createStatement();
ResultSet rs = stmt.executeQuery("select distinct StateID,State from election_history;");
while(rs.next())
{
%>
<option value="<%=rs.getString(2)%>"><%=rs.getString(2)%></option>
<%
}
}
catch(SQLException e){
e.printStackTrace();
}
finally{
con.close();
stmt.close();
}
%>
</select>
my jars are:
antlr.jar
java-image-scaling
mysql-connecotr-java 2.0.14
Error:
java.lang.NullPointerException
at org.apache.jsp.election_005fresults_jsp._jspService(election_005fresults_jsp.java:267)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:374)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:342)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:267)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:849)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:454)
at java.lang.Thread.run(Unknown Source)
the following error is displayed inside console .can anyone tell what is the error??
Yes — your cast will fail (with an exception), and then you're unconditionally calling stmt.close()
in the finally
block, even though stmt
is still null... hence the NullPointerException
.
Basically, don't do this — use con.prepareStatement(...)
to create a PreparedStatement
instead. con.createStatement()
simply doesn't create a PreparedStatement
, so just casting it later isn't going to work. If you really don't want to use prepareStatement
, just change your stmt
variable to be of type Statement
instead of PreparedStatement
— but personally I'd recommend using PreparedStatement
everywhere.
You should also either improve your finally block to check for null before calling close
, or (better) use a try-with-resources statement if you're using Java 7.
See more on this question at Stackoverflow