Re-using a PreparedStatement in a batch update

Can I use a PrepatedStatement to add to a batch for a delete and then recreate the same PreparedStatement to do a bunch of inserts and have all the database work be done when executeBatch() is called?
Or do I need a seperate PreparedStatement for my deletes and inserts?
// Add the delete old records SQL to the batch
String sql = "DELETE FROM LWOS422A WHERE RPMTRX=? AND RPVERS=? AND RPRPSC=?";
PreparedStatement ps = connection.prepareStatement(sql);
ps.setObject(1, primaryValues[0]);
ps.setObject(2, primaryValues[1]);
ps.setObject(3, primaryValues[2]);
ps.addBatch();
// Add the insert of all new records to the batch
sql = "INSERT INTO LWOS422A VALUES (?, ?, ?, ?, ?, ?, ?, ?)";
ps = connection.prepareStatement(sql);
for (int r=0; r<getRowCount(); r++) {
   for (int c=0; c<getColumnCount(); c++) {
      ps.setObject(c+1, getValueAt(r, c));
   ps.addBatch();
ps.executeBatch();
connection.commit()

unfortunately no. when you issue the con.prepareStatement for the second time, all the batches from the first prepared statement lost. You will have to make 2 different batched prepared statements or change to statement objects.
Jamie

Similar Messages

  • More than 1 preparedStatement  object using batch update

    Hey how can I execute more than 1 preparedStatement object using batch update..Pls explain with a code(java)
    Thanks

    // turn off autocommit
    con.setAutoCommit(false);
    PreparedStatement stmt = con.prepareStatement(
         "INSERT INTO employees VALUES (?, ?)");
    stmt.setInt(1, 2000);
    stmt.setString(2, "Kelly Kaufmann");
    stmt.addBatch();
    stmt.setInt(1, 3000);
    stmt.setString(2, "Bill Barnes");
    stmt.addBatch();
    // submit the batch for execution
    int[] updateCounts = stmt.executeBatch();
    search in google for more information

  • JDBC Batch Updates & PreparedStatement problems (Oracle 8i)

    Hi,
    we're running into problems when trying to use JDBC Batch Updates with PreparedStatement in Oracle8i.
    First of all, Oracle throws a SQLException if one of the statements in the batch fails (e.g. because of a unique constraint violation). As far as I understand, a BatchUpdateException should be thrown?
    The next problem is much worse:
    Consider this table:
    SQL> desc oktest
    ID NOT NULL NUMBER(10)
    VALUE NOT NULL VARCHAR2(20)
    primary key is ID
    When inserting in through batch updates with a PreparedStatement, I can pass 'blah' as a value for ID without getting an exception (Oracle silently converts 'blah' to zero). Only when the offending statement is the last statement in the batch, Oracle throws an exception (again, SQLException instead of BatchUpdateException).
    Any comments/suggestions are appreciated.
    E-mail me if you want to see the code...
    Thanks,
    Oliver
    Oracle version info:
    (Enterprise Edition Release 8.1.6.0.0, JServer Release 8.1.6.0.0, Oracle JDBC driver 8.1.6.0.0 and 8.1.7.0.0 (we're using the 'thin' driver)
    CLASSPATH=/opt/oracle/product/8.1.7/jdbc/lib/classes12.zip:...
    null

    Please refer
    http://www.oracle.com/technology/products/oracle9i/daily/jun07.html

  • PreparedStatement batch updates

    I'm trying to use a batch update with a PreparedStatement, but when my code executes
    I receive an error stating The JDBC 2.0 method is not implemented, when the code
    executes addBatch(). Does JRockit have any api documentation stating what is
    and is not implemented. Any suggestions on a workaround? I'm assuming there
    is a Weblogic specific class that will accomplish this, but I would rather not
    implement such a class unless there is no other alternative.

    Hi,
    This seems to be a JDBC driver specific question (may not be related to
    JRockit). You may post this in WebLogic JDBC news groups.
    Sathish Santhanam
    Developer Relations Engineer
    BEA Support
    "frank" <[email protected]> wrote in message
    news:3f01921a$[email protected]..
    >
    I'm using JRockit 7.0 sp2rp1
    "frank" <[email protected]> wrote:
    I'm trying to use a batch update with a PreparedStatement, but when my
    code executes
    I receive an error stating The JDBC 2.0 method is not implemented, when
    the code
    executes addBatch(). Does JRockit have any api documentation stating
    what is
    and is not implemented. Any suggestions on a workaround? I'm assuming
    there
    is a Weblogic specific class that will accomplish this, but I would
    rather
    not
    implement such a class unless there is no other alternative.

  • Why can't I select or update the Record using the PreparedStatement?

    the DB is oracle9i,here is the test table
    test
    id number;
    name char(10);
    intro char(10);
    I have insert a record in the table.
    Now I will select or update the record by using the PreparedStatement here is my code:
    sqlStr = "update test set intro = ? where name =?";
    PreparedStatement stmt = conn.prepareStatement(sqlStr);
    stmt.setString(1,"my name is irixwang");
    stmt.setString(2,"irixwang");
    int is = stmt.executeUpdate();
    if (is >=1 )
    System.out.println("ok");
    It will not work ,but where I change the update condiction as followed:
    sqlStr = "update test set intro = ? where id =?";
    PreparedStatement stmt = conn.prepareStatement(sqlStr);
    stmt.setString(1,"my name is irixwang");
    stmt.setInt(2,100);
    int is = stmt.executeUpdate();
    It works well,why?
    please help me!thk!!!!

    I doubt it works "well" in either case since you are inserting a string that is longer than 10 characters into a field that only holds 10 characters. But to your question....
    A char data type holds a fixed number of characters - in your case 10.
    Because it holds 10 characters it will never hold 9, regardless of what you think is in the field it will always have 10 characters in it. The value you are using for query for name is 9 characters. Since 9 characters, regardless of content, will never be equal to 10 characters there is no way that it will update.
    So your choice is to either use a varchar, which holds a variable number of characters, or to always use 10 characters for your query (pad it with spaces.)

  • How can I avoid using rollback segment for batch updates.

    I am currently trying to avoid associating a large amount of space for rollback segment as this gets filled up only during the nightly batch updates. All that space will never be used during the day. Hence want to know if there is any way of avoiding the use of rollback segment at the session level.
    Rajesh

    No, but what you can do is create a large rollback segment to use with your batch job, at the start of your batch job bring the segment online, then use set transaction to use that rollback segment, when the batch job is finished and committed, you can then bring the rollback segment offline.
    If you are really pressed for space, as an alternate plan, you could actually create the large segment before the batch job and drop it after.

  • How to update bulk no of records using  Batch Update

    i am trying to insert and update records into multiple tables using Statement addBatch() and executeBatch() method. But some times it is not executing properly. some tables records are inserting and some tables it is not inserting. But I want all the records need to excute and commit or if any error is there then all records need to rollback.
    This is the code i am using.
    public String addBatchQueryWithDB(StringBuffer quries, Connection conNew,Statement stmtNew) throws Exception {
    String success="0";
    try {         
    conNew.setAutoCommit(false);
    String[] splitquery=quries.toString().trim().replace("||","##").split("\\|");
    for(int i=0;i<splitquery.length;i++) {
    //System.out.println("query.."+i+".."+splitquery.trim().replace("##","||"));
    stmtNew.addBatch(splitquery[i].trim().replace("##","||"));
    int[] updCnt = stmtNew.executeBatch();
    for(int k=0;k<updCnt.length;k++){
    int test=updCnt[k];
    if(test>=0){
    success=String.valueOf(Integer.parseInt(success)+1);
    // System.out.println(".updCnt..."+updCnt[k]);
    System.out.println("success...length.."+success);
    if(updCnt.length==Integer.parseInt(success)){
    success="1";
    //conNew.commit();
    } catch (BatchUpdateException be) {
    //handle batch update exception
    int[] counts = be.getUpdateCounts();
    for (int i=0; i<counts.length; i++) {
    System.out.println("DB::addBatchQuery:Statement["+i+"] :"+counts[i]);
    success="0";
    conNew.rollback();
    throw new Exception(be.getMessage());
    } catch (SQLException ee) {
    success="0";
    System.out.println("DB::addBatchQuery:SQLExc:"+ee);
    conNew.rollback();
    throw new Exception(ee.getMessage());
    } catch (Exception ee) {
    success="0";
    System.out.println("DB::addBatchQuery:Exc:"+ee);
    conNew.rollback();
    throw new Exception(ee.getMessage());
    }finally {
    // determine operation result
    if(success!=null && success.equalsIgnoreCase("1")){
    System.out.println("commiting......");
    conNew.commit();
    }else{
    System.out.println("rolling back......");
    success="0";
    conNew.rollback();
    conNew.setAutoCommit(true);
    return success;
    }

    Koteshwar wrote:
    Thank you for ur reply,
    I am using single connection only, but different schemas. Then I am passing a Stringbuffer to a method, and First iam setting con.setAutoCommit(false); and then in that method i am splitting my queries in stringbuffer and adding it to stmt.addBatch(). After that I am executing the batch.after executing the batch i am commiting the connectionIf I am reading that right then you should stop using Batch.
    The intent of Batch is that you have one statement and you are going to be using it over and over again with different data.

  • Batch Updates Using Oracle

    Hi,
    We are currently having problems saving data using Weblogic 6.1 sp1 and Oracle
    8.1.6. we are using Weblogic JDBC Drivers for Oracle. The data we are saving is to
    the extent of 25-50,00 records. When we are trying to this we are getting transaction
    timeout errors since this saver process is taking more than 30 seconds which is out
    rimeout limit.
    Can we do this using Batch updates?? Any code snippets or pointers will be grealty
    appreciated.
    Thank YOu,
    Sanjeev

    Can you be more specific?
    - What do you want to do exactly?
    - What are your OS and Reports versions?
    At first, it sounds a bit strange to use Reports for batch updates. A pl/sql procedure may be more suitable.
    You can schedule a report in several ways, e.g. via CRON on Unix (see RWRUN command in the 9iDS Help)).
    You can do the same via the database using the SRW package (see http://download-west.oracle.com/docs/html/A92102_01/pbr_evnt.htm#1005558 )
    You can use the Reports Queue Manager.

  • Batch Updates using JDBC 2.0

    Dear all,
    I am trying to use a PreparedStatment's batchUpdate() method. Here is the problem.
    I get an exception if there is a duplicate entry, and that blows up the entire batch. This leaves me with no room for error checking as to where this error had occurred.
    How do I know at which row I got the error? How can I make the batchUpdate() execution to ignore the error and continue?
    Thanks,
    -rrvvg

    This is a case where reading the Java documentation will help you. Here is the link to
    int[] executeBatch() throws SQLException.
    http://java.sun.com/j2se/1.5.0/docs/api/java/sql/Statement.html#executeBatch()
    I know this isn't code showing you how to do it, but I think it does a good job of explaining how to handle your situation. The documentation clearly states that you also need to see the documentation of your specific JDBC driver because Exception behavior is defined by the driver and can be different between vendor offerings..

  • Batch updates with callable/prepared statement?

    The document http://edocs.bea.com/wls/docs70/oracle/advanced.html#1158797 states
    that "Using Batch updates with the callableStatement or preparedStatement is
    not supported". What does that actually mean? We have used both callable and prepared
    statements with the batch update in our current project (with the Oracle 817 db).
    It seems to run ok anyway.

    So the documentation should state that batch updates do not work ok in old versions
    of JDriver for Oracle, BUT work correctly with newer version. Additionally, batch
    updates work ok when used with Oracle supplied jdbc-drivers?
    "Stephen Felts" <[email protected]> wrote:
    Support for addBatch and executeBatch in the WLS Jdriver for Oracle was
    added in 7.0SP2.
    It was not available in 6.X or 7.0 or 7.0SP1.
    "Janne" <[email protected]> wrote in message news:3edb0cdc$[email protected]..
    The document http://edocs.bea.com/wls/docs70/oracle/advanced.html#1158797
    states
    that "Using Batch updates with the callableStatement or preparedStatementis
    not supported". What does that actually mean? We have used both callableand prepared
    statements with the batch update in our current project (with the Oracle817 db).
    It seems to run ok anyway.

  • Oracle 10G AS, Batch Update doesn't commit.

    Hello,
    We are using Batch uptdate to commit a set of records, but to our surprise one of the batches commits in DB and the other doesnt.
    The below pseudo code explains our scenario :
    String str1="INSERT INTO TBL1 (COL1,COL2) VALUES (?,?) ";
    String str2="UPDATE TBL2 SET COL1=? WHERE COL2=?";
    PreparedStatement pstmt1=conn.prepareStatement(str1);
    PreparedStatement pstmt2=conn.prepareStatement(str2);
    for(500 recs)
    pstmt1.addbatch();
    pstmt2.addbatch();
    On 500 Recs
    //This batch updates the DB and commits too
    pstmt1.executeBatch();
    pstmt1.clearBatch();
    con.commit();
    //The Batch executes successfully, but updates are not reflected in DB,
    //which may mean that it must not be committing in DB
    pstmt2.executeBatch();
    pstmt2.clearBatch();
    con.commit();
    - In the above, pstmt1 is an INSERT and pstmt2 is an UPDATE.
    - It so happens that at the end, DB reflects Inserts done by pstmt1, but it doesnt reflect the Updates of pstmt2.
    - We have also fired a SELECT stmt immediately after pstmt2 Execute Batch, but the updated values are not reflected.
    - Also the above scenario works absolutely fine if we use a STATEMENT instead of a PREPAREDSTATEMENT for pstmt2.
    Set up Details:
    App Server :: Oracle 10G AS for Linux 2.1,
    Database Driver :: Oracle 10G Driver which is bundled with Oracle 10G AS
    Database :: Oracle 9i
    Any ideas in this regards would be highly appreciated.
    Thanks !
    - Khyati
    Message was edited by:
    user473157

    I think this is not the right forum for this question; probably a JDBC forum or one of the Oracle AS forums.
    Kuassi

  • HELP!!! Strange Error with Batch Update

    While trying to do a batch update with the following preparedstatment:
    INSERT INTO TAB1
    (DATETIME,CALC_ID,SITE_ID,APPL_ID,VAL)
    values (
    {ts '2003-06-24 08:49:14'},
    (SELECT CALC_ID FROM TAB2 WHERE CALC_NAME=?),
    2,
    7,
    Notice the parameter in the subquery. This works fine for a single executeUpdate. But if i do two preparedstatement.addbatch calls and then executeBatch I see a unique constraint violation on (DATETIME,CALC_ID, and SITE_ID) CALC_ID is the only key that varies in my batch. It is as if the subquery is not being resolved and written literally to the field. If I try the 1st update by itself in add batch it works. If I try the second it works by itself. If I try them together in the batch it fails. The unique constraint is not violated when I execute them singly.
    Any Ideas?
    Thanks in advance.

    I've found that db2, PreparedStatements and batch updating do not work very well together. I've had to use dynmanic sql and build the update statement myself. Actually I wrote a query parser that builds the statement so I can pass it like a PreparedStatement.
    I'm not sure if your in db2 ver 7 but if so make sure your using fixpack 9. at lot of bad prepared statement behavior.

  • Statement caching and batch update

    Can these 2 JDBC features work together ?
    Is it possible while statement is cached to be reparsed (soft) if used in batch update ?
    I am asking this questions because i have a sitution where an insert is cached using implicit statement caching and then put in a batch to exeute batch updates !!! From statspack reports i find that 1/3 of statements are reparsed ... even soft !!!

    Statement caching and batch update work fine together. The most common cause of unexpected soft parses is changing the type of some parameters. If you first bind one type, setInt(1, ...), do addBatch, then bind another type to the same parameter, setString(1, ...), and do addBatch, you will get a soft reparse. There is nothing the JDBC driver can do about this, the RDBMS requires it.
    In general, whatever parse behavior you see with statement caching you would also see without it.
    Douglas

  • Error while running batch update statement

    Hi
    We are experiencing the below error while running the batch update statement where in the IN clause have more than 80,000 entries. The IN clause is already handled for max 1000 values so it has multiple or clause
    like update...where id in (1,2...999) OR id in (1000,1001........) OR Id in ()...
    Error at Command Line:1 Column:0
    Error report:
    SQL Error: ORA-00603: ORACLE server session terminated by fatal error
    ORA-00600: internal error code, arguments: [kghfrh:ds], [0x2A9C5ABF50], [], [], [], [], [], []
    ORA-00600: internal error code, arguments: [kkoitbp-corruption], [], [], [], [], [], [], []
    00603. 00000 - "ORACLE server session terminated by fatal error"
    *Cause: An ORACLE server session is in an unrecoverable state.
    *Action: Login to ORACLE again so a new server session will be created
    Is there a limitation of oracle or some bug?
    Thanks

    http://download.oracle.com/docs/cd/B19306_01/server.102/b14237/limits003.htm
    The limit on how long a SQL statement can be depends on many factors, including database configuration, disk space, and memoryI think you're over this limit.
    The only way is creating a temporary table with all the values and using
    IN (select ...)Max
    http://oracleitalia.wordpress.com

  • Inbound IDoc SHPCON - Batch update issue

    Hi all,
    I would like to use SHPCON.DELVRY03 idoc in order to update Outbound delivery document.
    Scope is :
    - picking
    - good issue
    - update batches
    - update serial numbers
    - update volume and weights
    We met issue on update batches a soon as one document item already has batch information before receiving IDoc.
    2 cases :
    - no batch spliting => High level item has already a linked batch (LIPS-POSNR = 000010 and LIPS-CHARG not empty.
    Is there a way to update batch information if external warehouse confirms another batch number ?
    - batch splitting => High level item has already 2 sublines POSNR = 900001 & 900002. Both has batch numbers.
    If I want to confirm it, I can send E1EDL19 with BAS qualifier but sublines are added...
    Must I delete existing sublines with E1EDL19 DEL ?
    Thanks a lot for your help.
    J.C.
    and to post good issue.
    All is OK except

    Hi,
    We are having same issue as of yours...
    i.e Updation of batch via IDOC to the delivery if delivery contains batch ...its not happening and also idoc not throwing any error
    Did you able to resolve this ?
    Thanks
    Rajesh

Maybe you are looking for