File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JDBC 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
Bookmark "Creating JAR" Watch "Creating JAR" New topic
Author

Creating JAR

A Alqtn
Greenhorn

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.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
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.

Layne


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 java.io.InputStream mis = this.getClass().getResourceAsStream(mdbFile);
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Creating JAR