1

I am wondering how can I get the database instance name from DatabaseMetaData, or is there any way I can do that, kind of stuck here, thanks for any pointers!

How to get sqlserver database name from datasource name in Java

Even

2
  • Could you please clearify what you would like to achieve? It is a little unclear to me what you mean by "database instance". I assume you've got the DatabaseMetaData via the Connection, which represents a connection to the database. You can do all the manipulation you'd like on the database via the Connection instance. However, it is unclear to me what you would like to achieve? Commented Aug 19, 2011 at 9:44
  • possible duplicate of How to get sqlserver database name from datasource name in Java Commented Sep 26, 2011 at 7:12

1 Answer 1

0

Once you've obtained a database connection:

// url is usually in the format jdbc:mysql://<ip address>:<port>/<database name>    
String url = theConnection..getMetaData().getURL();
String databaseName = StringUtils.substringAfterLast(url, "/");

The above code uses StringUtils in Apache Commons Lang 3

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.