当用Statement替换时,预备语句不起作用?

时间:2014-03-19 07:04:37

标签: java mysql jdbc

下面是我的代码,我正在使用JDBC并使查询运行得更快,将Statement替换为PrepareStatement,但页面不是woking。我正在做什么错误?

代码:

<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>

我的罐子是:

antlr.jar
java-image-scaling
mysql-connecotr-java 2.0.14

错误:

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)

控制台内显示以下错误。任何人都可以告诉错误是什么?

2 个答案:

答案 0 :(得分:4)

是 - 你的演员表会失败(有例外),然后你无条件地stmt.close()块中调用finally,即使{{1仍然是null ...因此stmt

基本上,不要这样做 - 使用NullPointerException代替创建con.prepareStatement(...)PreparedStatement根本没有创建con.createStatement(),因此稍后再投放它并不会起作用。如果确实不想使用PreparedStatement,只需将prepareStatement变量更改为stmt类型而不是Statement - 但我个人建议到处使用PreparedStatement

如果你正在使用Java 7,你还应该在调用PreparedStatement之前改进你的finally块以检查null,或者如果你正在使用Java 7,那么(更好)使用try-with-resources语句。

答案 1 :(得分:0)

在您使用的代码中

(PreparedStatement)con.createStatement();

返回Statement对象。

你需要

con.prepareStatement("query");

获取PreparedStatement对象