Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Need to know the reason why an insert failed

 
Suresh Kumar Mutyala
Greenhorn
Posts: 8
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you should use
stmnt.executeUpdate();
 
Suresh Kumar Mutyala
Greenhorn
Posts: 8
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 47244
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am not getting any exception. Simply getting false.
 
Martin Vajsar
Sheriff
Pie
Posts: 3747
62
Chrome Netbeans IDE Oracle
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic