繁体   English   中英

找不到java.sql.sqlexception列

[英]java.sql.sqlexception column not found

我正在尝试从事务表的tr_id(主键)中找到最大编号。
这是表格及其布局。
在此处输入图片说明


在此处输入图片说明


这是我的线。

             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");
               }


我正在使用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..


事情是当我搜索“ tr_id”列名称为Max(tr_id)时

这是因为SQL查询中的列名称是max(tr_id)。 你可以写成

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

现在您将能够得到它。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM