try
{
//Class.forName("sun.jdbc.odbc.jdbcodbcdriver");
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch (ClassNotFoundException e)
{
System.out.println("ERROR: Database driver not found!");
e.printStackTrace();
return;
}
(2) the above code will throw exception "class not found" using jdk 1.8
because it was removed:
http://docs.oracle.com/javase/7/docs/technotes/guides/jdbc/bridge.html
The JDBC-ODBC Bridge allows applications written in the Java programming language to use the JDBC API with many existing ODBC drivers. The Bridge is itself a driver based on JDBC technology ("JDBC driver") that is defined in the class sun.jdbc.odbc.JdbcOdbcDriver. The Bridge defines the JDBC sub-protocol odbc.
Status of the JDBC-ODBC Bridge
The JDBC-ODBC Bridge should be considered a transitional solution; it will be removed in JDK 8. In addition, Oracle does not support the JDBC-ODBC Bridge. Oracle recommends that you use JDBC drivers provided by the vendor of your database instead of the JDBC-ODBC Bridge.
Why use ODBC at all?
The ideal is "Pure Java": no native code, no platform dependent features. But you may need to begin your development effort right away, without waiting for your DBMS to offer a Java-only JDBC driver. Partly native drivers, such as the JDBC-ODBC Bridge, let you create programs that easily adapt to Pure Java drivers as they become available.
(3) so you could try getting the old .jar file from older jdk1.7 or you can use "jackcess"
http://jackcess.sourceforge.net/
Sample code
Here are a few snippets of code to whet your appetite. For more extensive examples, checkout the cookbook. And, since Jackcess is heavily unit tested, you can find even more example code in the unit tests.
- Iterating through the rows of a table:
Table table = DatabaseBuilder.open(new File("my.mdb")).getTable("MyTable"); for(Row row : table) { System.out.println("Column 'a' has value: " + row.get("a")); } - Searching for a row with a specific column value:
Row row = CursorBuilder.findRow(table, Collections.singletonMap("a", "foo")); if(row != null) { System.out.println("Found row where 'a' == 'foo': " + row); } else { System.out.println("Could not find row where 'a' == 'foo'"); } - Creating a new table and writing data into it:
Database db = DatabaseBuilder.create(Database.FileFormat.V2000, new File("new.mdb")); Table newTable = new TableBuilder("NewTable") .addColumn(new ColumnBuilder("a") .setSQLType(Types.INTEGER)) .addColumn(new ColumnBuilder("b") .setSQLType(Types.VARCHAR)) .toTable(db); newTable.addRow(1, "foo"); - Copying the contents of a JDBC ResultSet (e.g. from an external database) into a new table:
Database db = DatabaseBuilder.open(new File("my.mdb")); new ImportUtil.Builder(db, "Imported").importResultSet(resultSet); db.close(); - Copying the contents of a CSV file into a new table:
Database db = DatabaseBuilder.open(new File("my.mdb")); new ImportUtil.Builder(db, "Imported2").setDelimiter(",").importFile(new File("my.csv")); db.close();
(3) to get jackcess-2.0.4.jar to work i had to also get
commons-logging-1.1.3.jar
and
commons-lang-2.6.jar
http://commons.apache.org/proper/commons-lang/download_lang.cgi
http://commons.apache.org/proper/commons-logging/
No comments:
Post a Comment