简体   繁体   中英

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 ArrayList s, 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();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM