Read data from Excel : Excel : Database SQL JDBC : Java examples (example source code) Organized by topic

Java
C++
PHP


Java  »  Database SQL JDBC   » [  Excel  ]  Screenshots 
 



Read data from Excel

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

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

  public static void main(String args[]) {
    Connection conn = null;
    Statement stmt = null;
    ResultSet rs = null;
    try {
      conn = getConnection();
      stmt = conn.createStatement();
      String excelQuery = "select * from [Sheet1$]";
      rs = stmt.executeQuery(excelQuery);

      while (rs.next()) {
        System.out.println(rs.getString("BadgeNumber"" " + rs.getString("FirstName"" "
            + rs.getString("LastName"));
      }
    catch (Exception e) {
      System.err.println(e.getMessage());
    finally {
      try {
        rs.close();
        stmt.close();
        conn.close();

      catch (SQLException e) {
        e.printStackTrace();
      }
    }
  }
}
           
       
Related examples in the same category
























Home| Contact Us
Copyright 2003 - 07 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.