File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JDBC and the fly likes Need to know the reason why an insert failed Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Need to know the reason why an insert failed" Watch "Need to know the reason why an insert failed" New topic
Author

Need to know the reason why an insert failed

Suresh Kumar Mutyala
Greenhorn

Joined: Sep 14, 2009
Posts: 8
Hi,

I am facing an issue where my insert query is failing to be inserted in db, Below is teh query i am using,



I am using a PreparedStatement to execute the above query like below,

If i excute the same query from SQL Developer the record is getting inserted without any issues.

Since the execute() is returning a boolean i am unable to know the reason why the query execution failed. Is there any way that i can figure out why the query execution failed.

Appreciate your help and efforts in advace.

Thanks,
Suresh

[Added code tags - see UseCodeTags]
Md Shahnawaz sakib
Greenhorn

Joined: Jul 18, 2012
Posts: 3
I think you should use
stmnt.executeUpdate();
Suresh Kumar Mutyala
Greenhorn

Joined: Sep 14, 2009
Posts: 8
I have tried that as well, even it didn't work. Problem is the method execute() returns a boolean value true if insert executed successfully and false if it failed to insert, similarly executeUpdate() retruns the count of records impacted or 0(zero) if no records got updated. But i am unable to find why didn't any records got inserted when teh insert query is correct. So is there any way i can find out the reason why the insert didn't succeed.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38467
    
  23
What details are you getting from the Exception? Remember an SQLException has two (or maybe more) additional fields over and above those in ordinary Exceptions. Print them out and compare them with the instructions for your database.

And, Md Shahnawaz sakib, welcome to the Ranch
Suresh Kumar Mutyala
Greenhorn

Joined: Sep 14, 2009
Posts: 8
I am not getting any exception. Simply getting false.
Martin Vajsar
Sheriff

Joined: Aug 22, 2010
Posts: 3610
    
  60

Suresh Kumar Mutyala wrote:Problem is the method execute() returns a boolean value true if insert executed successfully and false if it failed to insert, ...

That is not true. Please check the Javadoc (java.sql.PreparedStatement#execute()) to see what the return value means in this case.

Perhaps you're not interpreting the code behavior correctly?
Suresh Kumar Mutyala
Greenhorn

Joined: Sep 14, 2009
Posts: 8
Yes you were correct, my understanding about the execute method's returned value was incorrect. The boolean value returned by the execute method would be false even if the insert was successfull, it will be true only if any records are returned by the query which got executed. Thank you all for your response.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Need to know the reason why an insert failed