dutchlab dutchlab - 2 months ago 9
Java Question

Populating Java multidimensional arrays

I'm new to Java and My example of what I am attempting to accomplish is a ColdFusion structure. Using the following data structure as an example.

id color
50 blue
60 red
70 green
80 yellow


I want to create a Java multidimensional array from a query resultSet that looks like this.

1 id 50
color blue
2 id 60
color red
3 id 70
color green
4 id 80
color yellow


My goal is to use the returned array to populate a JSP page with data. I am attempting to allow my code to accommodate varying numbers of rows returned by a query. My current code is as follows.

private static String[][][] query(Connection connection) throws SQLException{

Statement statement = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
ResultSet resultSet = statement.executeQuery("SELECT colors.id,colors.color FROM colors");
int size;
resultSet.last();
size = resultSet.getRow();
resultSet.beforeFirst();

ResultSetMetaData rsltmetadata = resultSet.getMetaData();
int colCount = rsltmetadata.getColumnCount();

String[][][] rlstSetArray = new String[size][colCount][];

int m = 0;
while(resultSet.next()) {
++m;
for (int j=1; j <= colCount; j++){
System.out.println(m);
System.out.println(rsltmetadata.getColumnName(j));
System.out.println(resultSet.getString(rsltmetadata.getColumnName(j)));

}
}
}


This code produces this out put.

1
id
50
1
color
blue
2
id
60
2
color
red


etc...

In place of the three System.out.println lines have tried the following options and receive the same message from NetBeans 8.1. 'incompatible types: String cannot be converted to int'

rlstSetArray[m][rsltmetadata.getColumnName(j)] = resultSet.getString(rsltmetadata.getColumnName(j));

rlstSetArray[m][rsltmetadata.getColumnName(j)] = "'" + resultSet.getString(rsltmetadata.getColumnName(j)) + "'";

rlstSetArray[m]["'" + rsltmetadata.getColumnName(j) + "'"] = resultSet.getString(rsltmetadata.getColumnName(j));


What is causing this message?

Is this going to allow me to retrieve data from the array in the JSP using the column names that are in the second dimension of the array?

Answer

What about to create POJO wrapper class for your result set instead of 3-dimensional array?

public class Color {
    private Integer id;
    private String name;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

Then the query method could be simplified:

private static List<Color> query(Connection connection) throws SQLException{
    List<Color> colors = new LinkedList();
    ...
    while(resultSet.next()) {
        Color color = new Color();
        color.setName(resultSet.getString(rsltmetadata.getColumnName("color"));
        color.setId(resultSet.getString(rsltmetadata.getColumnName("id"));
        colors.add(color);
    }
    return colors;
}
Comments