gpt4 book ai didi

java - H2 数据库 ALIAS_COLUMN_NAME=TRUE 似乎不起作用

转载 作者:行者123 更新时间:2023-12-04 05:28:04 24 4
gpt4 key购买 nike

将 ALIAS_COLUMN_NAME=TRUE 添加到 JDBC url 应该意味着 h2 允许在列名中使用“别名”,即您可以这样做:

resultSet.getString("p.first_name")

如果 p 是某个表的别名。如这段代码所示,这似乎对我不起作用:

package com.example;

import junit.framework.TestCase;
import org.apache.commons.dbcp.BasicDataSource;
import org.h2.Driver;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowCallbackHandler;
import org.springframework.jdbc.core.simple.SimpleJdbcInsert;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;

/**
* H2 Spring Test
*/
public class H2SelectTest extends TestCase {
public void testQuery() throws Exception {
BasicDataSource dataSource = new BasicDataSource();
dataSource.setDriverClassName(Driver.class.getName());
dataSource.setUrl("jdbc:h2:mem:test;ALIAS_COLUMN_NAME=TRUE");
dataSource.setUsername("sa");
dataSource.setPassword("");
JdbcTemplate template = new JdbcTemplate(dataSource);
template.afterPropertiesSet();
template.execute("create table people(id int auto_increment, first_name varchar);");
SimpleJdbcInsert insert = new SimpleJdbcInsert(template).withTableName("people");
insert.setGeneratedKeyName("id");
insert.compile();
HashMap<String, Object> map = new HashMap<String, Object>();
map.put("first_name", "Bob");
insert.execute(map);
template.query("select p.first_name from people p", new RowCallbackHandler() {
public void processRow(ResultSet rs) throws SQLException {
String name = rs.getString("p.first_name");
}
});
}
}

导致此错误的原因:

Caused by: org.h2.jdbc.JdbcSQLException: Column "p.first_name" not found [42122-168]
at org.h2.message.DbException.getJdbcSQLException(DbException.java:329)
at org.h2.message.DbException.get(DbException.java:169)
at org.h2.message.DbException.get(DbException.java:146)
at org.h2.jdbc.JdbcResultSet.getColumnIndex(JdbcResultSet.java:2918)
at org.h2.jdbc.JdbcResultSet.get(JdbcResultSet.java:2979)
at org.h2.jdbc.JdbcResultSet.getString(JdbcResultSet.java:291)
at org.apache.commons.dbcp.DelegatingResultSet.getString(DelegatingResultSet.java:225)
at com.example.H2SelectTest$1.processRow(H2SelectTest.java:35)
at org.springframework.jdbc.core.JdbcTemplate$RowCallbackHandlerResultSetExtractor.extractData(JdbcTemplate.java:1482)
at org.springframework.jdbc.core.JdbcTemplate$1QueryStatementCallback.doInStatement(JdbcTemplate.java:446)
at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:396)
... 19 more

最佳答案

这是您正在寻找的答案:

事实证明,根据这些文档,ALIAS_COLUMN_NAME 用于列名而不是表别名:

 /**
* System property <code>h2.aliasColumnName</code>.<br />
* When enabled, aliased columns (as in SELECT ID AS I FROM TEST) return the
* alias (I in this case) in ResultSetMetaData.getColumnName() and 'null' in
* getTableName(). If disabled, the real column name (ID in this case) and
* table name is returned. This setting only affects the default mode.
**/

还发现,使用表别名查询结果集只有MySQL支持,JDBC规范不支持。您可以使用 H2 中的完整表名来消除列歧义,例如resultSet.getString("people.first_name") 但我担心,如果您使用 h2 对您通常针对 MySQL 运行的代码进行内部测试,您将不得不找到另一种方法(例如,不要别名和使用完整的表名)。

如果您需要任何其他帮助,请通过我,给我留言。

关于java - H2 数据库 ALIAS_COLUMN_NAME=TRUE 似乎不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12959307/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com