java - 尝试读取 SQL 表时,参数索引超出范围(1 > 参数数量,即 0)

标签 java sql jdbc

因此,我尝试使用此 servlet 从 SQL 表中获取一些数据,并将其输出到 JSP 文件中。 我发现了一些与我的问题类似的文章,但没有一篇对我的情况有帮助。我希望这里有人可以帮助我。

我必须为我的一门课做这个项目。我基本上是在为一家服装网上商店编程。例如,如果我单击 T 恤,我希望此代码将它可以在 sql 表“T 恤”下找到的所有数据放入 JSP 文件中。

从其他人的错误中我了解到我的 SQL 查询是错误的。但我不明白为什么当我调用方法 display() 时它会将我的第 39 行标记为 false。

这是我的代码:

    @WebServlet("/DisplayClothes")
    public class DisplayClothes extends HttpServlet {
        private static final long serialVersionUID = 1L;


    public DisplayClothes() {
        super();

    }
    @Resource(lookup = "java:jboss/datasources/MySqlThidbDS")
    private DataSource ds;


    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        List<Clothes> Clothes= display();

        HttpSession session = request.getSession();
        session.setAttribute("Clothes", Clothes);

        final RequestDispatcher dispatcher = request.getRequestDispatcher("./JSP/ViewColthing.jsp");
        dispatcher.forward(request, response);
    }

    public List<Clothes> display() throws ServletException{


        List<Clothes> cl = new ArrayList<Clothes>();

        try (Connection con = ds.getConnection();
                 PreparedStatement pstmt = con.prepareStatement("SELECT * FROM clothes")) {

            pstmt.setInt(1, 1);

            try (ResultSet rs = pstmt.executeQuery()) {

                while (rs.next()) { 

                    Clothes clothes= new Clothes(); 
                    clothes.setId(Long.valueOf(rs.getInt("id")));
                    clothes.setName(rs.getString("name"));
                    clothes.setDes(rs.getString("description"));
                    clothes.price(rs.getFloat("preis"));
                    clothes.setSize(rs.getString("sizes"));
                    clothes.setQuantity(rs.getInt("quantity"));



                    cl.add(Clothes);
                }
            }
        } catch (Exception ex) {
            throw new ServletException(ex.getMessage());
        }

        return cl;
        }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        doGet(request, response);
    }

}

我收到此错误消息。

javax.servlet.ServletException: Parameter index out of range (1 > number of parameters, which is 0).
    at Servlets.DisplayClothes.display(DisplayClothes.java:76)
    at Servlets.DisplayClothes.doGet(DisplayClothes.java:39)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:503)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:590)
    at io.undertow.servlet.handlers.ServletHandler.handleRequest(ServletHandler.java:74)
    at io.undertow.servlet.handlers.security.ServletSecurityRoleHandler.handleRequest(ServletSecurityRoleHandler.java:62)
    at io.undertow.servlet.handlers.ServletChain$1.handleRequest(ServletChain.java:68)
    at io.undertow.servlet.handlers.ServletDispatchingHandler.handleRequest(ServletDispatchingHandler.java:36)
    at org.wildfly.extension.undertow.security.SecurityContextAssociationHandler.handleRequest(SecurityContextAssociationHandler.java:78)
    at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
    at io.undertow.servlet.handlers.RedirectDirHandler.handleRequest(RedirectDirHandler.java:68)
    at io.undertow.servlet.handlers.security.SSLInformationAssociationHandler.handleRequest(SSLInformationAssociationHandler.java:132)
    at io.undertow.servlet.handlers.security.ServletAuthenticationCallHandler.handleRequest(ServletAuthenticationCallHandler.java:57)
    at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
    at io.undertow.security.handlers.AbstractConfidentialityHandler.handleRequest(AbstractConfidentialityHandler.java:46)
    at io.undertow.servlet.handlers.security.ServletConfidentialityConstraintHandler.handleRequest(ServletConfidentialityConstraintHandler.java:64)
    at io.undertow.security.handlers.AuthenticationMechanismsHandler.handleRequest(AuthenticationMechanismsHandler.java:60)
    at io.undertow.servlet.handlers.security.CachedAuthenticatedSessionHandler.handleRequest(CachedAuthenticatedSessionHandler.java:77)
    at io.undertow.security.handlers.NotificationReceiverHandler.handleRequest(NotificationReceiverHandler.java:50)
    at io.undertow.security.handlers.AbstractSecurityContextAssociationHandler.handleRequest(AbstractSecurityContextAssociationHandler.java:43)
    at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
    at org.wildfly.extension.undertow.security.jacc.JACCContextIdHandler.handleRequest(JACCContextIdHandler.java:61)
    at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
    at org.wildfly.extension.undertow.deployment.GlobalRequestControllerHandler.handleRequest(GlobalRequestControllerHandler.java:68)
    at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
    at io.undertow.servlet.handlers.ServletInitialHandler.handleFirstRequest(ServletInitialHandler.java:269)
    at io.undertow.servlet.handlers.ServletInitialHandler.access$100(ServletInitialHandler.java:78)
    at io.undertow.servlet.handlers.ServletInitialHandler$2.call(ServletInitialHandler.java:133)
    at io.undertow.servlet.handlers.ServletInitialHandler$2.call(ServletInitialHandler.java:130)
    at io.undertow.servlet.core.ServletRequestContextThreadSetupAction$1.call(ServletRequestContextThreadSetupAction.java:48)
    at io.undertow.servlet.core.ContextClassLoaderSetupAction$1.call(ContextClassLoaderSetupAction.java:43)
    at org.wildfly.extension.undertow.security.SecurityContextThreadSetupAction.lambda$create$0(SecurityContextThreadSetupAction.java:105)
    at org.wildfly.extension.undertow.deployment.UndertowDeploymentInfoService$UndertowThreadSetupAction.lambda$create$0(UndertowDeploymentInfoService.java:1504)
    at org.wildfly.extension.undertow.deployment.UndertowDeploymentInfoService$UndertowThreadSetupAction.lambda$create$0(UndertowDeploymentInfoService.java:1504)
    at org.wildfly.extension.undertow.deployment.UndertowDeploymentInfoService$UndertowThreadSetupAction.lambda$create$0(UndertowDeploymentInfoService.java:1504)
    at org.wildfly.extension.undertow.deployment.UndertowDeploymentInfoService$UndertowThreadSetupAction.lambda$create$0(UndertowDeploymentInfoService.java:1504)
    at io.undertow.servlet.handlers.ServletInitialHandler.dispatchRequest(ServletInitialHandler.java:249)
    at io.undertow.servlet.handlers.ServletInitialHandler.access$000(ServletInitialHandler.java:78)
    at io.undertow.servlet.handlers.ServletInitialHandler$1.handleRequest(ServletInitialHandler.java:99)
    at io.undertow.server.Connectors.executeRootHandler(Connectors.java:376)
    at io.undertow.server.HttpServerExchange$1.run(HttpServerExchange.java:830)
    at org.jboss.threads.ContextClassLoaderSavingRunnable.run(ContextClassLoaderSavingRunnable.java:35)
    at org.jboss.threads.EnhancedQueueExecutor.safeRun(EnhancedQueueExecutor.java:1982)
    at org.jboss.threads.EnhancedQueueExecutor$ThreadBody.doRunTask(EnhancedQueueExecutor.java:1486)
    at org.jboss.threads.EnhancedQueueExecutor$ThreadBody.run(EnhancedQueueExecutor.java:1377)
    at java.lang.Thread.run(Thread.java:748)

最佳答案

您的查询中没有绑定(bind)参数。删除

pstmt.setInt(1, 1);

没有什么可设置的。另外,

cl.add(Clothes);

应该是

cl.add(clothes);

关于java - 尝试读取 SQL 表时,参数索引超出范围(1 > 参数数量,即 0),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62105572/

相关文章:

java - Hibernate SQL 查询结果映射/转换为对象/类/Bean

c# - Entity Framework - 如何检查表是否存在?

android - 适用于 Android 的 SQLDroid JDBC 驱动程序

mysql - 如何转义mysql jdbc连接字符串中的特殊字符

java - 我应该同步 DataSource 的 getConnection() 吗

java - 将字符串数组值直接传递给 setter 方法

java - 在 Java 中创建自定义 JButton

sql - 交换到以空白表结束的列

java.lang.NullPointerException 错误 - Intellij IDEA、Selenium、Java

mysql - NULL 或空字符串更有效/更自然?