Win a copy of Svelte and Sapper in Action this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Creating JAR

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can try this.getClass().getResource("../yourMDBFIle.mdb");
 
Wei Dai
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If it's in jar file, you can try java.io.InputStream mis = this.getClass().getResourceAsStream(mdbFile);
 
Did you miss me? Did you miss this tiny ad?
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic