- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
将 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/
今天有小伙伴给我留言问到,try{...}catch(){...}是什么意思?它用来干什么? 简单的说 他们是用来捕获异常的 下面我们通过一个例子来详细讲解下
我正在努力提高网站的可访问性,但我不知道如何在页脚中标记社交媒体链接列表。这些链接指向我在 facecook、twitter 等上的帐户。我不想用 role="navigation" 标记这些链接,因
说现在是 6 点,我有一个 Timer 并在 10 点安排了一个 TimerTask。之后,System DateTime 被其他服务(例如 ntp)调整为 9 点钟。我仍然希望我的 TimerTas
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我就废话不多说了,大家还是直接看代码吧~ ? 1
Maven系列1 1.什么是Maven? Maven是一个项目管理工具,它包含了一个对象模型。一组标准集合,一个依赖管理系统。和用来运行定义在生命周期阶段中插件目标和逻辑。 核心功能 Mav
我是一名优秀的程序员,十分优秀!