【发布时间】:2014-03-05 07:49:06
【问题描述】:
我正在尝试从新创建的表中获取列列表(它是在 java 代码中创建的)。 问题是我没有得到这些列。 该代码适用于已经在数据库中的表,但是如果我创建一个新表并尝试立即获取列信息,它不会找到任何...
更新: 这是我用于测试的完整代码:
@Test
public void testtest() throws Exception {
try (Connection conn = dataSource.getConnection()) {
String tableName = "Table_" + UUID.randomUUID().toString().replace("-", "");
try (Statement statement = conn.createStatement()) {
statement.executeUpdate(String.format("create table %s (id int primary key,name varchar(30));", tableName));
}
DatabaseMetaData metaData = conn.getMetaData();
try (ResultSet rs = metaData.getColumns(null, null, tableName, null)) {
int colsFound = 0;
while (rs.next()) {
colsFound++;
}
System.out.println(String.format("Found %s cols.", colsFound));
}
System.out.println(String.format("Autocommit is set to %s.", conn.getAutoCommit()));
}
}
和输出:
Found 0 cols.
Autocommit is set to true.
【问题讨论】:
-
创建新表后是否关闭连接?
-
是的,我关闭了两个:语句和连接。虽然我使用 DriverManagerDataSource 来获取连接所以它可能是它没有关闭......
标签: java postgresql jdbc