140 likes | 265 Views
JDBC-ODBC Bridge. Database Project. Contents. Microsoft Excel Microsoft Access. Microsoft Excel. JDBC-ODBC Bridge. Excel File. The Data Sources Administrative Tool. Add an Excel File Data Source. Choose the Microsoft Excel Driver. Select the Workbook as a Data Source.
E N D
JDBC-ODBC Bridge Database Project
Contents • Microsoft Excel • Microsoft Access JDBC-ODBC Bridge
Microsoft Excel JDBC-ODBC Bridge
Excel File JDBC-ODBC Bridge
The Data Sources Administrative Tool JDBC-ODBC Bridge
Add an Excel File Data Source JDBC-ODBC Bridge
Choose the Microsoft Excel Driver JDBC-ODBC Bridge
Select the Workbook as a Data Source JDBC-ODBC Bridge
Specifying a Name for the Data Source JDBC-ODBC Bridge
Confirm the Data Source Was Established JDBC-ODBC Bridge
JDBC Code import java.io.*; import java.net.*; import java.sql.*; import java.util.*; public class InteractWithExcel { public static void main(String[] args) { try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection conn = DriverManager.getConnection("jdbc:odbc:EXCELJDBCTest"); Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery("select State,Population from [Sheet1$]"); while (rs.next()) { String state = rs.getString(1); int population = rs.getInt(2); System.out.println(state + " - " + population); } rs.close(); stmt.close(); } catch (Exception e) { e.printStackTrace(); } } } JDBC-ODBC Bridge
Other SQLs • Aggregation queries • Select sum(population) from [MySheetName$] • Update queries • Insert into [Sheet1$] values(‘Illinois’, 12600620); JDBC-ODBC Bridge
Microsoft Access JDBC-ODBC Bridge
Source Code import java.sql.*; public class Test { public static void main(String[] args) { try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); String dataSourceName = "mdbTEST"; String dbURL = "jdbc:odbc:" + dataSourceName; Connection con = DriverManager.getConnection(dbURL, "",""); Statement s = con.createStatement(); s.execute("create table TEST12345 ( column_name integer )"); s.execute("insert into TEST12345 values(1)"); s.execute("select column_name from TEST12345"); ResultSet rs = s.getResultSet(); while ( rs.next() ) System.out.println("Data from column_name: " + rs.getString(1) ); s.execute("drop table TEST12345"); s.close(); con.close(); } catch (Exception e) { e.PrintStackTrace(); } } } JDBC-ODBC Bridge