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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T04:56:09+00:00 2026-06-01T04:56:09+00:00

I have 6 columns in a table. I have a select query which selects

  • 0

I have 6 columns in a table. I have a select query which selects some records from the table. While iterating over the result set, im using the following logic to extract the values in the columns:

Statement select = conn.createStatement();
        ResultSet result = select.executeQuery
        ("SELECT * FROM D724933.ECOCHECKS WHERE ECO = '"+localeco+"' AND CHK_TOOL = '"+checknames[i]+"'");
         while(result.next()) { // process results one row at a time
           String eco = result.getString(1);
           mapp2.put("ECO", eco);
           String chktool = result.getString(2);
           mapp2.put("CHECK_TOOL", chktool);
           String lastchktime = result.getString(3);
           mapp2.put("LAST_CHECK_TIME", lastchktime);
           String status = result.getString(4);
           mapp2.put("STATUS", status);
           String statcmts = result.getString(5);
           mapp2.put("STATUS_COMMENTS", statcmts);
           String details = result.getString(6);
           mapp2.put("DETAILS_FILE", details);
         }            

I have 2 questions here:
1. Is there any better approach rather than using result.getString()???
2. Lets say, another column gets added to the table at a later point. Is there any way my code handles this new addition without making change to the code at that point of time

  • 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-01T04:56:10+00:00Added an answer on June 1, 2026 at 4:56 am

    You can use ResultSetMetaData to determine the number and names of the columns in your ResultSet and deal with it this way. Note however that changing the number of columns in the database – affecting your code – and having the code still work may not always be a good idea.

    Additionally, note that you’re overwriting the values in your map on each iteration of the loop. You probably want to add those maps to some sort of List?

    Finally, you need to make sure that your getString methods will not return null anywhere, otherwise putting it into a map will throw an exception.

    Statement select = conn.createStatement();
    ResultSet result = select.executeQuery("SELECT * FROM D724933.ECOCHECKS WHERE ECO = '"+localeco+"' AND CHK_TOOL = '"+checknames[i]+"'");
    ResultSetMetaData rsmd = result.getMetaData();
    
    int numberOfColumns = rsmd.getColumnCount();    
    List data = new ArrayList<Map>();
    Map mapp2;
    
    while(result.next()) { // process results one row at a time
        mapp2 = new HashMap<String, String>();
        for(int i=1; i<=numberOfColumns; i++) {
            mapp2.put(rsmd.getColumnName(i), rs.getString(i));
        }
        data.add(mapp2);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a query like this: SELECT TABLE_NAME, COLUMN_NAME, IS_NULLABLE, DATA_TYPE FROM MY_DB.INFORMATION_SCHEMA.COLUMNS WHERE
I have a multi-table query, similar to this (simplified version) SELECT columns, count(table2.rev_id) As
I have the following query: select column_name, count(column_name) from table group by column_name having
i have a table with columns ID,SUBJECT,BRANCH i have select the rows which satisfies
I need a select from table which does not have column that tells when
I have table with 3 columns A B C. I want to select *
I have very simple select like this: SELECT * FROM table WHERE column1 IN
I have the following code: SELECT <column>, count(*) FROM <table> GROUP BY <column> HAVING
The following query is pretty simple. It selects the last 20 records from a
I've two tables which have following columns: Now I want have a SQL query

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.