Skip to content

Instantly share code, notes, and snippets.

@awendland
Created July 10, 2015 01:31
Show Gist options
  • Save awendland/be8399638a3b1a7a94c6 to your computer and use it in GitHub Desktop.
Save awendland/be8399638a3b1a7a94c6 to your computer and use it in GitHub Desktop.
Converts a java.sql.DatabaseMetaData object to a JSON friendly Map<String, Object>. This is intended for debugging running Java Web APIs
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Map<String, Object> getMapOfDatabaseMetaData(DatabaseMetaData dbMeta) {
Map<String, Object> databaseInfo = new HashMap<>();
databaseInfo.put("DatabaseMajorVersion", dbMeta.getDatabaseMajorVersion());
databaseInfo.put("DatabaseMinorVersion", dbMeta.getDatabaseMinorVersion());
databaseInfo.put("DatabaseProductName", dbMeta.getDatabaseProductName());
databaseInfo.put("DatabaseProductVersion", dbMeta.getDatabaseProductVersion());
databaseInfo.put("DriverMajorVersion", dbMeta.getDriverMajorVersion());
databaseInfo.put("DriverMinorVersion", dbMeta.getDriverMinorVersion());
databaseInfo.put("DriverName", dbMeta.getDriverName());
databaseInfo.put("DriverVersion", dbMeta.getDriverVersion());
databaseInfo.put("URL", dbMeta.getURL());
databaseInfo.put("UserName", dbMeta.getUserName());
List<String> schemas = new ArrayList<>();
ResultSet schemasRS = dbMeta.getSchemas();
while (schemasRS.next())
schemas.add(schemasRS.getString(1));
databaseInfo.put("Schemas", schemas);
List<String> tables = new ArrayList<>();
ResultSet tablesRS = dbMeta.getTables("", "", "", null);
while (tablesRS.next())
tables.add(tablesRS.getString(1));
databaseInfo.put("Tables", tables);
return databaseInfo;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment