Solved

Getting db hostname and port number from a jdbc connection

Posted on 2006-06-26
6
1,022 Views
Last Modified: 2008-02-01
Hi
I have a connection like this.
<code>
    DriverManager.registerDriver(new oracle.jdbc.OracleDriver());
         conn = DriverManager.getConnection(
            "jdbc:oracle:thin:@devbox:1521:UTF", "test", "test");
</code>

and I want to find out the following information from the connection.   I can get some of the information I need, but not all of it and the javadocs are not much help getting this out.  I've looked at INetAddress, but this seems to give local hostnames, not the hostname and the port number of the database I am connected to.
<code>
   try {
        DatabaseMetadata metaData = conn.getMetaData();
        String URL metaData.getURL();
        String hostname = /* How do we do this? */
        int port = /* how do we do this? */
        String DBType = metaData.getDatabaseProductName();
        String username = metaData.getUserName();
       } catch (SQLException e) {
                e1.printStackTrace();
       }
</code>
0
Comment
Question by:BarryMcGillin
6 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 16986247
The hostname and port are in the connection string:

hostname: devbox
port: 1521
0
 
LVL 14

Expert Comment

by:sathyagiri
ID: 16986441
Check this
http://www.princeton.edu/~storacle/jdbc8_doc/oracle.jdbc.pool.OracleDataSource.html

Looks like you can use
 getDatabaseName()

and
getPortNumber()
0
 
LVL 30

Accepted Solution

by:
Mayank S earned 500 total points
ID: 16989563
Yes but that would work only for Oracle, not for other DBs - there is no generic way to do this, unless you want to get your hands dirty with it and just parse the connection string/ URL to obtain the values (but again, the values will be in a different format in the connection-string depending on the DB/ driver).
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 2

Expert Comment

by:pankilmpatel
ID: 16990970
0
 
LVL 2

Author Comment

by:BarryMcGillin
ID: 16993316
These dont seem to work.  I'm wondering why this doesn't work either.
If we get back a URL, surely then creating a standard java.net.URL will be able to pull it out.
<code>
 try {
           connURL = new URL(metaData.getURL());
      } catch (MalformedURLException e) {
          e.printStackTrace();
      }
       String host = connURL.getHost();
       int port = connURL.getPort());
</code>
Unfortunately I'm getting this.....
<exception>
java.net.MalformedURLException: unknown protocol: jdbc
      at java.net.URL.<init>(URL.java:574)
      at java.net.URL.<init>(URL.java:464)
      at java.net.URL.<init>(URL.java:413)
</exception>
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 16999438
java.net.URL is not for JDBC URLs. It is for web-like URLs that are on HTTP, etc.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

26 Experts available now in Live!

Get 1:1 Help Now