zoukankan      html  css  js  c++  java
  • java.sql.SQLException: Column 'id' not found.

    发生这种错误的原因可能有多种情况。


    最近在写DAO层的时候,写了一个统计数据库中商品数量的方法,结果在调用的时候,发生了这个错误。于是看了下报的错误,找到了错误的根源:

    java.sql.SQLException: Column 'id' not found.
    	at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:129)
    	at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97)
    	at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:89)
    	at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:63)
    	at com.mysql.cj.jdbc.result.ResultSetImpl.findColumn(ResultSetImpl.java:561)
    	at com.mysql.cj.jdbc.result.ResultSetImpl.getInt(ResultSetImpl.java:825)
    	at com.dao.ProductDAOImpl.countQueriedProduct(ProductDAOImpl.java:119)
    	at com.dao.PageDAO.getTotalQueriedProductPage(PageDAO.java:91)
    	at com.servlet.ProductPageServlet.doPost(ProductPageServlet.java:30)
    	at javax.servlet.http.HttpServlet.service(HttpServlet.java:660)
    	at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
    	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:53)
    	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:202)
    	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
    	at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:490)
    	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)
    	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
    	at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:678)
    	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)
    	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
    	at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:408)
    	at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
    	at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:853)
    	at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1587)
    	at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
    	at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
    	at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
    	at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    	at java.base/java.lang.Thread.run(Thread.java:835)

    源代码如下:

    @Override
        public int countProduct() {
            String sql = "SELECT COUNT(id) FROM goods";
            int row = 0;
            try {
                this.connection = ConnectToDatabase.getConnection();
                preparedStatement = connection.prepareStatement(sql);
                resultSet = preparedStatement.executeQuery();
                resultSet.next();
                row = resultSet.getInt("id");
                return row;
            } catch (SQLException e) {
                e.printStackTrace();
            } finally {
                this.closeConnection();
            }
            return row;
        }

    错误之处发生在 row = resultSet.getInt("id") 那里,应该改为:

    row = resultSet.getInt(1);

    看一下SQL语句就知道为什么报错了。

    在命令行里输入SQL,结果是这样的,所以resultSet.setInt() 的参数应该写 1(第一行)。

    苟利国家生死以, 岂因祸福避趋之
  • 相关阅读:
    终于干掉了默认的输入法, 关于ctfmon.exe文件
    新工作一周的一些记录
    派送Maxthon 2.0 社区预览版本邀请.
    终于决定要跳槽了.
    当QA推出免责条款, 你会怎么看?
    Post by Word 2007, Test. 用word2007来发表一篇随笔.
    一个好的点菜系统
    再见2006
    昨天的实况以及忽悠姐妹花
    在公司我最近都只喝矿泉水
  • 原文地址:https://www.cnblogs.com/chintsai/p/11829233.html
Copyright © 2011-2022 走看看