Question

I need to connect to a remote database using Database link using JDBC commands. How can it be done?

Was it helpful?

Solution

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

OTHER TIPS

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.

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...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top