Hi! I want to read a table from Access to Java.
This is the table:
http://kepfeltoltes.hu/090221/heights_www.kepfeltoltes.hu_.jpg
I tried to do it with HashMap, but I have a problem, and I don't even know what im doing is right, I have no practice in this, altough I've been trying to find a solution for like a week. I made a class for the column names, like this:

class HeightStats{
     public int agee;
     public int heightt1;
     public int heightt2;
     public int heightt3;
     public int heightt4;
     public int heightt5;
     public int heightt6;
     
    
    public HeightStats(int age, int h1, int h2, int h3, int h4, int h5, int h6){
                agee = age; 
                heightt1=h1;                 
                heightt2=h2; 
                heightt3=h3;
                heightt4=h4;                 
                heightt5=h5; 
                heightt6=h6;
  }
 }

This is my code:

String sql_mag = "SELECT * FROM STATS";
              try
              {
                 Statement statement = con.createStatement ();
            ResultSet rs;
            rs = statement.executeQuery (sql_mag);
        
            int index=0;
                    if (rs != null) 
                     {
                            while (rs.next())
                            {
                            HashMap hMap = new HashMap();
                            String s = hMap.toString();
                            hMap.put(index,new HeightStats(rs.getInt(1),rs.getInt(2),rs.getInt(3),rs.getInt(4),rs.getInt(5),rs.getInt(6), rs.getInt(7)));
                            index++;
                            Collection c;
                            c = hMap.values();
                            Iterator itr = c.iterator();
                  
                           System.out.println(hMap.toString());
                     
                    }              
                }
                rs.close ();
                statement.close();
            } catch (Exception ex) 
            {
                System.out.println("Error reading database information!");
                System.out.println(ex);
            }
        }

And the output says:
Success!
{0=project1.HeightStats@a0dcd9}
{1=project1.HeightStats@1034bb5}
{2=project1.HeightStats@15f5897}
{3=project1.HeightStats@b162d5}
{4=project1.HeightStats@1cfb549}

Any help is appreciated.

Comments
Belongs to rare species of rookies who use code tags in first post

Create a toString method in that HeightStats close to control how it "prints".

I tried it before but I don't know how. Could you write down?

class HeightStats{
...
public String toString() {
return "Age= " + age + " heights= " + h1 + " "  h2 ..... ;
}

To simplify things, I just wrote: return age;
It says:
Error(41,12): field age not found in class project1.HeightStats

And I did a HeightStats hs = new HeightStats(); in while cycle where HashMap is defined and altered the printout line to this:
System.out.println(hs.toString());

Here the problem is:
Error(98,50): constructor HeightStats() not found in class project1.HeightStats

Can U help?

Sorry, I wrote "age" when it should have been "agee".
(It would be better to call it "age" and in the constructor you say
this.age = age; ).
Your simplification breaks the code! agee is an int, and the method returns String. By saying
"something" + someInt
you force the int to be converted to String.
Constructor HeightStats() is not found becuase you haven't created it. Your constructor is HeightStats(int age ...)
Finally, you don't need to do println(hs.toString()); because println automatically does a toString() on whatever you pass to it. Just stick with println(hs);

Thank U!
System.out.println(mk.toString()); at last System.out.println(mk.toString()); didn't print it out, so i used

while (itr.hasNext()) 
                             {
                                Map.Entry me = (Map.Entry)itr.next();
                                System.out.println(me.getKey() + " : " +  me.getValue());
                             }

Which says:

0 : Age= 15 heights= 168 172 170 176 170 168
0 : Age= 16 heights= 168 171 173 165 172 174
0 : Age= 17 heights= 173 166 177 175 173 170
0 : Age= 18 heights= 170 180 175 179 177 176
0 : Age= 19 heights= 175 170 169 174 179 180

1. Why are the keys all 0?
2. How can it print out e.g. the bold number?

That System.out.println(mk.toString()) is System.out.println(hs.toString()), I can't alter my post anymore.

1. Because when you add the entry to the HashMap you never change index from its initial value of 0.
2. You need to get the values one at a time - eg hs.heightt1, hs.heightt2 etc. You may find this gets easier if you replace those 5 ints by a single array with 5 elements height[0], height[1] etc

Yeah, I know, that would have been my 3. question :)
So instead public int heightt1; public int heightt2; I wrote
public int heightt[] = new int[6]; and instead heightt1=h1; heightt2=h2; I wrote heightt[0]=ht[1]; heightt[1]=ht[2];....but what should i do with this line:

public Magassagok(int age, int h1, int h2, int h3, int h4, int h5, int h6){

because ht hasn't been declared yet.
and the return line and so on?

I simply cant find an answer in tutorials.

That's ok as it is, unless you find some reason to change it. Obviously the following code changes to

height[0] = h1; // etc

and the toString method returns

"Age= " + age + " heights= " +height[0]  ..... ;
Comments
Helpful

Thank you.
1. And now how can I print out an element, like the bold number in my previous post?
2. How can I alter the elements, like adding a certain number to all?
3. Is there any other way to handle the matrix like the usual

for (int i=0; i<6; i++)
{
    for (int j=0; j<5; j++)
    {
    }
}
I would like to move elements from a row to another and a lot of things, and with this solution it seems pretty difficult.

1. eg: System.out.println(height[2]); 2.

for (int i=0; i<5; i++) {
  height[i] += 99;  // add 99 to each element
}

3. Don't understand question. If you want to do complicated math on whole matrices, you'll find a load of Java packages you can get from the web to do it for you. Google is your friend!
In general, arrays are an essential basic part of any programming language. Look for some tutorials on the web, and give it a go. Once you get the hang of it, it's really easy.

This article has been dead for over six months. Start a new discussion instead.