Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Frits Walraven
Bartenders:
  • Carey Brown
  • salvin francis
  • Claude Moore

Control and return of error whien accessing MySQL db from a servlet  RSS feed

 
Ranch Hand
Posts: 103
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have a servlet DAO to delete, update and select records in my db. These all are working fine. But in case of, for instance duplicate key I can't see how to return this error to the servlet that invoked this DAO.
This is my method to include records in my db.

Thanks in advance.
 
Marshal
Posts: 67257
170
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because you catch the exception in the method, it cannot propagate to the caller. I would change the method to let the exception propagate to the caller where the caller can catch it (if it wants to) and deal with the problem.
 
Cezar Apulchro
Ranch Hand
Posts: 103
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Bear,
how I do that? Simply exclude the catch?
 
Sheriff
Posts: 21719
102
Chrome Eclipse IDE Java Spring Ubuntu VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Cezar Apulchro wrote:Hi Bear,
how I do that? Simply exclude the catch?


Yes, but you should make sure that the statement is closed even if an exception occurs. The best way to do this is to use try-with-resources:

Note that you don't have to explicitly close pstmInput anymore, that's done for you now.
 
Cezar Apulchro
Ranch Hand
Posts: 103
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How control exceptions in caller side?
 
Sheriff
Posts: 24374
55
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Cezar Apulchro wrote:How control exceptions in caller side?



If an exception is thrown then the caller can either handle it or allow it to propagate to the caller of the caller.

Now whether the caller should handle the exception, and if so how -- that entirely depends on the design of the caller. We know nothing about the caller so it's impossible to say more than that.

If you're asking about what Java code you should use in the caller, well, you already know how to use a catch-statement. The other part is to declare that your cadastrarUsuarios method throws SQLException.
 
Cezar Apulchro
Ranch Hand
Posts: 103
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
very very good.
This is my DAO now:

My servlet now:

To test this, I trying to insert an record that already exist, the error code of MySQL is 1022, can I get this error in the catch??
 
Cezar Apulchro
Ranch Hand
Posts: 103
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry I know how to get error code.

Thank you to all.
 
Do you pee on your compost? Does this tiny ad?
Create Edit Print & Convert PDF Using Free API with Java
https://coderanch.com/wiki/703735/Create-Convert-PDF-Free-Spire
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!