The Excel ODBC source is represented by "jdbc:odbc:BulkMail", and the Access ODBC source is represented by "jdbc:odbc:Whisky" in the code.
import java.sql.*; public class ExcelToAccess { public static void main (String[] args) { try { Class.forName ("sun.jdbc.odbc.JdbcOdbcDriver"); String sourceURL = "jdbc:odbc:BulkMail"; String sourceAccess = "jdbc:odbc:Whisky"; Connection dbConnection = DriverManager.getConnection(sourceURL); Connection dbAccess = DriverManager.getConnection(sourceAccess); String comp; String fax; Statement st = dbConnection.createStatement(); Statement stAccess = dbAccess.createStatement(); ResultSet rs = st.executeQuery("Select LastName, FirstName, Company, FaxNumber from Customers"); while (rs.next()){ comp = rs.getString ("Company"); fax = rs.getString ("FaxNumber"); stAccess.executeUpdate("INSERT INTO Company (CompanyName, FaxNumber) VALUES('"
+ comp + "','" + fax + "')"); } rs.close(); dbConnection.close(); dbConnection =null; dbAccess.close(); dbAccess = null; } catch(SQLException sqle) { System.err.println(sqle); } catch(ClassNotFoundException cnfe) { System.err.println(cnfe); } } }
No comments:
Post a Comment