参数索引超出范围(参数数量为0,索引为1),尝试读取 SQL 表时发生错误。

huangapple go评论78阅读模式
英文:

Parameter index out of range (1 > number of parameters, which is 0) When trying to read SQL Table

问题

这是我翻译好的代码部分:

@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> clothesList = display();

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

        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")) {

            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.setPrice(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);
    }
}

如果你需要进一步的帮助,请随时提问。

英文:

So I am trying to get some data out of my SQL Table with this servlet and output it in a JSP file.
I found some articles with similar problems to mine but none of them helped in my case. I Hoped someone here can help me.

I have to do this Project for one of my classes. I am basically programming an online shop for Clothing. If I click on for example t-shirts, I want this code to put out all data it can find under the sql table "t-shirts" into a JSP file.

From what i learned from other peoples mistakes is that my SQL Query is wrong. But i dont understand why it marks me line 39 as false When I call the Method display().

So Here is my Code:

    @WebServlet(&quot;/DisplayClothes&quot;)
public class DisplayClothes extends HttpServlet {
private static final long serialVersionUID = 1L;
public DisplayClothes() {
super();
}
@Resource(lookup = &quot;java:jboss/datasources/MySqlThidbDS&quot;)
private DataSource ds;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
List&lt;Clothes&gt; Clothes= display();
HttpSession session = request.getSession();
session.setAttribute(&quot;Clothes&quot;, Clothes);
final RequestDispatcher dispatcher = request.getRequestDispatcher(&quot;./JSP/ViewColthing.jsp&quot;);
dispatcher.forward(request, response);
}
public List&lt;Clothes&gt; display() throws ServletException{
List&lt;Clothes&gt; cl = new ArrayList&lt;Clothes&gt;();
try (Connection con = ds.getConnection();
PreparedStatement pstmt = con.prepareStatement(&quot;SELECT * FROM clothes&quot;)) {
pstmt.setInt(1, 1);
try (ResultSet rs = pstmt.executeQuery()) {
while (rs.next()) { 
Clothes clothes= new Clothes(); 
clothes.setId(Long.valueOf(rs.getInt(&quot;id&quot;)));
clothes.setName(rs.getString(&quot;name&quot;));
clothes.setDes(rs.getString(&quot;description&quot;));
clothes.price(rs.getFloat(&quot;preis&quot;));
clothes.setSize(rs.getString(&quot;sizes&quot;));
clothes.setQuantity(rs.getInt(&quot;quantity&quot;));
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);
}
}

And I get This Error message.

javax.servlet.ServletException: Parameter index out of range (1 &gt; 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)

答案1

得分: 0

你的查询中没有绑定参数。移除

pstmt.setInt(1, 1);

没有需要设置的内容。另外,

cl.add(Clothes);

应该是

cl.add(clothes);
英文:

There are no bind parameters in your query. Remove

pstmt.setInt(1, 1);

There is nothing to set. Also,

cl.add(Clothes);

should be

cl.add(clothes);

huangapple
  • 本文由 发表于 2020年5月31日 00:37:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/62105572.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定