This week's book giveaways are in the Cloud and AI/ML forums.
We're giving away four copies each of Cloud Native Patterns and Natural Language Processing and have the authors on-line!
See this thread and this one for details.
Win a copy of Cloud Native PatternsE this week in the Cloud forum
or Natural Language Processing in the AI/ML forum!
  • 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
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

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

 
Ranch Hand
Posts: 1122
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
Monad Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What if the insert fails?
 
Marshal
Posts: 64991
246
  • 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.
 
Sheriff
Posts: 24594
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: 1122
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?
 
Rancher
Posts: 4174
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: 10389
221
  • 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:
     
    Saloon Keeper
    Posts: 6021
    58
    Eclipse IDE Firefox Browser MySQL Database VI Editor Java 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: 1170
    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: 10389
    221
    • 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: 1122
    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
    Sheriff
    Posts: 24594
    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: 1122
    4
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    thanks
     
    Dave Tolls
    Rancher
    Posts: 4174
    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.
     
    It is sorta covered in the JavaRanch Style Guide.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!