[英]Groovy Sql to get the max value from a column
I have the following Groovy code to return the max value from the 'id' column from the 'topic' table: 我有以下Groovy代码从'topic'表中的'id'列返回最大值:
def rs = sql.executeQuery("select max(id) from topic")
def maxId = rs.getLong(1)
It doesn't work, I get the following error: 它不起作用,我收到以下错误:
java.sql.SQLException: Invalid column index at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:113) at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:147) at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:209)... java.sql.SQLException:oracle.jdbc.driver上的oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:113)处的oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:147)中的列索引无效。 DatabaseError.throwSqlException(DatabaseError.java:209)...
Does any one know what the correct code is? 有谁知道正确的代码是什么? Thanks. 谢谢。
I think it would be easier if you'd use the method firstRow
. 我认为如果你使用firstRow
方法会更容易。 You can either get the value from the result object by name or by index. 您可以通过名称或索引从结果对象中获取值。
by name: 按名字:
def row = sql.firstRow("select max(id) as max from topic")
def maxId = row.max
by index: 按索引:
def row = sql.firstRow("select max(id) from topic")
def maxId = row[0]
Nobody seems to have mentioned that the index in rs.getLong(1)
is out of bounds. 似乎没有人提到rs.getLong(1)
中的索引超出范围。 Getting fields uses a starting index of 0. Binding fields uses a starting index of 1. Why? 获取字段使用起始索引0. 绑定字段使用起始索引1.为什么? Historical SQL behaviour. 历史SQL行为。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.