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

  • Home
  • SEARCH
  • 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 8510323
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T03:40:16+00:00 2026-06-11T03:40:16+00:00

I have the following code: ResultSet rs = DB.doQuery(SELECT username,register_ip FROM users ORDER BY

  • 0

I have the following code:

    ResultSet rs = DB.doQuery("SELECT username,register_ip FROM users ORDER BY joined DESC LIMIT 10");
    Map<String,String> botInfo = new HashMap<String, String>();
    List<Map<String , String>> myMap  = new ArrayList<Map<String,String>>();
    int c = 0;

    while(rs.next()) {
        botInfo.put("username", rs.getString("username"));
        botInfo.put("register_ip", rs.getString("register_ip"));
        myMap.add(c, botInfo);
        c++;
    }

The idea is to have the output be like this.. and I’ll use PHP as an example:

[0] => array('username' => 'someUsername', 'register_ip' => 'someIP');

And so on.. but, what I get is a list of 10 HashMaps that all contain the same username,register_ip value. What are my options?

  • 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-11T03:40:18+00:00Added an answer on June 11, 2026 at 3:40 am

    That is because you’re adding the same botInfo HashMap to your List. What you would want to do is create a new HashMap inside your while loop, add elements to it and finally add this map to your list. What you would want to do is something like this:

    ResultSet rs = DB.doQuery("SELECT username,register_ip FROM users ORDER BY joined DESC LIMIT 10");
    List<Map<String , String>> myMap  = new ArrayList<Map<String,String>>();
    int c = 0;
    
    while(rs.next()) {
        //Create a new instance of the map
        Map<String,String> botInfo = new HashMap<String, String>();
    
        //Add elements to the the map
        botInfo.put("username", rs.getString("username"));
        botInfo.put("register_ip", rs.getString("register_ip"));
    
        //Add the map to the list
        myMap.add(c, botInfo);
    
        //Increment your counter
        c++;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following Java code: String query = Select 1 from myTable where
I have the following java code: import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement;
I have following code for updating user's column public void UpdateLastModifiedDate(string username) { using
I have following code for loading image from url in xml parsing endElement method
I have the following code: String table; private DB.ConnectMAS mas=new DB.ConnectMAS(); java.sql.ResultSet rs1 =
I currently have the following code that retrieves data from the database and then
I have the following code : $id = $_GET['id']; // get the recod from
Lets say I have the following code : PreparedStatement ps = null; ResultSet rs
I have the following bit of code that reads data from the an Oracle
I have JDBC connection code similiar to the following from the Java JDBC tutorial:

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.