BarryMcGillin
asked on
Getting db hostname and port number from a jdbc connection
Hi
I have a connection like this.
<code>
DriverManager.registerDriv er(new oracle.jdbc.OracleDriver() );
conn = DriverManager.getConnectio n(
"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.getDatabaseProduc tName();
String username = metaData.getUserName();
} catch (SQLException e) {
e1.printStackTrace();
}
</code>
I have a connection like this.
<code>
DriverManager.registerDriv
conn = DriverManager.getConnectio
"jdbc:oracle:thin:@devbox:
</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.getDatabaseProduc
String username = metaData.getUserName();
} catch (SQLException e) {
e1.printStackTrace();
}
</code>
Check this
http://www.princeton.edu/~storacle/jdbc8_doc/oracle.jdbc.pool.OracleDataSource.html
Looks like you can use
getDatabaseName()
and
getPortNumber()
http://www.princeton.edu/~storacle/jdbc8_doc/oracle.jdbc.pool.OracleDataSource.html
Looks like you can use
getDatabaseName()
and
getPortNumber()
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Hi
Following link will be useful to u.
http://xqzone.marklogic.com/pubs/3.0/javadoc/com/marklogic/xdbc/XDBCConnectionFactory.html
Following link will be useful to u.
http://xqzone.marklogic.com/pubs/3.0/javadoc/com/marklogic/xdbc/XDBCConnectionFactory.html
ASKER
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.MalformedURLExcep tion: unknown protocol: jdbc
at java.net.URL.<init>(URL.ja va:574)
at java.net.URL.<init>(URL.ja va:464)
at java.net.URL.<init>(URL.ja va:413)
</exception>
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.MalformedURLExcep
at java.net.URL.<init>(URL.ja
at java.net.URL.<init>(URL.ja
at java.net.URL.<init>(URL.ja
</exception>
java.net.URL is not for JDBC URLs. It is for web-like URLs that are on HTTP, etc.
hostname: devbox
port: 1521