Solved

Getting db hostname and port number from a jdbc connection

Posted on 2006-06-26
6
1,061 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
maven module vs maven project 3 73
Detect Closed Loops (circles, figure-8s, etc) in PNG Images 6 74
American Express @Work site and Java 4 63
Java array 21 80
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

761 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