Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

java.sql.BatchUpdateException

Posted on 2011-03-16
13
Medium Priority
?
427 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 800 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 400 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
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…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Suggested Courses

971 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