Two Laptop Bag
The moose likes JDBC and Relational Databases and the fly likes Creating JAR Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "Creating JAR" Watch "Creating JAR" New topic

Creating JAR

A Alqtn

Joined: Jan 02, 2006
Posts: 14
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.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";

// 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");
while ( {
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());
finally {
// 6) Close the connection
if( conn != null ) {
try {
catch (SQLException ex) {

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.

Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Are you asking how to open and manipulate the Access DB file if it is packaged inside the JAR file? If so, I don't think it is possible to access it directly. A potential work-around is to write code that extracts the DB file from the JAR and then use it from the regular file system. You could then put it back into the JAR when you are done with it. This seems like more trouble than it is worth, in my opinion. But I guess that's up to you to decide.


Java API Documentation
The Java Tutorial
Wei Dai
Ranch Hand

Joined: Jun 22, 2005
Posts: 86
You can try this.getClass().getResource("../yourMDBFIle.mdb");
Wei Dai
Ranch Hand

Joined: Jun 22, 2005
Posts: 86
If it's in jar file, you can try mis = this.getClass().getResourceAsStream(mdbFile);
I agree. Here's the link:
subject: Creating JAR
It's not a secret anymore!