0

I use databasemetadata to find the column size. But getColumns(null,null,"table_name",null) returns an empty resultset. I checked for the table by querying it and the table is present. Where is the error? Thanks in advance!

Update:

Connection connection = getConnection(); //getting the connection -   
Statement st = connection.createStatement();  
ResultSet rs = st.executeQuery("select * from table_name");  
while (rs.next()) { 
   System.out.println(rs.getString("column_name")); //The values get printed 
}
ResultSet rsColumns = null;
DatabaseMetaData meta = connection.getMetaData();
rsColumns = meta.getColumns(null, null, "table_name",null);     
System.out.println(rsColumns.next()); // I get false here which means resultset is empty. But I dunno why.
jmj
  • 237,923
  • 42
  • 401
  • 438
Abhi
  • 1
  • 2

2 Answers2

0

Which database are you using? It's probably related to how it handle unquoted identifiers, databases that follow the standard automatically store them in upper case. You can programmatically check if it does that by calling the storesUpperCaseIdentifiers method.

The solution would be to either use quoted identifiers while creating the table or calling the toUpperCase method on the table name string if storesUpperCaseIdentifiers returns true.

Nicolas Buduroi
  • 3,565
  • 1
  • 28
  • 29
0

Can you try the below snippet:

ResultSet rsColumns = null;
DatabaseMetaData meta = connection.getMetaData();
rsColumns = meta.getColumns(null, null, "table_name",null);     
While (rsColumns,next()) {
System.out.println(rsColumns.getInt("COLUMN_SIZE"));
}

I think you are missing the "COLUMN_SIZE".

ABC
  • 354
  • 2
  • 3
  • 13