• 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
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

Is it acceptable practice to have insertRecords method with void as return type?  RSS feed

 
Ranch Hand
Posts: 1115
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I have a method which insert records in database. Is acceptable practice to have void as its return type as it just inserts records or there is a better way of doing this?

thanks
 
Ranch Hand
Posts: 105
1
Java Linux Monad
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What if the insert fails?
 
Marshal
Posts: 64494
225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Gerard Gauthier wrote:What if the insert fails?

You have the option of returning false or that of throwing an exception.
 
Marshal
Posts: 24488
55
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The executeQuery method in JDBC returns the number of rows which were inserted. You might want your method to return that number. Or you might not... it depends on your design and whether it needs the caller of the insertRecords method to know that number.
 
Monica Shiralkar
Ranch Hand
Posts: 1115
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If it is not required to know the number the  is void acceptable or I should return string which is success or failure depending upon whether records got inserted or exception occurred?
 
Master Rancher
Posts: 4078
47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I expect you would want to know if it caused an exception, so I would expect it to throw one.

Anything other than that is a success.

If there's no need to know a count (eg because the INSERT only inserts one row anyway) then void is OK.
 
Saloon Keeper
Posts: 10245
216
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If your method call can succeed or fail, there are two acceptable options:

  • Return a boolean value indicating success. This is appropriate if failure of the method call is a normal occurrence and can happen regularly.
  • Throw an exception. This is almost always the more appropriate option, because failure is usually an exceptional situation.

  • Why would an SQL query fail, unless something exceptional happened? So throwing an exception is probably the way to go. Now, you have two ways of throwing the exception:

  • Just let the exception propagate up the call stack by declaring that your method may throw an SQLException. Appropriate if it's clear to a client of your method that your class/method is operating on a relational database.
  • Catch the exception and throw a new exception that wraps the caught exception. Appropriate if your class provides an abstraction level that allows clients to remain ignorant of relational databases.

  • The latter of the two options may look like this:
     
    Bartender
    Posts: 5851
    57
    Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Your particular application may only need to know if the insert is successful. You could return a boolean, a count, or throw an exception. My preference would be to return a count because this is consistent with the JDBC interface, and though this particular app may not need it I like to write code that is reusable if needed.
     
    Ranch Hand
    Posts: 1166
    11
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Stephan van Hulst wrote:If your method call can succeed or fail, there are two acceptable options:

  • Return a boolean value indicating success. This is appropriate if failure of the method call is a normal occurrence and can happen regularly.
  • Throw an exception. This is almost always the more appropriate option, because failure is usually an exceptional situation.

  • Why would an SQL query fail, unless something exceptional happened? So throwing an exception is probably the way to go. Now, you have two ways of throwing the exception:



    I know that in C# throwing an exception is a LOT more expensive than returning a primitive value.  Is it the same in Java?  If so and you're inserting 500 DB rows of data in a loop, i'd change the return type to a boolean (success/failure) or an int (either zero or one row inserted).

     
    Stephan van Hulst
    Saloon Keeper
    Posts: 10245
    216
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    In Java exceptions are significantly more expensive as well. However, you have to consider that a failure to insert a row is an exceptional situation, and as such will not occur frequently. That combined with the fact that the overhead of using an exception is NOTHING compared to the overhead of a round-trip to the database, makes throwing an exception the right choice.

    Besides, if you need to insert 500 rows in one go, you're better off to send them as a single batch using the PreparedStatement.addBatch() method.
     
    Monica Shiralkar
    Ranch Hand
    Posts: 1115
    4
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanks. I think one good way is to return boolean value.
     
    Paul Clapham
    Marshal
    Posts: 24488
    55
    Eclipse IDE Firefox Browser MySQL Database
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Yes, that's true. But don't start thinking that that's the only good way -- "acceptable practice" should always start with the requirements of the application and not with a list of "best practices".
     
    Monica Shiralkar
    Ranch Hand
    Posts: 1115
    4
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    thanks
     
    Dave Tolls
    Master Rancher
    Posts: 4078
    47
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Ryan McGuire wrote:
    I know that in C# throwing an exception is a LOT more expensive than returning a primitive value.  Is it the same in Java?  If so and you're inserting 500 DB rows of data in a loop, i'd change the return type to a boolean (success/failure) or an int (either zero or one row inserted).



    If you're inserting 500 rows then you'd be batching them up anyway, so only dealing with a single exception.
     
    permaculture is a more symbiotic relationship with nature so I can be even lazier. Read tiny ad:
    how do I do my own kindle-like thing - without amazon
    https://coderanch.com/t/711421/engineering/kindle-amazon
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!