我想使用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();
}
}
感谢您的帮助。
答案 0 :(得分: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"));
}
只需执行另一个查询即可从下一页获取结果