I need to connect to a remote database using Database link using JDBC commands. How can it be done?
-
I am using Oracle DB, I tried to connect to it by using the getConnection() method, but I need to use an already created db link to access the remote database. – Nigel Thomas Jun 20 '12 at 12:48
3 Answers
If you already have the dblink setup, you can utilize it in your SQL (sent via jdbc) by addressing the required tables like such:
select * from SCHEMA.TABLE@DBLINK_NAME
Using this query inside of your java would look something like this
public ResultSet execQuery() throws SQLException, ClassNotFoundException{
//Load the database driver
Class.forName("oracle.jdbc.OracleDriver");
//Create connection to the database
Connection myConnection = DriverManager.getConnection(connectURL,userName,userPwd);
//Create a statement link to the database for running queries
Statement myQuery = myConnection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
//Create a resultSet to hold the returned query information
ResultSet myQueryResults = myQuery.executeQuery("select * from SCHEMA.TABLE@DBLINK_NAME");
return myQueryResults;
}
*java & oracle assumed

- 88
- 6
If you are asking about how to use JDBC to create a link between the DB you are talking to and another one, then it is "just SQL" that you (presumably) would execute in the same way as you would any other SQL statement. (If you tell us which DB you are using, we could talk about the actual SQL you need to execute.)
Otherwise, I don't think this makes sense. A DB link / Database link is a link from one database to another. But JDBC is for talking to a database from a Java client. It makes no sense (to me) to use DB link to connect a JDBC client to a database.

- 698,415
- 94
- 811
- 1,216
-
I have created a DB link and tried to connect to the remote database from the java client. It was working. Now I need to create a DB link inside the client and send SQL statements from the client itself. Both the local and remote database is Oracle Database. – Nigel Thomas Jun 20 '12 at 13:10
Please take a look at orajdbclink, on sourceforge
I am planning to to connect my oracle plsql sources to phoenix skin of hbase. It seems to me the unique way to create a connector between oracle and hbase for the moment...

- 87
- 4