• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Oracle Connection from JDBC?

 
Dave Barnes
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi I am trying to get the underlying oracle connection from a jdbc connection. To do this i guess i need to get an oracle.jdbc.connection and call the unwrap method

what i have currently is:

Class.forName(oracle.jdbc.driver.OracleDriver)

java.sql.connection connection = DriverManager.getConnection(a,b,c)

How do i cast this connection to an oracle connection?

What i want to do to get the underlying connection is:

if (connection.isWrapperFor(OracleConnection.class)){
OracleConnection oracleConnection= connection.unwrap(OracleConnection.class);
}else{
// recover, not an oracle connection
}
.
 
Rob Spoor
Sheriff
Pie
Posts: 20749
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why would you need to cast it? The object the java.sql.Connection refers to already is a connection to your Oracle database (and probably an instance of OracleConnection). What does the OracleConnection class offer that the java.sql.Connection interface doesn't?
 
Dave Barnes
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The reason it needs casting is because I am trying to get a connection to the oracle jms queue, in order to do this the connection needs to be a native connection
 
Martin Vajsar
Sheriff
Posts: 3752
62
Chrome Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You (usually) only need to "unwrap" the connection if you obtained it from a connection pool (or some other indirect method). When you obtain the connection directly from DriverManager, it's already "the connection", as Rob has said before. You only need to cast it.
 
Rob Spoor
Sheriff
Pie
Posts: 20749
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The question is, why would you need to?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic