I am trying to get all db tables using DatabaseMetaData.getTables() method. But this method requires database schema name pattern. Is it possible to get schema name for current db connection?
The standard schema for your current connection is the name of the user you use to log in. So if your user is SCOTT
you have to use SCOTT
for DatabaseMetaData.getTables()
.
You can obtain the username through DatabaseMetaData.getUserName()
.
But remember that the comparison of schema/username done in the JDBC driver is case-sensititve and normally usernames are in uppercase.
I am not 100% sure if DatabaseMetaData.getUserName()
will return the name in the correct case in all situations. To be sure, you might want to do an upperCase() before using that value.
Try to play with getCatalogs(). This is a quick draft
public List<String> getDatabases(DBEnv dbEnv) {
Connection conn = getConnection(dbEnv);
List<String> resultSet = new ArrayList<String>();
try {
DatabaseMetaData metaData = conn.getMetaData();
ResultSet res = metaData.getCatalogs();
while (res.next()) {
resultSet.add(res.getString("TABLE_CAT"));
}
} catch (SQLException e) {
logger.error(e.toString());
}
return resultSet;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With