Dinidu Dinidu - 4 months ago 12
Java Question

How to get database table values in to java class values?

I have a class which contains all the columns in a table as variables. I want to get all the table values as objects of that class. Here is my table structure,

|col1 | col2 | col3 |
|---------------------|
|a1 | b1 | c1 |
|a2 | b2 | c2 |


My class is,

public class tab_class{
String col1;
String col2;
String col3;
}


What I want is I want to get the values of the table and store these values in each class variable. Lets say I have a result set of this table called 'rs', then I do some thing like this,

tab_class tbcl = new tab_class();
while(rs.next()){
tbcl.col1 = rs.getString("col1");
tbcl.col2 = rs.getString("col4");
tbcl.col3 = rs.getString("col3");
}


Now I have an object of 'tab_class' with values of a row of the table.
Now I want multiple instances for each row of the table.

How can I achieve this. Your ideas are highly appreciated.

Answer

Your code has a problem:

tab_class tbcl = new tab_class();
while(rs.next()){
   tbcl.col1 = rs.getString("col1");
   tbcl.col2 = rs.getString("col4");
   tbcl.col3 = rs.getString("col3");
}
  • You create just one and only one tab_class object when you will actually need to create one object for each row.
  • This means that when the while loop is done, only one object has been created, and it holds only the data returned from the last row of the result set.
  • You're directly accessing the fields of tab_class, something that breaks the OOPs principle of encapsulation.

I think that you need to simply create an ArrayList of objects of your type of interest, since you will need to create a new tab_class object for each row in the ResultSet. For example:

List<tab_class> tabList = new ArrayList<>();
while(rs.next()){
   String col1 = rs.getString("col1");
   String col2 = rs.getString("col4");
   String col3 = rs.getString("col3");
   tabList.add(new tab_class( col1, col2, col3); // assuming you have this type of constructor
}

This will create an ArrayList that holds all the rows from the table as objects of your tab_class. Again, this assumes that tab_class has a constructor that takes 3 Strings. If it doesn't then you can call setter methods. Whatever you do, do not make tab_class fields public or set them directly as that flies against OOP principles.

As an aside, you will want to learn and use Java naming conventions. Variable names should all begin with a lower letter while class names with an upper case letter. Learning this and following this will allow us to better understand your code, and would allow you to better understand the code of others.

So for example, the tab_class could be:

class MyTab {
    // I use constants or an enum to avoid typo errors
    public static final String COLUMN_1 = "col1";
    public static final String COLUMN_2 = "col2";
    public static final String COLUMN_3 = "col3";

    private String col1;
    private String col2;
    private String col3;

    public MyTab(String col1, String col2, String col3) {
        this.col1 = col1;
        this.col2 = col2;
        this.col3 = col3;
    }

    public String getCol1() {
        return col1;
    }

    public String getCol2() {
        return col2;
    }

    public String getCol3() {
        return col3;
    }

    // you'll want an equals, haschCode and toString methods

}

And the loop:

    List<MyTab> tabList = new ArrayList<>();
    while (rs.next()) {
        // I use constants or an enum to avoid typo errors
        String col1 = rs.getString(MyTab.COLUMN_1);
        String col2 = rs.getString(MyTab.COLUMN_2);
        String col3 = rs.getString(MyTab.COLUMN_3);
        tabList.add(new MyTab(col1, col2, col3));
    }