简体   繁体   中英

How to use two excel sheet to read data in java using JDBC-ODBC?

I want to read the data from two excel sheets using JDBC. So i have setup two data sources(system DSN names) for both excel sheets.Now i'm able to read the data from one excel sheet ? But how to connect the second the excel sheet ?

Please see the below coding to read the data from one excel sheet.But how to use second data source name in the second connection ? Can you please advise.

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

Thanks for help.

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

Just execute the other query to get the results from the next sheet

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