简体   繁体   English

如何使用 JDBC 的 Oracle DBMS 的 COPY 表命令?

[英]How to use COPY table command of Oracle DBMS using JDBC?

I'm trying copy table from one database to another(On different machines), and using JDBC Template to execute query, but this request is specific to Oracle:我正在尝试将表从一个数据库复制到另一个数据库(在不同的机器上),并使用 JDBC 模板执行查询,但此请求特定于 Oracle:

COPY FROM username1/passwd1@//192.168.3.17:1521/PROD_SERVICE to username2/passwd2@//192.168.4.17:1521/SANDBOX_SERVICE INSERT TABLE_C (*) USING (SELECT * FROM TABLE_C);

And I get error:我得到错误:

Caused by: java.sql.SQLSyntaxErrorException: ORA-00900: invalid SQL statement引起:java.sql.SQLSyntaxErrorException: ORA-00900: invalid SQL 语句

How can I use specific to Oracle syntax in JDBC?如何在 JDBC 中使用特定于 Oracle 的语法?

Like some of the comments have already clarified, COPY is a sqlplus command, and it has been deprecated for a while.就像一些评论已经澄清的那样, COPY是一个 sqlplus 命令,并且已经被弃用了一段时间。 You cannot use it inside JAVA, because this command is not part of the SQL engine, it's just a kind of additional feature available only in sqlplus.你不能在 JAVA 内部使用它,因为这个命令不是 SQL 引擎的一部分,它只是 sqlplus 中才有的一种附加功能。 It is still available, but only for backwards compatibility.它仍然可用,但仅用于向后兼容。

If you want to copy a table using Java, you need to understand first some things:如果要使用 Java 复制表,首先需要了解一些事情:

  • Java, or any external engine for that matter, can't connect at the same time to both databases. Java 或任何外部引擎无法同时连接到两个数据库。 Either it connects to one or to the other.它要么连接到一个,要么连接到另一个。
  • You need to have a kind of bridge between both databases, so that your Java program is only acting as trigger.您需要在两个数据库之间建立一种桥梁,以便您的 Java 程序仅充当触发器。
  • Copying tables between databases is something related to the database, so you should think in using tools provided by your database engine.在数据库之间复制表是与数据库相关的事情,因此您应该考虑使用数据库引擎提供的工具。 You have some options, like Datapump or RMAN, although I consider Datapump the best suitable for your scenario.您有一些选择,例如 Datapump 或 RMAN,尽管我认为 Datapump 最适合您的方案。

However, if you insist in using Java, first you need to have a database link between both databases.但是,如果您坚持使用 Java,首先您需要在两个数据库之间建立数据库链接。 Then you can use Java to invoke an insert from one database to another.然后,您可以使用 Java 调用从一个数据库到另一个数据库的插入。

https://docs.oracle.com/database/121/SQLRF/statements_5006.htm#SQLRF01205 https://docs.oracle.com/database/121/SQLRF/statements_5006.htm#SQLRF01205

If you don't want to depend on thsnames entries in the server, here an example of database links:如果您不想依赖服务器中的 thsnames 条目,这里是数据库链接的示例:

CREATE DATABASE LINK to_my_remote_user 
   CONNECT TO remote_user IDENTIFIED BY password
   USING '(DESCRIPTION=
            (ADDRESS=(PROTOCOL=TCP)(HOST=remote_server)(PORT=remote_port))
            (CONNECT_DATA=(SERVICE_NAME=remote_service_name))
          )';

Once you have the dblink created, then you can connect from java to the database where the link is available and copy the data to the remote database创建 dblink 后,您可以从 java 连接到链接可用的数据库,并将数据复制到远程数据库

INSERT INTO remote_user.remote_table@to_my_remote_user 
select * from local_user.local_table ;

Important : Normally dblinks are not allowed on Production systems, because they increase security risks.重要提示:通常在生产系统上不允许使用 dblink,因为它们会增加安全风险。 Also remember that DDL operations over a database link require an extra step, such as using the procedure DBMS_UTILITY.EXEC_DDL_STATEMENT@dblink('create table...);还要记住,对数据库链接的 DDL 操作需要一个额外的步骤,例如使用过程DBMS_UTILITY.EXEC_DDL_STATEMENT@dblink('create table...);

Another option outside of Java is using SQL Developer copy feature. Java 之外的另一个选项是使用 SQL 开发人员复制功能。 Although I only recommend it for small tables.虽然我只推荐它用于小桌子。 If you want to use it with big tables, it will probably hang.如果您想将它与大桌子一起使用,它可能会挂起。 You can read here an good example:你可以在这里阅读一个很好的例子:

copy from one database to another using oracle sql developer - connection failed 使用 oracle sql 开发人员从一个数据库复制到另一个数据库 - 连接失败

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM