简体   繁体   English

使用JAVA将Oracle表数据转换为INSERT语句

[英]Oracle table(s) data to INSERT statements with JAVA

How can I get sql inserts from a oracle table with JAVA? 如何使用JAVA从Oracle表中获取SQL插入? Is there a Oracle API for it? 是否有Oracle API?

I need to store it in a file, the result file should have these examples lines: 我需要将其存储在文件中,结果文件应包含以下示例行:

-- INSERTING into TEST
Insert into "TEST" ("CODE","FAMILY","SUB_FAMILY","SEVERITY","STATUS","ANOMALY_DATE","DESCRIPTION",
"COMMENTS","VALUE0","VALUE1","VALUE2","VALUE3","VALUE4","VALUE5","VALUE6","VALUE7","VALUE8",
"VALUE9") values (1,'family1','subFamily11','bad','initial',to_date('0005-11-21','DD/MM/RR'),'someDescription',
'someComment','someValue','someValue','someValue','uselessValue','uselessValue',null,null,null,null,null);

the example line was created with the export tool from Oracle SQl developer 该示例行是使用Oracle SQl开发人员的导出工具创建的

Thks. THKS。

How crucial is it that you have insert statements? 插入语句有多重要? The normal way to transfer data between Oracle databases is to use the DataPump utility. 在Oracle数据库之间传输数据的通常方法是使用DataPump实用程序。 This comes with a PL/SQL API which can be manipulated programmatically. 它带有一个PL / SQL API,可以通过编程方式对其进行操作。 I posted a sample set of code for doing that in another recent thread . 我在另一个最近的线程中发布了一个示例代码集。

Use Toad for Oracle . 使用Toad for Oracle You can save a query as insert statements, spreadsheet, CSV... about anything you can think of. 您可以将查询另存为插入语句,电子表格,CSV等等任何您能想到的东西。

I would also use Toad, but not sure if it's free. 我也将使用Toad,但不确定是否免费。
If really need to do it in Java, you must use JDBC and the MetaData of the result set to create your commends. 如果确实需要用Java做到这一点,则必须使用JDBC和结果集的MetaData来创建表扬。
Here an example, just as an idea - it is NOT complete * 这里有一个例子,只是一个想法-它不完整*

Class.forName("oracle.jdbc.driver.OracleDriver");
Connection conn = DriverManager.getConnection("jdbc:oracle:oci:@DB_ID", "user", "password");
try {
  PreparedStatement stmt = conn.prepareStatement("SELECT * FROM MY_TABLE");
  StringBuilder command = new StringBuilder();
  ResultSet rset = stmt.executeQuery();
  ResultSetMetaData meta = rset.getMetaData();
  int columns = meta.getColumnCount();
  command.append("INSERT (");
  for (int i = 1; i <= columns; i++) {
    if (i > 1) {
      command.append(',');
    }
    command.append(meta.getColumnName(i));
  }
  command.append(") VALUES (");
  int head = command.length();
  while(rset.next()) {
    for (int i = 1; i <= columns; i++) {
      if (i > 1) {
        command.append(',');
      }
      String value = rset.getString(i);
      if (value != null) {
        command.append('\'');
        command.append(value);
        command.append('\'');
      } else {
        command.append("NULL");
      }
    }
    command.append(")");
    System.out.println(command.toString());
    command.setLength(head);
  }
} finally {
  conn.close();
}

* missing error handling, type handling (assumed all data is String),... * 缺少错误处理,类型处理(假设所有数据都是String),...

I came across this question looking for a pure java implementation to generate the INSERT statements of a Oracle table. 我遇到了这个问题,寻找一个纯Java实现来生成Oracle表的INSERT语句。 I found this piece of code that may be useful to someone that want to do it using just Java with JDBC without any tool. 我发现这段代码对于想要仅使用Java和JDBC而不使用任何工具的人可能有用。

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

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