未找到 java.sql.sqlexception 列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21596834/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 09:34:29  来源:igfitidea点击:

java.sql.sqlexception column not found

javamysqlsqljdbc

提问by Dilini

I am trying to find max no from tr_id (primary key) in transaction table.
Here is the table and it's layout.
enter image description here

我正在尝试从事务表中的 tr_id(主键)中找到 max no。
这是桌子和它的布局。
在此处输入图片说明


enter image description here


在此处输入图片说明


Here is my cord.


这是我的绳子。

             try {
                ResultSet rs = db.getData("SELECT MAX(tr_id) FROM transaction");
                ResultSetMetaData meta = rs.getMetaData();
                for (int index = 1; index <= meta.getColumnCount(); index++) {
                    System.out.println("Column " + index + " is named " +    meta.getColumnName(index));
                }
                if (rs.first()) {
                    int tr_id = rs.getInt("tr_id");
               }


I'm using JDBC connection. When I'm running this cord I'm getting this error.


我正在使用 JDBC 连接。当我运行这条线时,我收到了这个错误。

        Column 1 is named MAX(tr_id)
        java.sql.SQLException: Column 'tr_id' not found.
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:910)
    at com.mysql.jdbc.ResultSet.findColumn(ResultSet.java:955)
    at com.mysql.jdbc.ResultSet.getInt(ResultSet.java:2570)
    at Controler.InvoiceFinalising.saveInvoice(InvoiceFinalising.java:57)
  etc..


The thing is when I'm searching out as "tr_id" column name goes to Max(tr_id)


问题是当我搜索“tr_id”列名到 Max(tr_id)

采纳答案by Sanjeev

This is because the column name in your sql query is max(tr_id). You can write it as

这是因为您的 sql 查询中的列名是 max(tr_id)。你可以把它写成

ResultSet rs = db.getData("SELECT MAX(tr_id) as tr_id FROM transaction");

now you will be able to get it.

现在你将能够得到它。