Solved

java.sql.BatchUpdateException

Posted on 2011-03-16
13
425 Views
Last Modified: 2012-05-11
Is there any way to print PreparedStatement with binding data ?  This will help me to debug a runtime exception I'm facing.

Presently, I'm using datasource and JNDI name configured in JBOSS server to establish database connection.  I can not change the database driver configured in the server.

In my code , I do the following ..

ps = con.prepareStatement(<update-sql query>)

loop
 param1=//get data from list
 param2 =///get data from list
 ps.setString(1,param1)
 ps.setString(2,param2)
System.out.println(ps.toString()) ---> This prints org.jboss.resource.adapter.jdbc.jdk6.WrappedPreparedStatementJDK6@1f1640f
 ps.addBatch();
end loop

ps.executeBatch();

I'm getting an Exception :

java.sql.BatchUpdateException: Cannot add or update a child row: a foreign key constraint fails .
The exception  is quite understandable . And so,  I'd like to reproduce the query with the binding data to troubleshoot further to find out query with values failed to update.



0
Comment
Question by:cofactor
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 2
  • +1
13 Comments
 
LVL 92

Expert Comment

by:objects
ID: 35146059
don't believe so, the binding is typically done by the driver.
instead look at enabling sql logging on the database server
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 35146071
>>Is there any way to print PreparedStatement with binding data ?  

No. It would be easy enough to log debug the statement that's about to be executed though
0
 

Author Comment

by:cofactor
ID: 35146072
>>>instead look at enabling sql logging on the database server

How do I do that in MySQL 5  server ?
0
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
LVL 92

Expert Comment

by:objects
ID: 35146079
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 35146095
e.g.
System.out.printf("Statement is 'insert into foo (x,y) values (%s, %s)'\n", param1, param2);

Open in new window

0
 

Author Comment

by:cofactor
ID: 35146096
>>>It would be easy enough to log debug the statement that's about to be executed though

its a batch update.   Could you please tell , how it would be helpful to debug ?

 I can put some print for param1, param2 ...but  again all those will be added to the ps.addBatch() ....so its very difficult which query failed at the execution time of ps.executeBatch()
0
 

Author Comment

by:cofactor
ID: 35146114
>>>System.out.printf("Statement is 'insert into foo (x,y) values (%s, %s)'\n", param1, param2);

Yes. I understand that . but these are not executed right away ..correct ?. These are added to the batch.  

so, exact query which failed would be very much helpful .  
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 35146129
>>.so its very difficult which query failed ...

You could use the return values from executeBatch, but it's probably going to be easier to log at the db. Just make sure you don't leave logging turned up too hight when you've done
0
 
LVL 47

Accepted Solution

by:
for_yan earned 200 total points
ID: 35146135
Well,not very oiriginal suggestion, but maybe you want to  execute them one by one for the sake of debugging and then return to batch after debugging.

More stuff about inability to printout prepared statement:
http://www.velocityreviews.com/forums/t126331-print-out-preparedstatement.html
0
 

Author Comment

by:cofactor
ID: 35146655
>>>but maybe you want to  execute them one by one for the sake of debugging
gotcha , the mistake was  in the PreparedStatement  placeholder  .

I had
ps.setString(7,pvo.get(k).getProgramId());

This should be
ps.setString(8,pvo.get(k).getProgramId());

This works now . Errors vanished.



But I'have landed in  another trouble. Here is my current approach
loop
ps.setString(1,param1)
ps.setString(2,param2)      
.......................
...........................
//ps.addBatch();

boolean success=ps.execute();  

LOGGER.info("#### success ="+ success);  // this prints false , although I see changes in db!
end loop

//ps.executeBatch();


I'm doing individual updates  and inserts.  I dont rely on the return of boolean success.   In the server console it returns false , But I see the updates/ inserts of data has taken place in database .  This  execute() method does not return correctly . Is it a bug ?

I'm using ps.execute()  for insert and update.  Why this does not return correctly ? Or I'm missing something ? whats the correct approach ?
0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 100 total points
ID: 35146670
It should be ps.executeUpdate
0
 
LVL 47

Expert Comment

by:for_yan
ID: 35148592
ps.execute returns true and false not in the way you expect - it is not success or failure - more complicated:  check
http://download.oracle.com/javase/1.4.2/docs/api/java/sql/PreparedStatement.html

 executeUpdate which returns integer number of affected rows
0
 

Author Closing Comment

by:cofactor
ID: 35154362
I dont have access to mysql query logs . I had to do it through java end.  I liked the tricks posted by for_yan.

That was helpful .That solved my problem.
0

Featured Post

More Than Just A Video Library

Train for your certification. Learn the latest DevOps tools. Grow your skillset to do better work.

At Linux Academy, we release new training modules every week so you'll always be up to date on the latest tech.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

622 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question