我試圖從新創建的表(它是在java代碼中創建的)獲取列列表。 問題是我沒有得到列。 代碼工作對於那些已經在數據庫表中,但如果我創建一個新的並嘗試獲取列信息立刻沒有找到任何...從jdbc/postgresql獲取新創建表的列元數據
更新: 下面是我用全碼測試:
@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獲取連接,所以它可能是它沒有關閉... – jmac