Solved

java.sql.BatchUpdateException

Posted on 2011-03-16
13
418 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
  • 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
 
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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 

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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

760 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

Need Help in Real-Time?

Connect with top rated Experts

26 Experts available now in Live!

Get 1:1 Help Now