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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T23:38:17+00:00 2026-06-02T23:38:17+00:00

I have a following function: public Map<Integer, Product> fetchAllProducts() { Map<Integer, Product> pArr =

  • 0

I have a following function:

public Map<Integer, Product> fetchAllProducts() {

  Map<Integer, Product> pArr = new HashMap();
  try {
     Statement st = conn.createStatement();
     ResultSet rs = st.executeQuery("SELECT id, intro, content, price FROM Product");
     while (rs.next()) {
       pArr.put(rs.getInt("id"), new Product(rs));
     }
     st.close();
  } catch (SQLException ex) {
   //...
  }
  return pArr;
}

which gets all the rows from mySQL table Product and for each row creates a new Product class. Product constructor:

 public Product(ResultSet rs) {
    try {
       price = rs.getInt("price");
       content = rs.getString("content");
       intro = rs.getString("intro");
    } catch (SQLException ex) {
       //...
    }
   }

My question is: is there a better way to assign the result columns to variables in Product? The code price = rs.getInt("price"); and so on seems redundant, doesn’t it? Perfect would be, if I change the query statement to SELECT intro, content, tax, delivery FROM ... the constructuor would automaticlly assign it to the appropriate variables (i.e. intro, content, tax, delivery) in the constructor. Can this be done in Java or am I just dreaming?

  • 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-06-02T23:38:20+00:00Added an answer on June 2, 2026 at 11:38 pm

    First: You really shouldn’t pass the ResultSet to the constructor of Product! You should cleanly divide your database access code from your business logic.

    So typically I would expect to see this in your code:

    private static final String TABLE_NAME = "product";
    private static final String ID_COLUMN = "id";
    private static final String INTRO_COLUMN = "intro";
    private static final String CONTENT_COLUMN = "content";
    private static final String PRICE_COLUMN = "price";
    
    private static final String FETCHALLPRODUCTS_QUERY = String.format("SELECT %s, %s, %s, %s FROM %s", ID_COLUMN, INTRO_COLUMN, CONTENT_COLUMN, PRICE_COLUMN, TABLE_NAME);
    
    public Map<Integer, Product> fetchAllProducts() {
    
      Map<Integer, Product> pArr = new HashMap();
      try {
         Statement st = conn.createStatement();
         ResultSet rs = st.executeQuery(FETCHALLPRODUCTS_QUERY);
         while (rs.next()) {
            Integer price = rs.getInt(PRICE_COLUMN);
            String content = rs.getString(CONTENT_COLUMN);
            String intro = rs.getString(INTRO_COLUMN);
    
            Product product = new Product(price, content, intro);
            Integer id = rs.getInt(ID_COLUMN);
            pArr.put(id, product);
         }
         st.close();
      } catch (SQLException ex) {
       //...
      }
      return pArr;
    }
    

    But to answer your question: Doing this is a very common practise when dealing with plain JDBC. What you are looking for is an ORM framework like Hibernate.

    One thing I’m doing when using JDBC connection is declaring constants for the column names and table names. That way it is a bit cleaner in my opinion.

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

Sidebar

Related Questions

Is it possible to have a TLD map to the following function: public static
I have the following function: public static byte[] StringToByte(string str) { int length =
I have the following function: public static T TryGetArrayValue<T>(object[] array_, int index_) { ...
I have the following function below: public function setupHead($title){ $displayHead .='<!DOCTYPE html PUBLIC -//W3C//DTD
I have following repository: public function findClassPhotoByPath($path) { return $this->getEntityManager() ->createQuery('SELECT p FROM KSRGalleryBundle:Photo
I have the following Symfony2 form: public function buildForm(FormBuilder $builder, array $options) { $builder
If I have the following registry class: Class registry { private $_vars; public function
I have the following code: Public Shared Function GetAvailableManufacturers() As List(Of Manufacturer) 'first get
Hi i have the following Groovy code: package fp; abstract class Function { public
I have the following function which should return a map of dates to longs

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.