2

I don't know how to retrieve data using hashmap and while loop from my database. plz help me.
My code is

    package com.glomindz.mercuri.dao;

    import java.sql.Connection;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.util.HashMap;

    import com.glomindz.mercuri.util.MySingleTon;

          public class UserServicesDAO {

private Connection connection;

public UserServicesDAO() {
    //connection = new MySingleTon().getConnection();
    connection = MySingleTon.getInstance().getConnection();

}

public void get_all_data() {
    HashMap<Integer, String> result = new HashMap<Integer, String>();
    String query = "SELECT * FROM spl_user_master";
    try {
        PreparedStatement stmt = connection.prepareStatement(query);
        boolean execute = stmt.execute();
        System.out.println(execute);
        ResultSet resultSet = stmt.getResultSet();
        System.out.println(resultSet.getMetaData());

    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

public static void main(String[] args) {
    new UserServicesDAO().get_all_data();
}
 }

What's wrong with the code?

2
  • I don't know... Do tell us whats wrong? Commented Jul 8, 2013 at 12:22
  • Mapping all data into HashMaps is a very very bad idea for any code that is part of a released product. I just switched to a company where that was the standard, and it is one hell of unstable unreadable code. I wish the programmer who designed that years ago will die a painful death some day. Commented Jul 8, 2013 at 12:34

2 Answers 2

6

You can get values from your records in a loop like this

ResultSet resultSet = stmt.getResultSet();
while (resultSet.next()) {
    String someStringValue = resultSet.getString("some_column_name");
    int someIntegerValue = resultSet.getInt("some_other_column_name");
    //...
}
Sign up to request clarification or add additional context in comments.

Comments

1

You're creating a map but you're not returning it, also you're not putting the SQL result it the map result:

public Map<Integer, String> get_all_data() {
    HashMap<Integer, String> result = new HashMap<Integer, String>();
    String query = "SELECT * FROM spl_user_master";
    try {
        PreparedStatement stmt = connection.prepareStatement(query);
        boolean execute = stmt.execute();
        System.out.println(execute);
        ResultSet resultSet = stmt.getResultSet();
        System.out.println(resultSet.getMetaData());
        while (resultSet.next()) {
              result.put(resultSet.getInt(...), resultSet.getString(...));
        }
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return result;
}

In the code above ... should be the column name

3 Comments

i have modifed my code but then it gives a message java.sql.SQLException: Column 'SELECT * FROM spl_user_master' not found.
Not sure what you're doing but in the code above '...' should be replaced by the column name, not by the SQL query
A Map<Integer, String> is probably not the right structure to store data from a table with 8 columns. Create a user class and instantiate it for each row in your resultset

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.