Solved

Getting db hostname and port number from a jdbc connection

Posted on 2006-06-26
6
1,089 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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This video teaches viewers about errors in exception handling.
Suggested Courses

617 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