These chapters have been written down for a fast setup and result in viewing MS-Access with Java JDBC stand-alone. Step by step we'll discuss what to do with the following components.
JDK 1.1.
JDBC-ODBC Bridge.
System environment.
32 Bits ODBC Driver configuration.
Java Code for accessing database.
| 1. | JDK 1.1 | |
| Where to get Java Development Kit? http://java.sun.com, version 1.1 incudes the java.sql.* and sun.jdbc.odbc.* drivers. Install them under the d:/java directory. |
||
| Back to top | ||
| 2. | JDBC-ODBC Bridge | |
Consists of two packages. |
||
| Back to top | ||
| 3. | SYSTEM ENVIRONMENT | |
This process creates the following subdirectory structure beneath your Java home directory: Directory Contains
--------- ----------------------------------------------
java\sql The JDBC API classes provided by JavaSoft
sun\jdbc\odbc The JDBC-ODBC Bridge classes, libraries, and
sample source code
|
||
| Back to top | ||
| 4. | 32-BITS ODBC DRIVER CONFIGURATION | |
Now add System DSN (Data Source Name) by pressing System DSN and Add. Select Microsoft Access Driver (*.mbd). Fill out the form; Data Source Name, Description and press select to make link to the actual database on your harddrive. The Data Source Name we'll use in our Java Source Code to make a connection with the ODBC. Our settings are: data source name = Noorderwind (Northwind)
description = default database for ms-access
select path = c:/msoffice/access/nwind.mdb
|
||
| Back to top | ||
| 5. | Java Code For Accessing Database | |
public NorthWind() {
// Load the JDBC-ODBC bridge driver
try {
Class.forName ("sun.jdbc.odbc.JdbcOdbcDriver");
} catch( ClassNotFoundException ee) {
ee.printStackTrace();
}
}
static void open() throws SQLException {
// ODBC data source name
String dsn = "jdbc:odbc:Nooderwind";
String user = "admin";
String password = "";
// Connect to the database
con = DriverManager.getConnection(dsn, user, password);
// Shut off autocommit
con.setAutoCommit(false);
}
static void select(String whereClause) {
Statement stmt; // SQL statement object
String query; // SQL select string
ResultSet rs; // SQL query results
boolean more; // "more rows found" switch
String v1, v2, v3; // Temporary storage results
results = new Vector( 10 );
query = "SELECT EmployeeId, LastName, FirstName, Title "
+ "FROM Employees "
+ whereClause;
try {
stmt = con.createStatement();
rs = stmt.executeQuery(query);
// Check to see if any rows were read
more = rs.next();
if (!more) {
System.out.println("No rows found.");
return;
}
// Loop through the rows retrieved from the query
while (more) {
v1 = "ID: " + rs.getInt("EmployeeId");
v2 = "Name: " + rs.getString("FirstName") + " " + rs.getString("LastName");
v3 = "Title: " + rs.getString("Title");
System.out.println( v1 );
System.out.println( v2 );
System.out.println( v3 );
System.out.println("");
results.addElement( v1 + "\n" + v2 + "\n" + v3 + "\n");
more = rs.next();
}
rs.close();
stmt.close();
} catch (SQLException e) {
System.out.println("" + results.size() + " results where found.");
}
|
||
| Back to top | ||