tags:

views:

41

answers:

1

I am using JDBC to query a MySQL database for specific records from two tables.

My MySQL query sample is this:

SELECT
r.name
  , r.network
  , r.namestring
  , i.name
  , r.rid
  , i.id
  , d.dtime
  , d.ifInOctets
FROM router AS r
INNER JOIN interface AS i
ON r.rid = i.rid
INNER JOIN 1278993600_1_60 AS d
ON i.id = d.id
AND dtime BETWEEN 1279027200 AND 1279029000
WHERE r.network = "ITPN"
AND i.status = "active"
WHERE i.id BETWEEN 1418 AND 1518

Now is it possible for me to add all the specific records which I need:

  r.name
, r.network
, r.namestring
, i.name
, r.rid
, i.id
, d.dtime
, d.ifInOctets

to an array specifically and print it out to an excel sheet?

I tried creating different classes for the 3 tables: r, i, d. Then as I queried the database I created objects and added them to ArrayLists, which I printed out to a different sheet. (The goal is to print it out to an excel sheet)

But this method is making me store and iterate over millions of objects.

Is there any method to add all records dynamically in an easier less time consuming way and send it to the sheet?

+2  A: 

It would be something like that:

PreparedStatement stmt = cnt.prepareStatement("...");
try {
    /* ... */
    ResultSetMetaData meta = stmt.getMetaData();
    Object row[] = new Object[meta.getColumnCount()];
    ResultSet rs = stmt.executeQuery();
    while (rs.next()) {
        for (int i = 0; i < row.length; ++i) {
            row[i] = rs.getObject(i+1);
        }
        processRow(row);
    }
} finally {
    stmt.close();
}
Maurice Perry
hi, thanks.Is it possible to get the whole column in an Array? I was trying to use rs.getArray() function with no luck.Also if there are multiple data ids which map to the same time what could I use to keep track of it as every id would have a different list of values???
jillika iyer
Loop through the resultset and add the column value to an `ArrayList` on every iteration.
BalusC
Yes but if you have millions of rows, I wouldn't recommend it
Maurice Perry