Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

Please Help :::: Multiple inserts with Prepared statement

843840Sep 6 2007 — edited Sep 7 2007
Hi,

I am trying to do a multiple insert with a prepared statement. I am iterating an ArrayList to get all my values from TABLE_A and insert into TABLE_B . However only one raw is inserted in TBALE_B with an java.sql.SQLException: ORA-00001: unique constraint violated
exception.My initial code is below.

MyBean.java - my DTO
package transaction;
 
import java.io.*;
import java.sql.Date;
 
public class MyBean implements Serializable{
 
private String firstName = "";
private Date regDate = null;
 
    public String getFirstName() {
        return firstName;
    }
 
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }
 
    public Date getRegDate() {
        return regDate;
    }
 
    public void setRegDate(Date regDate) {
        this.regDate = regDate;
    }
}
Transaction.java
package transaction;

import java.sql.*;
import java.util.*;

public class Transaction {
    private static final String DB_A = "java:comp/env/jdbc/dbA";
    private static final String DB_B = "java:comp/env/jdbc/dbB";
    private static final String TABLE_A = "table_A";
    private static final String TABLE_B = "table_B";
    
    public  static ArrayList doTransaction() {
        String sqlQuery = "SELECT * FROM " + TABLE_A;
        String  insertStr = "INSERT INTO " + TABLE_B + " VALUES(?,?)";
        
        Connection dbConn_A = null;
        Connection dbConn_B = null;
        Statement statmnt_A = null;
        PreparedStatement insert = null;
        ResultSet rstset = null;
        MyBean records = null;
        ArrayList recordList = new ArrayList();
        try {
            
            dbConn_A = DbConnection.getDbConnection(DB_A);
            dbConn_B = DbConnection.getDbConnection(DB_B);
            dbConn_A .setAutoCommit(false);
            dbConn_B.setAutoCommit(false);
            
            statmnt_A = dbConn_A.createStatement();
            rstset = statmnt_A.executeQuery(sqlQuery);
            
            while(rstset.next()){
                records = new MyBean();
                
                records.setFirstName(rstset.getString("first_name"));
                records.setRegDate(rstset.getDate("reg_date"));
                
                recordList.add(records);
                
                try {
                    insert = dbConn_B.prepareStatement(insertStr);
                    
                    Iterator it =  recordList.iterator();
                    while ( it.hasNext()) {
                        MyBean curRecord = (MyBean)it.next();
                        //Interting
                        insert.setString(1, curRecord.getFirstName());
                        insert.setDate(2, curRecord.getRegDate());
                    }
                } catch (SQLException ex) {
                    ex.printStackTrace();
                    ////
                    ////
                }
            }
            dbConn_A.commit();
            dbConn_B.commit();
            dbConn_A.setAutoCommit(true);
            dbConn_B.setAutoCommit(true);
        } catch (SQLException ex) {
            ex.printStackTrace();
            /////
            ////
        } finally {
             /////
             /////
        }
        
        return recordList;/////for debug'n
    }
}
I will appreciate for any help.
Thanx in advance
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Oct 5 2007
Added on Sep 6 2007
6 comments
687 views