Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3235018
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T17:27:30+00:00 2026-05-17T17:27:30+00:00

Time and again I find myself creating a database utility class which has multiple

  • 0

Time and again I find myself creating a database utility class which has multiple functions which all do almost the same thing but treat the result set slightly differently.

For example, consider a Java class which has many functions which all look like this:

public void doSomeDatabaseOperation() {
    Connection con = DriverManager.getConnection("jdbc:mydriver", "user", "pass");
    try {
        Statement stmt = con.createStatement();
        ResultSet rs = stmt.executeQuery("SELECT whatever FROM table"); // query will be different each time
        while (rs.next()) {
            // handle result set - differently each time
        }
    } catch (Exception e) {
        // handle
    } finally {
        con.close();
    }
}

Now imagine a class with 20 of these functions.

As you can see, tons of boilerplate (opening a connection, try-finally block), and the only thing that changes would be the query and the way you handle the result set. This type of code occurs in many languages (considering you’re not using an ORM).

How do you manage your DB utility classes so as to reduce code duplication? What does a typical DB utility class look like in your language/framework?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T17:27:31+00:00Added an answer on May 17, 2026 at 5:27 pm

    The way I have done in one of my project is that I followed what Spring does with JDBC template and came up with a Query framework. Basically create a common class which can take select statement or pl/sql calls and bind parameters. If the query returns resultset, also pass the Rowmapper. This rowmapper object will be called by the framework to convert each row into an object of any kind.

    Example –

    Query execute = new Query("{any select or pl/sql}",
                              // Inputs and Outputs are for bind variables.
                              new SQL.Inputs(Integer.class, ...),
                              // Outputs is only meaningful for PL/SQL since the
                              // ResultSetMetaData should be used to obtain queried columns.
                              new SQL.Outputs(String.class));
    

    If you want the rowmapper –

    Query execute = new Query("{any select or pl/sql}",
                              // Inputs and Outputs are for bind variables.
                              new SQL.Inputs(Integer.class, ...),
                              // Outputs is only meaningful for PL/SQL since the
                              // ResultSetMetaData should be used to obtain queried columns.
                              new SQL.Outputs(String.class), new RowMapper() {
    
            public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
                Actor actor = new Actor();
                actor.setFirstName(rs.getString("first_name"));
                actor.setSurname(rs.getString("surname"));
                return actor;
            });
    

    Finally a Row class is the output which will have list of objects if you have passed the RowMapper –

    for (Row r : execute.query(conn, id)) {
      // Handle the rows
    }
    

    You can go fancy and use Templates so that type safety is guaranteed.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I find myself again and again faced with a similar problem: there's some piece
Quite often I find myself with bunch of R processes running in ESS buffers.
I have an engine that has quite a few dependencies. To make it easier
I cant seem to find an answer on the internet anywhere. So I have
Once have a look at the part of my GUI Once a user selects
I think I have something like programmer's OCD. I like my code to be
There's a couple of emacs features, such as flyspell-mode , highlight-beyond-fill-column , or auto-fill-mode
Preamble Using VTK library with C++, quite often I have to write something like
I am hitting a brick wall with something I'm trying to do. I'm trying

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.