开发者

jdbc - java - method query

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?


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();
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜