0

我想使用 JDBC 从两个 excel 表中读取数据。所以我为两张excel表设置了两个数据源(系统DSN名称)。现在我可以从一张excel表中读取数据了吗?但是如何连接第二个excel表?

请参阅下面的编码以从一个 Excel 表中读取数据。但是如何在第二个连接中使用第二个数据源名称?你能给些建议么。

public class MergeXSLFiles {

public static Connection getConnection() throws Exception {
String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
String url = "jdbc:odbc:Report";
String username = "";
String password = "";
Class.forName(driver);
return DriverManager.getConnection(url, username, password);
}

public static void main(String args[]) throws Exception {
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;

conn = getConnection();
stmt = conn.createStatement();
String excelQuery = "select * from [Sheet$]";
rs = stmt.executeQuery(excelQuery);

while (rs.next()) {
  System.out.println(rs.getString("name") + " " + rs.getString("age"));
}

rs.close();
stmt.close();
conn.close();
}
}

感谢帮助。

4

1 回答 1

0
String excelQuery = "select * from [Sheet$]1";
String excelQuery2 = "select * from [Sheet$]2";
rs = stmt.executeQuery(excelQuery);

while (rs.next()) {
  System.out.println(rs.getString("name") + " " + rs.getString("age"));
}
rs = stmt.executeQuery(excelQuery2);
while (rs.next()) {
  System.out.println(rs.getString("name") + " " + rs.getString("age"));
}

只需执行另一个查询即可从下一张表中获取结果

于 2012-12-01T22:17:11.620 回答