Take the 2-minute tour ×

Is it possible to get the oracle server port no from a sql query?


share|improve this question
I believe you're looking for the Listener port number, which is a separate process from the database itself. –  Rob Sep 30 '09 at 14:38

2 Answers 2

It doesn't seem to be directly possible: see the discussion here on askTom

share|improve this answer

No, but in order to run a query you're going to need a connection to the DB so I suspect you can get the port using a call to java.sql.DatabaseMetaData.getURL(). Not sure exactly what the Oracle drivers return but something like this should do the trick:

String url = connection.getMetadata().getURL();
String port = url.substring(url.indexOf(":"), url.indexof("/", url.indexOf(":")) - 1);
share|improve this answer
chicken and egg: how can you make a connection without knowing the port number? can't get meta data without it. –  duffymo Sep 30 '09 at 9:51
@duffymo: like I said, if you're going to run a query you must have a connection. If the question was how do I connect to some Oracle instance on the network somewhere then then agreed you're screwed and it's time to phone the DBAs and get some more information. –  Nick Holt Sep 30 '09 at 9:58
im creating a connection to the oracle database using the sun.jdbc.odbc.JdbcOdbc driver.Just require the DSN to create the connection.Once I have the connection I want to find out the port no on which the server is running.. –  Fell Sep 30 '09 at 11:22
@Fell: I've managed to avoid the JDBC bridge driver but surely it still needs the connection information host, port, SID, etc, which is returned as per the docs (java.sun.com/j2se/1.3/docs/guide/jdbc/getstart/bridge.doc.html)? If the port is missing then I suspect the DB is listening on the default port, 1521 I think for Oracle. –  Nick Holt Sep 30 '09 at 14:11

Your Answer


