Hello Everyone,
I have been working with a simple application that reads a table rows from MS Access database. The code is shown below:
import java.io.File;
import java.sql.DriverManager;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.Connection;
import java.sql.SQLException;
public class ReadingAccessDB {
public static void main(String[] args) {
Connection conn = null;
try {
// 1) Load the driver
String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
Class.forName(driver);
// 2) Obtain a connection to the contacts database
File file = new File("carpets.mdb");
String databaseFile = file.getAbsolutePath();
System.out.println("database path " + databaseFile);
String url = "jdbc dbc river={Microsoft Access Driver (*.mdb)};DBQ=" + databaseFile;
conn = DriverManager.getConnection(url, "", "");
// 3) Create a JDBC statement for executing SQL expressions
Statement statement = conn.createStatement();
// 4) Execute an SQL expression and save the result in a ResultSet
ResultSet result = statement.executeQuery("select * from members");
// 5) Maniplute the result
System.out.println("Got Results:");
System.out.println("Member ID\tFirst Name\tLast Name");
System.out.println("---------\t----------\t---------");
while (result.next()) {
int memberID = result.getInt("memberID");
String firstName = result.getString("firstName");
String lastName = result.getString("lastName");
System.out.println(memberID + "\t" + firstName + "\t" + lastName);
}
}
catch (ClassNotFoundException ex) {
System.out.println("Failed to load MS Access driver");
}
catch (SQLException ex) {
System.out.println("SQL Exception: " + ex.getMessage());
ex.printStackTrace();
}
finally {
// 6) Close the connection
if( conn != null ) {
try {
conn.close();
}
catch (SQLException ex) {
ex.printStackTrace();
}
}
}
}
}
The above code packages the class file (ReadingAccessDB.class) in jdbc folder. now when I run this file, I usually move the "carpets.mdb" file one level above the jdbc folder. for example if the class file is in C:\jdbc\ReadingAccessDB.class, I put the "carpets.mdb" in C:\
When running the class file I use "java jdbc.ReadingAccessDB" and it runs fine. My problem is now how can archive this application with its database file and make it executable so that when wherever I move the JAR file it should run.
Alqtn