简体   繁体   English

jdbc-Java-方法查询

[英]jdbc - java - method query

I am using JDBC to query a MySQL database for specific records from two tables. 我正在使用JDBC从两个表中查询MySQL数据库以获得特定记录。

My MySQL query sample is this: 我的MySQL查询示例是这样的:

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? 专门用于数组并将其打印到Excel工作表中?

I tried creating different classes for the 3 tables: r , i , d . 我尝试为3个表创建不同的类: rid Then as I queried the database I created objects and added them to ArrayList s, which I printed out to a different sheet. 然后,当我查询数据库时,我创建了对象并将其添加到ArrayList ,并打印到另一张纸上。 (The goal is to print it out to an excel sheet) (目标是将其打印到Excel工作表中)

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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