Storing Multiple Rows from MySQL query in MultiDimensional HashMap - java

So I'm trying to store a MySQL query result set into a multi dimensional HashMap as listed so:
public HashMap<String, HashMap<String, String>> getData(String query)
{
Statement stmt = null;
HashMap<String, HashMap<String, String>> results = new HashMap<String, HashMap<String, String>>();
try
{
stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(query);
ResultSetMetaData rsmd = rs.getMetaData();
while (rs.next())
{
for (int i = 1; i < rsmd.getColumnCount() + 1; i++)
{
results.put(Integer.toString(i - 1), new HashMap<String, String>());
results.get(Integer.toString(i - 1)).put(rsmd.getColumnLabel(i), rs.getString(i));
}
}
}
catch (SQLException ex)
{
ex.printStackTrace(System.out);
}
return results;
}
However when using the function to print it out as so:
public static void printMap(Map mp)
{
Iterator it = mp.entrySet().iterator();
while (it.hasNext())
{
Map.Entry pair = (Map.Entry)it.next();
System.out.println(pair.getKey() + " = " + pair.getValue());
it.remove();
}
}
It is only storing a single row result and I can't wrap my head around why.
0 = {Date=2014-11-04}
1 = {Num=1256}
2 = {ATime=null}
3 = {ALocCode=null}
4 = {DTime=1:00 PM}
5 = {DLocCode=JFK}
6 = {EstATime=8:00 PM}
7 = {EstDTime=1:00 PM}
8 = {EId=7624}
My question is, and the only way I can put it is relating to PHP, is how can I make it store like this?
$result[0]['Date'] = '3214';
....
$result[1]['Date'] = '6426';
Since that is essentially what I'm trying to achieve?

main problem that you've swapped "rows" and "columns", next one is that you're re-creating HashMap every time you put field, proper code will look like this:
public Map<String, Map<String, String>> getData(final String query) {
final Map<String, Map<String, String>> results = new HashMap<>();
try (final Statement stmt = this.conn.createStatement(); final ResultSet rs = stmt.executeQuery(query);) {
final ResultSetMetaData rsmd = rs.getMetaData();
long rId = 0;
while (rs.next()) {
final Map<String, String> record = new HashMap<>();
for (int i = 1; i < (rsmd.getColumnCount() + 1); i++) {
record.put(rsmd.getColumnLabel(i), rs.getString(i));
}
results.put(String.valueOf(rId++), record);
}
} catch (final SQLException ex) {
ex.printStackTrace(System.out);
}
return results;
}
public static void printMap(final Map<?, ?> mp) {
for (final Entry<?, ?> entry : mp.entrySet()) {
final Object key = entry.getKey();
final Object value = entry.getValue();
if (value instanceof Map) {
System.out.println(key);
printMap((Map<?, ?>) value);
} else {
System.out.println(key + "=" + entry.getValue());
}
}
}

The answer by Lashane is good for the errors you needed solving, however it can be improved:
You wanted numeric access ($result[0]['Date']) to the rows, not string.
print method should use fully typed parameter.
Rows should be stored in TreeMap or LinkedHashMap or ArrayList to retain row order. ArrayList is better for your case, actually.
Columns should be stored in LinkedHashMap to retain column order.
Do not catch exception and continue. Allow it to cascade up to caller.
Updated version:
public List<Map<String, String>> getData(final String query) throws SQLException {
final List<Map<String, String>> results = new ArrayList<>();
try (Statement stmt = this.conn.createStatement();
ResultSet rs = stmt.executeQuery(query)) {
ResultSetMetaData metaData = rs.getMetaData();
while (rs.next()) {
Map<String, String> record = new LinkedHashMap<>();
for (int col = 1; col <= metaData.getColumnCount(); col++)
record.put(metaData.getColumnLabel(col), rs.getString(col));
results.add(record);
}
}
return results;
}
public static void printMap(List<Map<String, String>> rows) {
for (int rowNum = 0; rowNum < rows.size(); rowNum++)
System.out.println(rowNum + " = " + rows.get(rowNum));
}
You can now access it like you did in PHP:
// PHP (for reference, the way you requested)
$result[0]['Date']
// Java
result.get(0).get("Date")
// Groovy
result[0]['Date']
result[0].Date
// JSP
<c:forEach var="row" items="${result}" varStatus="rowStatus">
${rowStatus.index} = <c:out value="${row.Date}"/>, ...
</c:forEach>

Related

Loop in List of Map in get Query JDBC [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I'm trying to make a get result of my query with List<Map<String, String>> result
But I'm blocked because I don't see how I can do the loop in my List Map and I have an error in method put
This is a part of my method with the loop for below :
protected List<Map<String, String>> getAllResultForQuery(String sql) throws SQLException {
List<Map<String, String>> result = null;
Connection conn = null;
Statement stmt = null;
try {
// Create connection
conn = endpoint.getDBConnection();
// Execute a query
stmt = conn.createStatement();
// Let us select all the records and display them.
ResultSet rs = stmt.executeQuery(sql);
LOG.debug("Execute query: " + sql);
ResultSetMetaData rsmd = rs.getMetaData();
int nbColumns = rsmd.getColumnCount();
//loop
for (Map<String, String> map : result) {
for (Map.Entry<String, String> entry : map.entrySet()) {
if (rs.next()) {
if (result == null) {
result = new HashMap<String, String>();
}
for (int i = 1; i <= nbColumns; i++) {
String columnName = rsmd.getColumnName(i).toUpperCase();
String columnValue = rs.getString(i);
result.put(columnName, columnValue);
}
}
}
}
Anyone can Help please ?
Thanks
Try this.
protected List<Map<String, String>> getAllResultForQuery(String sql) throws SQLException {
List<Map<String, String>> result = new ArrayList<>();
try (Connection conn = endpoint.getDBConnection();
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql)) {
ResultSetMetaData rsmd = rs.getMetaData();
int nbColumns = rsmd.getColumnCount();
while (rs.next()) {
Map<String, String> map = new HashMap<>();
for (int i = 1; i <= nbColumns; i++) {
String columnName = rsmd.getColumnName(i).toUpperCase();
String columnValue = rs.getString(i);
map.put(columnName, columnValue);
}
result.add(map);
}
}
return result;
}
I make like this :
for (Map result : results) {
if (rs.next()) {
if (result == null) {
result = new HashMap<String, String>();
}
for (int i = 1; i <= nbColumns; i++) {
String columnName = rsmd.getColumnName(i).toUpperCase();
String columnValue = rs.getString(i);
result.put(columnName, columnValue);
}
}
}
How do you think ?

How to optimize this Java JDBC code which selects many rows and columns

I used all kinds of database CURD by the Hibernate before. Actually, I have never used JDBC before Hibernate. So I don't know anything about JDBC. So the following code is working but I believe it can optimize a lot. This query returns join data from 2 tables and it is huge:-
private final String COL1 = "COL1";
private final String COL2 = "COL2";
private final String COL3 = "COL3";
private final String COL4 = "COL4";
private final String COL5 = "COL5";
private final String COL6 = "COL6";
private final String COL7 = "COL7";
private final String COL8 = "COL8";
private final String COL9 = "COL9";
private final String COL10 = "COL10";
public void getDataByPp(String pp) {
PreparedStatement pst = null;
ResultSet rset = null;
try {
pst = conn.prepareStatement(SELECT_MASTER_AND_FEATURE);
pst.setString(1, pp);
rset = pst.executeQuery();
ArrayList<String> data = new ArrayList<String>();
Map<Integer, Map<String, String>> mapMap = new HashMap<Integer, Map<String, String>>();
int index = 0;
while (rset.next()) {
Map<String, String> dataMap = new HashMap<String, String>();
dataMap.put(COL1, rset.getString("COL1"));
data.add(rset.getString("COL1"));
dataMap.put(COL2, rset.getString("COL2"));
data.add(rset.getString("COL2"));
dataMap.put(COL3, rset.getString("COL3"));
data.add(rset.getString("COL3"));
dataMap.put(COL4, rset.getString("COL4"));
dataMap.put(COL4, rset.getString("COL4"));
dataMap.put(pp, rset.getString("pp"));
data.add(rset.getString("pp"));
dataMap.put(COL5, rset.getString("COL5"));
data.add(rset.getString("COL5"));
dataMap.put(COL6, rset.getString("COL6"));
data.add(rset.getString("COL6"));
dataMap.put(COL7, rset.getString("COL7"));
data.add(rset.getString("COL7"));
dataMap.put(COL8, rset.getString("COL8"));
data.add(rset.getString("COL8"));
dataMap.put(COL9, rset.getString("COL9"));
data.add(rset.getString("STATE"));
dataMap.put(COL10, rset.getString("COL10"));
data.add(rset.getString("COL10"));
mapMap.put(index, dataMap);
index++;
}
for (String val : data) {
System.out.println("data : " + val);
}
for (Integer key : mapMap.keySet()) {
Map<String, String> dataMap = mapMap.get(key);
for (String key2 : dataMap.keySet()) {
System.out.println("key: " + key2 + " val: " + dataMap.get(key2));
}
}
} catch (SQLException e) {
System.err.format("SQL State: %s\n%s", e.getSQLState(), e.getMessage());
} catch (Exception e) {
System.out.println("Exception when insert into INTERIOR_ROOMS_FEATURES");
}
}
Well, I notice a fairly long while loop which appears to iterate a set number of "COL"(s) and add them in a consistent way (except "STATE" / "COL9") which may require an if - but otherwise, all of those fields could be read from an array. Also, I would prefer the diamond operator <> for initializing the generic collections. Like,
String[] cols = { COL1, COL2, COL3, COL4, "pp", //
COL5, COL6, COL7, COL8, COL9, "STATE", COL10 };
List<String> data = new ArrayList<>();
Map<Integer, Map<String, String>> mapMap = new HashMap<>();
int index = 0;
while (rset.next()) {
Map<String, String> dataMap = new HashMap<>();
for (String c : cols) {
String s = rset.getString(c);
dataMap.put(c, s);
data.add(s);
}
mapMap.put(index, dataMap);
index++;
}

HashMap repeating the last element [duplicate]

This question already has answers here:
Why does my ArrayList contain N copies of the last item added to the list?
(5 answers)
Closed 5 years ago.
I am trying to create a HashMap in a HashMap so it will be easier for me to access elements of it in the future as shown below.
The problem is it only repeating the last elements of the while loop and not the rest of it.
HashMap<String,String> result = new HashMap<>();
HashMap<Integer, HashMap<String,String>> fr = new HashMap<>();
int i = 0;
try {
ResultSet rq = qexec.execSelect();
// ResultSetFormatter.out(System.out, rq, query);
// get result from SPARQL query
while (rq.hasNext()) {
QuerySolution soln = rq.next();
id = soln.getLiteral("?id").getLexicalForm();
//...
result.put("id",id);
//...
if (soln.getLiteral("?wateruse") != null) {
wateruse = soln.getLiteral("?wateruse").getLexicalForm();
//...
result.put("wateruse",wateruse);
} else {
System.out.println("NO");
}
fr.put(i, result);
i++;
}
} finally {
qexec.close();
}
This is how the result should be:
John001
High usage
John002
John003
Smith001
Moderate
Smith002
Smith003
...
Kevin001
Low usage
But fr only repeats Kevin001 and Low usage without the rest.
I've tried to put fr.put(i,result) outside the loop but that still does not give the correct result.
EDIT
I tried to print all elements from fr that shows the repeating elements.
finally {
qexec.close();
}
for (int index : fr.keySet()) {
for(Map.Entry<String, String> entry :result.entrySet()) {
System.out.println(index + " = " + entry.getKey() + " : " + entry.getValue());
}
}
UPDATE - SOLUTION
Declare HashMap inside the loop as mentioned in comments below.
To print nested HashMap, no need to use result.
I did as shown below and it prints both outermap and innermap as well.
for (int k=0; k < fr.size(); k++) {
HashMap<String,String> innermap = fr.get(k);
for(Map.Entry<String, String> e : innermap.entrySet()) {
System.out.println(k + " = " + e.getKey() + " : " + e.getValue());
}
}
You're adding the same result map to your parent map each time through the loop. Create a new instance of result each time through the loop:
Map<String, String> result = new HashMap<>();
Map<Integer, Map<String, String>> fr = new HashMap<>();
int i = 0;
try {
ResultSet rq = qexec.execSelect();
while (rq.hasNext()) {
// Create your new HashMap inside the loop:
result = new HashMap<>();
QuerySolution soln = rq.next();
id = soln.getLiteral("?id").getLexicalForm();
//...
result.put("id",id);
//...
if (soln.getLiteral("?wateruse") != null) {
wateruse = soln.getLiteral("?wateruse").getLexicalForm();
//...
result.put("wateruse",wateruse);
}
else {
System.out.println("NO");
}
fr.put(i,result);
i++;
}
}
To print the results from fr an its nested map, you can do something like this:
for (Map<String, String> map : fr.values()) {
for(Map.Entry<String, String> e : map.entrySet()) {
System.out.println(index + " = " + e.getKey()
+ " : " + e.getValue());
}
}
Try this a small change here, place the "result" map creation in while loop
Map<Integer, Map<String, String>> fr = new HashMap<>();
int i = 0;
try {
ResultSet rq = qexec.execSelect();
while (rq.hasNext()) {
Map<String, String> result = new HashMap<>();
QuerySolution soln = rq.next();
id = soln.getLiteral("?id").getLexicalForm();
//...
result.put("id",id);
//...
if (soln.getLiteral("?wateruse") != null) {
wateruse = soln.getLiteral("?wateruse").getLexicalForm();
//...
result.put("wateruse",wateruse);
}
else {
System.out.println("NO");
}
fr.put(i,result);
i++;
}
}
This for loop to print elemenets:
for (int i=0;i< fr.size();i++){
Map<String,String> element= fr.get(i);
// use the element here.
}

Java HashMap inside ArrayList

How can I get the value of HashTable inside the arrayList?
I have the following code:
public ArrayList resultSetToArrayList(ResultSet rs) {
ArrayList list = new ArrayList();
try {
ResultSetMetaData md = rs.getMetaData();
int columns = md.getColumnCount();
int rowcount = 0;
while (rs.next()) {
Hashtable row = new Hashtable();
for (int i = 1; i <= columns; ++i) {
row.put(md.getColumnName(i), rs.getString(i));
}
list.add(rowcount, row);
rowcount++;
}
} catch (Exception e) {
e.printStackTrace();
}
return list;
}
You appear to be using raw types and Hashtable instead of a HashMap. I think you're asking for something like
public List<Map<String, String>> resultSetToArrayList(ResultSet rs) {
List<Map<String, String>> list = new ArrayList<>();
try {
ResultSetMetaData md = rs.getMetaData();
int columns = md.getColumnCount();
while (rs.next()) {
Map<String, String> row = new HashMap<>();
for (int i = 1; i <= columns; ++i) {
row.put(md.getColumnName(i), rs.getString(i));
}
list.add(row);
}
} catch (Exception e) {
e.printStackTrace();
}
return list;
}
As for getting the values back out of a Map, you might iterate the Map.keySet() like
for (String key : map.keySet()) {
System.out.printf("%s = %s%n", key, map.get(key));
}

Efficient way to Handle ResultSet in Java

I'm using a ResultSet in Java, and am not sure how to properly close it. I'm considering using the ResultSet to construct a HashMap and then closing the ResultSet after that. Is this HashMap technique efficient, or are there more efficient ways of handling this situation? I need both keys and values, so using a HashMap seemed like a logical choice.
If using a HashMap is the most efficient method, how do I construct and use the HashMap in my code?
Here's what I've tried:
public HashMap resultSetToHashMap(ResultSet rs) throws SQLException {
ResultSetMetaData md = rs.getMetaData();
int columns = md.getColumnCount();
HashMap row = new HashMap();
while (rs.next()) {
for (int i = 1; i <= columns; i++) {
row.put(md.getColumnName(i), rs.getObject(i));
}
}
return row;
}
Iterate over the ResultSet
Create a new Object for each row, to store the fields you need
Add this new object to ArrayList or Hashmap or whatever you fancy
Close the ResultSet, Statement and the DB connection
Done
EDIT: now that you have posted code, I have made a few changes to it.
public List resultSetToArrayList(ResultSet rs) throws SQLException{
ResultSetMetaData md = rs.getMetaData();
int columns = md.getColumnCount();
ArrayList list = new ArrayList(50);
while (rs.next()){
HashMap row = new HashMap(columns);
for(int i=1; i<=columns; ++i){
row.put(md.getColumnName(i),rs.getObject(i));
}
list.add(row);
}
return list;
}
I just cleaned up RHT's answer to eliminate some warnings and thought I would share. Eclipse did most of the work:
public List<HashMap<String,Object>> convertResultSetToList(ResultSet rs) throws SQLException {
ResultSetMetaData md = rs.getMetaData();
int columns = md.getColumnCount();
List<HashMap<String,Object>> list = new ArrayList<HashMap<String,Object>>();
while (rs.next()) {
HashMap<String,Object> row = new HashMap<String, Object>(columns);
for(int i=1; i<=columns; ++i) {
row.put(md.getColumnName(i),rs.getObject(i));
}
list.add(row);
}
return list;
}
RHT pretty much has it. Or you could use a RowSetDynaClass and let someone else do all the work :)
this is my alternative solution, instead of a List of Map, i'm using a Map of List.
Tested on tables of 5000 elements, on a remote db, times are around 350ms for eiter method.
private Map<String, List<Object>> resultSetToArrayList(ResultSet rs) throws SQLException {
ResultSetMetaData md = rs.getMetaData();
int columns = md.getColumnCount();
Map<String, List<Object>> map = new HashMap<>(columns);
for (int i = 1; i <= columns; ++i) {
map.put(md.getColumnName(i), new ArrayList<>());
}
while (rs.next()) {
for (int i = 1; i <= columns; ++i) {
map.get(md.getColumnName(i)).add(rs.getObject(i));
}
}
return map;
}
A couple of things to enhance the other answers. First, you should never return a HashMap, which is a specific implementation. Return instead a plain old java.util.Map. But that's actually not right for this example, anyway. Your code only returns the last row of the ResultSet as a (Hash)Map. You instead want to return a List<Map<String,Object>>. Think about how you should modify your code to do that. (Or you could take Dave Newton's suggestion).
i improved the solutions of RHTs/Brad Ms and of Lestos answer.
i extended both solutions in leaving the state there, where it was found.
So i save the current ResultSet position and restore it after i created the maps.
The rs is the ResultSet, its a field variable and so in my solutions-snippets not visible.
I replaced the specific Map in Brad Ms solution to the gerneric Map.
public List<Map<String, Object>> resultAsListMap() throws SQLException
{
var md = rs.getMetaData();
var columns = md.getColumnCount();
var list = new ArrayList<Map<String, Object>>();
var currRowIndex = rs.getRow();
rs.beforeFirst();
while (rs.next())
{
HashMap<String, Object> row = new HashMap<String, Object>(columns);
for (int i = 1; i <= columns; ++i)
{
row.put(md.getColumnName(i), rs.getObject(i));
}
list.add(row);
}
rs.absolute(currRowIndex);
return list;
}
In Lestos solution, i optimized the code. In his code he have to lookup the Maps each iteration of that for-loop. I reduced that to only one array-acces each for-loop iteration. So the program must not seach each iteration step for that string-key.
public Map<String, List<Object>> resultAsMapList() throws SQLException
{
var md = rs.getMetaData();
var columns = md.getColumnCount();
var tmp = new ArrayList[columns];
var map = new HashMap<String, List<Object>>(columns);
var currRowIndex = rs.getRow();
rs.beforeFirst();
for (int i = 1; i <= columns; ++i)
{
tmp[i - 1] = new ArrayList<>();
map.put(md.getColumnName(i), tmp[i - 1]);
}
while (rs.next())
{
for (int i = 1; i <= columns; ++i)
{
tmp[i - 1].add(rs.getObject(i));
}
}
rs.absolute(currRowIndex);
return map;
}
Here is the code little modified that i got it from google -
List data_table = new ArrayList<>();
Class.forName("oracle.jdbc.driver.OracleDriver");
con = DriverManager.getConnection(conn_url, user_id, password);
Statement stmt = con.createStatement();
System.out.println("query_string: "+query_string);
ResultSet rs = stmt.executeQuery(query_string);
ResultSetMetaData rsmd = rs.getMetaData();
int row_count = 0;
while (rs.next()) {
HashMap<String, String> data_map = new HashMap<>();
if (row_count == 240001) {
break;
}
for (int i = 1; i <= rsmd.getColumnCount(); i++) {
data_map.put(rsmd.getColumnName(i), rs.getString(i));
}
data_table.add(data_map);
row_count = row_count + 1;
}
rs.close();
stmt.close();
con.close();
public static List<HashMap<Object, Object>> GetListOfDataFromResultSet(ResultSet rs) throws SQLException {
ResultSetMetaData metaData = rs.getMetaData();
int count = metaData.getColumnCount();
String[] columnName = new String[count];
List<HashMap<Object,Object>> lst=new ArrayList<>();
while(rs.next()) {
HashMap<Object,Object> map=new HashMap<>();
for (int i = 1; i <= count; i++){
columnName[i-1] = metaData.getColumnLabel(i);
map.put(columnName[i-1], rs.getObject(i));
}
lst.add(map);
}
return lst;
}

Categories