Solved

Getting db hostname and port number from a jdbc connection

Posted on 2006-06-26
6
1,006 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
Comment Utility
The hostname and port are in the connection string:

hostname: devbox
port: 1521
0
 
LVL 14

Expert Comment

by:sathyagiri
Comment Utility
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:
mayankeagle earned 500 total points
Comment Utility
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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 2

Expert Comment

by:pankilmpatel
Comment Utility
0
 
LVL 2

Author Comment

by:BarryMcGillin
Comment Utility
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:mayankeagle
Comment Utility
java.net.URL is not for JDBC URLs. It is for web-like URLs that are on HTTP, etc.
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
hash value 2 38
json example 39 110
Which is the latest version of Apache Tomcatl 3 35
Java / Linux and Regular Expressions 11 42
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…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

762 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

8 Experts available now in Live!

Get 1:1 Help Now