Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Batch Update Exception

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
I am using batch updating (i.e. stmt.addBatch(sqlString), executeBatch(), etc.)
my code is:
int id = 0;
String sqlStr = "";
...
...
//createNewStatement is a customize method for creating a new statement.
Statement insertStmt = db.createNewStatement();
while(!(idVector.isEmpty())){
id = idVector.firstElement();
sqlStr = "INSERT INTO ids VALUES ('" + id + "')";
insertStmt.addBatch(sqlStr);

}
...
insertStmt.executeBatch();
insertStmt.close();
...
My question is: if somehow the id had a problem when it is executing the batch, how could I output the id that had the problem and continue on processing the others after it? Is there someway in retreiving the error from the executeBatch? I have tried putting it in a try-catch block, it doesn't work. I tried this:
try{
//execute batch
}catch(BatchUpdateException b){
System.err.println("SQLException: " + b.getMessage());
System.err.println("SQLState: " + b.getSQLState());
System.err.println("Message: " + b.getMessage());
}
and all it does is give me a "General Error" message.
Any help would be greatly appreciated. Thank you.
Allen
It just
 
Ranch Hand
Posts: 1879
MySQL Database Suse
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Although every driver handles this differently, when I was using the Oracle thin drivers it was an all or nothing deal. If there was one exceptioin in the thousands of batched updates, then all would fail and you could not find out at which statement it failed. Don't know about the other drivers, but I assume they are similar.
Jamie
 
The knights of nee want a shrubbery. And a tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic