在其他数据库语言中,您可以构造查询以定义表的实例,例如:
SELECT a.foo, a.bar FROM ACME a WHERE a.ID=1;
然后执行类似的操作
rs.getInt("a.foo");
获取此表第一行的值。我在 hsql 中执行这个看似基本的功能时遇到了麻烦,想知道是否有简单的解决方案或者其他人是否遇到过这个问题。如果我执行上述查询并尝试访问“a.foo”,我会使用 hsql 收到以下错误:
org.hsqldb.jdbc.Util.sqlException(Unknown Source)
org.hsqldb.jdbc.jdbcResultSet.findColumn(Unknown Source)
org.hsqldb.jdbc.jdbcResultSet.getInt(Unknown Source)
但是,如果我将访问权限更改为
rs.getInt("foo");
一切正常。是什么赋予了?以这种方式定义查询时,为什么 hsql 难以检索“a.foo”?这一系列命令在 MySQL 中运行良好。
最佳答案
我不知道其他 dbms 的行为如何使用这种语法,但 hsqldb 的行为似乎与
的规范相匹配 int java.sql.ResultSet.getInt(String columnLabel) throws SQLException
Retrieves the value of the designated column in the current row of this ResultSet object as an int in the Java programming language.
参数:
columnLabel the label for the column specified with the SQL AS clause. If the SQL AS clause was not specified, then the label is the name of the column Returns:
在您的查询中,**a.fo**
代表 fo
列。它不是别名。如果你想使用别名,你可以试试:
SELECT a.foo as 'myFoo', a.bar FROM ACME a WHERE a.ID=1;
然后得到这样的值:
rs.getInt("myFoo");
关于java - hsql Java表列访问问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41024995/