java 如何使用JDBC通过数据库链接连接到远程数据库?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11119694/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to connect to remote database through DB link using JDBC?
提问by Nigel Thomas
I need to connect to a remote database using Database link using JDBC commands. How can it be done?
我需要使用 JDBC 命令使用数据库链接连接到远程数据库。怎么做到呢?
采纳答案by jarhed
If you already have the dblink setup, you can utilize it in your SQL (sent via jdbc) by addressing the required tables like such:
如果您已经有 dblink 设置,您可以在您的 SQL(通过 jdbc 发送)中通过寻址所需的表来使用它,如下所示:
select * from SCHEMA.TABLE@DBLINK_NAME
Using this query inside of your java would look something like this
在你的 java 中使用这个查询看起来像这样
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
* java & oracle 假设
回答by Saber Chebka
Please take a look at orajdbclink, on sourceforge
请看看orajdbclink,在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...
我打算将我的 oracle plsql 源连接到 hbase 的 phoenix skin。在我看来,目前在 oracle 和 hbase 之间创建连接器的独特方式......
回答by Stephen C
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.)
如果您询问如何使用 JDBC 在您正在交谈的数据库和另一个数据库之间创建链接,那么您(大概)将以与执行任何其他 SQL 语句相同的方式执行“只是 SQL”。(如果您告诉我们您使用的是哪个数据库,我们可以讨论您需要执行的实际 SQL。)
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.
否则,我认为这没有意义。数据库链接/数据库链接是从一个数据库到另一个数据库的链接。但是 JDBC 用于从 Java 客户端与数据库对话。使用 DB 链接将 JDBC 客户端连接到数据库是没有意义的(对我来说)。