英文:
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("/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);
}
}
And I get This Error message.
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)
答案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);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论