Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the hostname from a DataSource class

Tags:

Is there a way to get the hostname from the DataSource class in java? I mean, I have a DataSource object (DS), which is annotated to get the Jboss datasource. Anyway, I want to get the hostname used in that DS.

Debugging, i can see it this way: I get the Connection from DS, then I get the DataSourceMetaData and inside of that the is something called Protocol Connection which have the hostname, but I don't know how to get it.

Anyone here knows how? or another way to get the hostname? Thanks in advance. Kind regards, RDAM

like image 419
Ron Avatar asked Jun 14 '11 08:06

Ron


People also ask

How do I get a connection from DataSource?

Use the DataSource. getConnection method to obtain the connection. You can use one of the following forms of the getConnection method: getConnection(); getConnection(String user, String password);

What is DataSource name in JDBC?

In this case, billingDB is the logical name that is given to the BasicDataSource object ds . The subcontext jdbc is reserved for logical names to be bound to DataSource objects, so jdbc will always be the first part of a logical name for a data source.

What is DataSource class in Java?

A DataSource object is the representation of a data source in the Java programming language. In basic terms, a data source is a facility for storing data. It can be as sophisticated as a complex database for a large corporation or as simple as a file with rows and columns.


1 Answers

Once you have the DatabaseMetaData, just call the getURL() method which should contain the hostname like so:

dataSource.getConnection().getMetaData().getURL(); 
like image 157
Suraj Chandran Avatar answered Sep 26 '22 23:09

Suraj Chandran