0

I have a Question to ExceptionHandling on java.sql.PreparedStatement.

I have an PreparedStatement:

insertTEZDETAIL = new XPreparedStatement(con, "INSERT INTO TEZDETAIL (PACKAGE_ID, LFDNR, ARCHIVTAG, PAYINF)"
                 + " Values(?, ?, ? ,XMLPARSE(DOCUMENT CAST(? AS CLOB)))" );

and Add many Statements in Batch:

        insertTEZDETAIL.setInt(1, paket_id);
        insertTEZDETAIL.setInt(2, Counter1);
        insertTEZDETAIL.setString(3, archiv_dat);
        SQLXML xmlvar = con.createSQLXML();
        xmlvar.setString(gesXML.toString());
        insertTEZDETAIL.setSQLXML(4, xmlvar);

        insertTEZDETAIL.addBatch();

Now I execute it Batch:

 public static void DB2Commit()
 {
    try
    {   insertTEZDETAIL.executeBatch();
    } catch (SQLException ex)
    {   for ( ; ex != null ; ex = ex.getNextException ())
        {
            ex.printStackTrace ();
        }
        try
        {
            con.rollback();
        } catch (SQLException e) {
            e.printStackTrace();
            System.exit(12);
        }
        System.err.println("Fehler beim execute Batch");
        System.exit(12);
    }
 }

With the "Forech exception loop" I get all Exception, but it there a way to get the GeneratedKey's of this Statement where the exception is? With RETURN_GENERATED_KEYS it doesnt work, may because of the Batch?? Or maybe to get the Statement it self?? Because I have a class to printout the complete Statement with generatedKey's, just like the DB2 get the Statement.

Thanks for Answers,

Florian

1 Answers1

0

You can get the generated key using

PreparedStatement pstmt = connection.prepareStatement(query,  Statement.RETURN_GENERATED_KEYS);
int rowsAffected = pstmt.executeUpdate();
ResultSet rs = pstmt.getGeneratedKeys();
int newId = -1;
if (rs.next()) 
{
  newId = rs.getInt(1);
}
rs.close();
pstmt.close();
Pradeep Kr Kaushal
  • 1,506
  • 1
  • 16
  • 29
  • Thanks for your Answer, with ExecuteBatch() it is: int[] rowsAffected = insertTEZDETAIL.executeBatch(); ??? – user3173681 Jan 08 '14 at 15:12
  • `XPrepareStatment` is your own class? – Pradeep Kr Kaushal Jan 08 '14 at 15:16
  • Could it be this they're talking about: http://www.openoffice.org/api/docs/common/ref/com/sun/star/sdbc/XPreparedStatement.html however I do not see any `executeBatch` method. – Paul Richter Jan 08 '14 at 15:21
  • I don't see any `excecuteBatch()` in doc. – Pradeep Kr Kaushal Jan 08 '14 at 15:24
  • yes this it is, but with batch the sysout from the XPreparedStatement is the first Query and not the Query where it dumps. – user3173681 Jan 08 '14 at 15:25
  • the Executebatch is in the fourth line of DB2Commit: insertTEZDETAIL.executeBatch(); – user3173681 Jan 08 '14 at 15:26
  • OK got it... for your question. Yes. Return type will be `int[]` of `executeBatch()` – Pradeep Kr Kaushal Jan 08 '14 at 15:29
  • See the link for more details http://stackoverflow.com/questions/4952316/using-getgeneratedkeys-with-batch-inserts-in-mysql-with-connector-j – Pradeep Kr Kaushal Jan 08 '14 at 15:31
  • thanks, but it is not possible that the XPreparedStatemet display the last Query when it dumps? Or to get the last Query(not only GeneratedKeys) and display it? – user3173681 Jan 08 '14 at 15:36
  • @user3173681 Do you mean you literally just want to see what the query looks like? If so, what happens if you do `System.out.println(insertTEZDETAIL)`? Unfortunately, unless this `XPreparedStatement` has a method of some sort that gets the last or "current" query, or that the `SQLException` contains the query in its error message, I would be willing to bet you cannot get the rendered query at any given point. However, I am not familiar with this class and I am basing this comment on experience with other `PreparedStatement` classes, so my comment is merely conjecture. – Paul Richter Jan 08 '14 at 16:44