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
Solved

java.sql.BatchUpdateException

Posted on 2011-03-16
13
421 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
web application structure 18 95
How do I remove an object from a 3 40
mysql jsp example issue 32 48
ejb message driven bean mdb creation steps 2 15
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
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…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
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.

809 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