org.postgresql.util.PSQLException:没有为参数 1 指定值。- Postgres

Posted

技术标签:

【中文标题】org.postgresql.util.PSQLException:没有为参数 1 指定值。- Postgres【英文标题】:org.postgresql.util.PSQLException: No value specified for parameter 1. - Postgres 【发布时间】:2017-09-10 14:55:51 【问题描述】:

我正在尝试运行preparedStatement,但出现以下异常。

org.postgresql.util.PSQLException: No value specified for parameter 1.
    at org.postgresql.core.v3.SimpleParameterList.checkAllParametersSet(SimpleParameterList.java:257)
    at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:284)
    at org.postgresql.jdbc.PgStatement.executeInternal(PgStatement.java:428)
    at org.postgresql.jdbc.PgStatement.execute(PgStatement.java:354)
    at org.postgresql.jdbc.PgPreparedStatement.executeWithFlags(PgPreparedStatement.java:169)
    at org.postgresql.jdbc.PgPreparedStatement.executeQuery(PgPreparedStatement.java:117)
    at org.apache.tomcat.dbcp.dbcp2.DelegatingPreparedStatement.executeQuery(DelegatingPreparedStatement.java:82)
    at org.apache.tomcat.dbcp.dbcp2.DelegatingPreparedStatement.executeQuery(DelegatingPreparedStatement.java:82)
    at com.eventory.dao.ManagerDao.isExists(ManagerDao.java:62)
    at com.eventory.create.web.MSignUp.doPost(MSignUp.java:44)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:661)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:198)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:478)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:80)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:650)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342)
    at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:799)
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1457)
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:748)
Sep 10, 2017 8:15:20 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [com.eventory.create.web.MSignUp] in context with path [/eventory] threw exception
java.lang.IllegalStateException: Cannot forward after response has been committed
    at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:321)
    at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:311)
    at com.eventory.create.web.MSignUp.doPost(MSignUp.java:52)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:661)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:198)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:478)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:80)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:650)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342)
    at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:799)
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1457)
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:748)

这是 ManagerDao 中导致异常的方法。

public static boolean isExists(String managerID) 
        boolean st = false;

        // sql = "SELECT managerID FROM ManagerLogin";
        try 
            pstmt = currentCon.prepareStatement(GET_MANAGER_PASSWORD);

            ResultSet rs = pstmt.executeQuery();
            while (rs.next()) 
                String originalMID = rs.getString("managerID");
                if (originalMID.equals(managerID)) 
                    st = true;
                
            
         catch (SQLException e) 
            // TODO Auto-generated catch block
            e.printStackTrace();
        
        return st;
    

preparedStatement 是

GET_MANAGER_PASSWORD = "SELECT pwd FROM ManagerLogin WHERE managerID=?";

当我使用 MS SQL 服务器时,它运行良好。我已经切换到 Postgres。查询有什么问题吗?请帮忙。

【问题讨论】:

信息非常清晰。你读过吗?你不明白什么?您是否为准备好的语句的第一个参数指定了值? 您的语句以经理 ID 作为参数,并返回密码。另一方面,您的 Java 不设置任何参数,并期望得到一个经理 ID 作为回报。这对你有意义吗? 我试过 pstmt.setString(1, managerID);它仍然不起作用。它给出了同样的错误 【参考方案1】:

您没有在查询语句的任何地方传递参数managerID

应该是这样的:

pstmt = currentCon.prepareStatement(GET_MANAGER_PASSWORD);
pstmt.setString(1, managerID);
ResultSet rs = pstmt.executeQuery();
//Rest of your code

但是,正如 @RealSkeptic 的 cmets 提到的那样,您的其余代码没有多大意义

【讨论】:

我试过 pstmt.setString(1, managerID);它仍然不起作用。它给出了同样的错误

以上是关于org.postgresql.util.PSQLException:没有为参数 1 指定值。- Postgres的主要内容,如果未能解决你的问题,请参考以下文章