1

How can I get sql inserts from a oracle table with JAVA? Is there a Oracle API for it?

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

Thks.

2
  • So you're trying to retrieve logging information for all the inserts that were performed? Commented Jan 22, 2010 at 15:43
  • 1
    specify a little more what you're trying to do, your question isn't very clear that way Commented Jan 22, 2010 at 15:46

4 Answers 4

1

Use Toad for Oracle. You can save a query as insert statements, spreadsheet, CSV... about anything you can think of.

Sign up to request clarification or add additional context in comments.

Comments

1

I would also use Toad, but not sure if it's free.
If really need to do it in Java, you must use JDBC and the MetaData of the result set to create your commends.
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),...

Comments

1

How crucial is it that you have insert statements? The normal way to transfer data between Oracle databases is to use the DataPump utility. This comes with a PL/SQL API which can be manipulated programmatically. I posted a sample set of code for doing that in another recent thread.

Comments

0

I came across this question looking for a pure java implementation to generate the INSERT statements of a Oracle table. 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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.