• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Need to know the reason why an insert failed

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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]
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you should use
stmnt.executeUpdate();
 
Suresh Kumar Mutyala
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am not getting any exception. Simply getting false.
 
Sheriff
Posts: 3837
66
Netbeans IDE Oracle Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Well THAT's new! Comfort me, reliable tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic