Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / December 2006

Tip: Looking for answers? Try searching our database.

Batch Insert with duplicate key

Thread view: 
Felix Roberto - 07 Dec 2006 12:47 GMT
any one knows how to do a
prepared statement batch insert, and when you get a pk exception
instead of breaking
the batch, keep going with the rest?

this is a simplyfied example of what i do
problem is i can get as much as 1500 record that are generated.
so how can i insert them into the db with out much performance
sacrifice?
and garantee that if a record allready exists it just keeps going with
the rest of the
group.

PreparedStatement pstm = con.crearPreparedStatement(
                    "INSERT INTO "+TABLE+" ("+PK_1+","+PK_2+") VALUES (?,?)");

for(int x=0;x<record1.length;x++){
    for(int y=0;y<record2.length;y++){
        pstm.setString(1, record1[x]);
        pstm.setString(2, record2[y]);
        pstm.addBatch();
    }
}
pstm.executeBatch();
Furious George - 07 Dec 2006 23:31 GMT
> any one knows how to do a
> prepared statement batch insert,

as you have it below.

> and when you get a pk exception
> instead of breaking
> the batch, keep going with the rest?

One way would be like this.

CREATE TEMPORARY TABLE t1 ( ... same as permanent table but without
primary key ... ) ;
batch insert into temporary table ;
batch execute ;
INSERT INTO permanentTable SELECT * FROM t1 WHERE primary key logic ;

> this is a simplyfied example of what i do
> problem is i can get as much as 1500 record that are generated.
> so how can i insert them into the db with out much performance
> sacrifice?

Maybe your db has a better way.  For example MySQL has a LOAD DATA
statement that is useful for loading large amounts of data.

> and garantee that if a record allready exists it just keeps going with
> the rest of the
> group.

You would really need to read the documentation for your particular
database to figure the optimal solution.

> PreparedStatement pstm = con.crearPreparedStatement(
>                     "INSERT INTO "+TABLE+" ("+PK_1+","+PK_2+") VALUES (?,?)");
[quoted text clipped - 7 lines]
> }
> pstm.executeBatch();
Felix Roberto - 12 Dec 2006 15:07 GMT
Rigth now we are using oracle 10g
but problem is that the data is not to be loaded it is generated.
it calculated data that is generated in whole block, up to 10k records
and there is a big chance for the primary key to be duplicated.
so what i was triying to do, was to execute the batch,
and on error to keep executing and give me at the end
all the error or something like that.

or any other solution that i can implement using jdbc

> > any one knows how to do a
> > prepared statement batch insert,
[quoted text clipped - 39 lines]
> > }
> > pstm.executeBatch();
andrewmcdonagh - 12 Dec 2006 19:23 GMT
On Dec 12, 3:07 pm, "Felix Roberto" <felix.roberto.r...@gmail.com>
wrote:
> Rigth now we are using oracle 10g
> but problem is that the data is not to be loaded it is generated.
[quoted text clipped - 5 lines]
>
> or any other solution that i can implement using jdbc

So your Java app is generating 10k records, some of which their primary
keys might already have been used?

If so....

does java app need to create the keys, can't you use the auto-generated
ones from Oracle?

If your app does need to, can it also validate that its used them
itself before sending to db?

if it can't, then I'd suggest calling a Stored Procedure with the 10k
entries and have it process them.  This would be much faster than the
java app, trying to do multiple small batches or single inserts. Take a
look at 'setArray()' from the JDBC classes, or if oracle is the only
db, look at their 'setARRAY()' version.

Andrew
NOTE: the stored Procedure can also be Java - but it wont run as fast
as a normal SQL stored Proc.  It should however, run as fast as a
PL/SQL stored Proc.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.