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 7429347
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T08:57:21+00:00 2026-05-29T08:57:21+00:00

I currently have the following code that retrieves data from the database and then

  • 0

I currently have the following code that retrieves data from the database and then create a User. This code is used in many of my classe to create other objects such as News, Commentsetc…

It uses apache commons dbutils.

final ResultSetHandler<User> handler = new ResultSetHandler<User>() {

            @Override
            public User handle(ResultSet rs) throws SQLException {

                User user = null;
                if (rs.next()) {
                    user = new User();
                    user.setId(rs.getInt("id"));
                    user.setUsername(rs.getString("username"));
                    user.setPassword(rs.getString("password"));
                }
                return user;
            }
        };

        final User user = run.query(
                "SELECT id, username, password FROM users WHERE username = ? AND active = 2 LIMIT 1;", handler,
                username);

Would it be possible to wrap the QueryRunner in a generic class and override the query method so the handler instanciate the generic T with the ResultSet. I would make sure any Ttype would ahve a constructor accepting a ResultSet.

Like so :

        public class QueryExecuter<T> extends QueryRunner {
    private ResultSetHandler<T> _handler;

    public QueryExecuter(){//The T type was for testing haha
        super();
        handler = new ResultSetHandler<T>() {

            @Override
            public T handle(ResultSet rs) throws SQLException {

                T object = null;
                if (rs.next()) {
                    object = new T(rs);
                }
                return object;
            }
        };
    }
}

I don’t know if you’ll understand, but I hope so, ask me if you want more details or a better explanation.

EDIT

I thought I could use a AbstractClass instead of the generic type that all of the differents objects would extends but it seems like I can’t write an abstract constructor. Will I have to make a static method that return an instance of the object like:

public abstract class DatabaseEntity {
    public static abstract DatabaseEntity create(ResultSet rs);//even this doesn't work...
}
  • 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-29T08:57:22+00:00Added an answer on May 29, 2026 at 8:57 am

    Possible, yes? But its a bad idea.

    You could do:

    class ResultSetHandler<T> {
      ResultSetHandler<T>(Class<T> clazz) {
        this.clazz = clazz;
      }
    
      public T handle(ResultSet rs) throws SQLException {
        T object = null;
        if (rs.next()) {
          object = clazz.getConstructor(ResultSet.class).newInstance(rs)
        }
        return object;
      }
    }
    

    Mixing domain and database is a bad idea, however. What would be better, however, would be to define an abtract method that creates the object based on the resultset:

    abstract class ResultSetHandler<T> {
    
      protected abstract T create(ResultSet rs);
    
      public T handle(ResultSet rs) throws SQLException {
        T object = null;
        if (rs.next()) {
          object = create(rs);
        }
        return object;
      }
    }
    

    Then, in your implementing class, you only need to provide a create() method instead of handling the result set yourself, for example:

    h = new ResultSetHandler<Person>() {
      protected Person create(ResultSet rs) {
        return new Person(rs.getString("name"));
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I currently have code that does the following: private final static ExecutorService pool =
I have a select query that currently produces the following results: Description Code Price
currently I have the following code: String select = qry.substring(select .length(),qry2.indexOf( from )); String[]
I currently have the following code to get a specific value i need from
I currently have the following js code function clearMulti(option) { var i; var select
I have the following code currently: <DataTemplate DataType={x:Type vm:SectionViewModel}> <ScrollViewer> <ItemsControl ItemsSource={Binding ViewModels}> </ItemsControl>
currently i have jdbc code with the following basic stucture: get Connection (do the
I am currently working on a php/javascript project which retrieves information from a database
On my site I have a jquery function which retrieves data from another (secured)
Anything I have tried didn't work. Currenly I have following code to change asp.net

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.