Even Even - 2 years ago 176
Java Question

How to get sqlserver database name from datasource name in Java

As specified in the title, I want to get the database name in sqlserver, all info I know is datasource name, login name/password to get the Connection object, please show some pointers on how to retrieve the database name correctly, in java, thanks!


Answer Source

Obtain an instance of java.sql.DatabaseMetaData from the connection object.

The names of database can be obtained via getCatalogs() or getSchemas() method (It depends upon the vendor of JDBC driver).

ResultSet rs=cn.getMetaData().getSchemas();
while(rs.next()) {

Or use Connection.getCatalog() or Connection.getSchema() method.

In case if you are interested to get host name or ip address of the Oracle database server.

 ResultSet rs=st.executeQuery("select UTL_INADDR.GET_HOST_NAME from dual");
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download