Get the hostname from a DataSource class

19,499

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

dataSource.getConnection().getMetaData().getURL();
Share:
19,499

Related videos on Youtube

Ron
Author by

Ron

Software Engineer

Updated on June 14, 2020

Comments

  • Ron
    Ron almost 4 years

    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

  • Ron
    Ron almost 13 years
    it contains the hostname but in a jdbc URL String format, which is not the way to get it. Is a bad practice to depend on parsing a database URL to get a hostname. What if the URL has no hostname? What if you use different databases? URLs are in different format. That's why I'm looking the hostname directly
  • Suraj Chandran
    Suraj Chandran almost 13 years
    i doubt whether u can..the reason is that the both DataSource and Connection are abstraction that dont know(dont need to know) the concept of hostname. All it knows is how to get a connection and how to call queries
  • Ron
    Ron almost 13 years
    Yes they are meant into that concept. But picture my problem. I'm running -pg_dumps- (Many performance reasons to do that but that's other subject), but the jboss is not always in the same machine as the databaServer, so I need to pass the hostname parameter, and using the DS is the best way I think. Cause you know for sure where the database is
  • Suraj Chandran
    Suraj Chandran almost 13 years
    @RDAM, i am not sure i get what u mean. why not just have the db details configured in files, where everyone can read it. we even had a condition where all confiugrations of all DBs where stored in a central DB
  • Ron
    Ron almost 13 years
    Great! I have configuration parameters inside a database. I never pictured that as another configuration parameter #Mybad. Thank u very much for your help @Suraj
  • Peter Perháč
    Peter Perháč almost 12 years
    @SurajChandran mate, as to "both DataSource and Connection are abstraction that dont know(dont need to know) the concept of hostname" - if a Connection does not need to know the concept of hostname then I don't know what does... otherwise you're quite right and thanks for the helpful answer!
  • Nelda.techspiress
    Nelda.techspiress about 8 years
    The real problem is that the metadata is not available unless the connection is successful. If getConnection() returns null there is no metadata to be gotten and an exception will be thrown. So the question still stands is there anyway to get the innards of the dataSourceProperties table that is a part of the datasource object? I can see it in the debugger - just can't get to it from any class public methods.
  • andresp
    andresp over 3 years
    Also, the hostname might not be even a concept depending on the socket factory. For example for CloudSQL Postgres you would typically pass the database instance as the data source property "cloudSqlInstance", so you wouldn't be able to obtain this information from the url itself.