Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to Print HashMaps 1

Status
Not open for further replies.

jtfrier

Technical User
Jan 12, 2006
85
0
0
This post is related to another post Clean up on Database
I think I got the solution to that problem. But I am having trouble figuring how how to print my HashMap to a table or XLS sheet. Here is my code This is my first java program so pleas give detail.
Code:
//Concatenate.java
//adds decription fields together
import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;

public class Concatenate
{
	private static Connection connection;
	private static Statement statement;
	private static HashMap map = new HashMap();
	static ArrayList Data;
	public static void main(String args[])
	{
		try
		{
			Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
			connection = DriverManager.getConnection("jdbc:odbc:manman","","");
			statement = connection.createStatement();		
			String Query ="SELECT test1.partno, test1.lineno, test1.Desc FROM test1";
			ResultSet result = statement.executeQuery(Query);
			while(result.next()) 
			{ 
				// process results one row at a time                                                                    
	            String partno = result.getString("partno");
	            String lineno = result.getString("lineno");
	            String desc = result.getString("Desc");
	                        
	            if (map.containsKey(partno))
	            {	            
	            	Data.add ("," + desc);				
	            }
	            else
			    {            	
			    	Data = new ArrayList();
			    	Data .add(desc);
			    	map.put(partno,Data);
			    }
			}
		}
		catch(Exception E)
		{
		}				
	}
}
 
a) a bad idea is, to make all members static, and bypass the constructor, to do everything in main.

instead, use a constructor:
Code:
 public class Concatenate
{
	private Connection connection;
	private HashMap map = new HashMap();
        // lower case: 'data'
	private ArrayList data;

	public Concatenate ()
	{
		/* we don't need the statement in the whole class - do we? */
		Statement statement;
		// ...
		// do your thing here
	}

	public static void main (String args[])
	{
		new Concatenate ();
	}
}

b) don't ignore exceptions:
Code:
        // again: lower case 'e'
        catch(Exception e)
        {
                e.printStackTrace ();
        }
c) if you don't sort the sql-result by partno, you will get random results.

d)
Code:
 if (map.containsKey(partno))
	{                
	    Data.add ("," + desc);                
	}
Well - if the map contains the key 'partno', let's get the data:
Code:
 if (map.containsKey(partno))
	{                
		data = map.get (partno);                
		// data isn't a String - adding ", " is an output - issue
		data.add (desc);
	}
Not tested, but should be the right way.

Before printing to a Table (JTable?) or to XLS (not an issue to start with), you should try to print to stdout.

Can you do it?

seeking a job as java-programmer in Berlin:
 
Thank you so much for your help this has been a hard project for me. This was my first program in Java thank you again.
 
Ok every thing is working great i found that Iterator that let me show my resluts and it is working great. I now need to export it to table I think that im goign to need another loop in my wile statment were my iterator is and some kind of string like a buffer that I append to an array to send it to the table I think im way off. Just need to be pointed in the right direction.
Code:
//Concatenate.java
//adds decription fields together
import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
public class Concatenate
{
	private static Connection connection;
	private static Statement statement;
	private static HashMap map = new HashMap();
	static ArrayList Data;
	public static void main(String args[])
	{
		try
		{
			Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
			connection = DriverManager.getConnection("jdbc:odbc:manman","","");
			statement = connection.createStatement();		
			String Query ="SELECT test1.partno, test1.lineno, test1.Desc FROM test1";
			ResultSet result = statement.executeQuery(Query);
			while(result.next()) 
			{ 
				// process results one row at a time                                                                    
	            String partno = result.getString("partno");
	            String lineno = result.getString("lineno");
	            String desc = result.getString("Desc");
	                        
	            if (map.containsKey(partno))
	            {	            
	            	Data = (ArrayList) map.get(partno);
	            	Data.add(desc);
	            }
	            else
			    {            	
			    	Data = new ArrayList();
			    	Data .add(desc);
			    	map.put(partno,Data);			    	
			    }
			}
		}
		catch(Exception e)
		{
		
		}	
		Iterator it = map.entrySet().iterator();
		while (it.hasNext()) 
		{
		            Map.Entry pairs = (Map.Entry)it.next();
		            System.out.println(pairs.getKey());
		            System.out.println(pairs.getValue());
		}

	}
}
 
Code:
while (it.hasNext())
{
	Map.Entry pairs = (Map.Entry)it.next();
	// key is the partno ...
	System.out.println (pairs.getKey());
	// and value is data (data, not Data!)
	data = (ArrayList) (pairs.getValue());
	StringBuffer sb = new StringBuffer ();
	for (int i = 0; i < data.size (); ++i)
	{
		sb.append (data.get (i));
		if (i < (data.size () - 1))
			sb.append (", ");
	}
}

seeking a job as java-programmer in Berlin:
 
man you rock thank you for all your help every thing is working great. I got it printing to table now thanks again sooooo much.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top