db-derby-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Annetta C. Green" <victorianladytur...@gmail.com>
Subject RE: Storing/Accessing a Derby database inside a JAR
Date Sun, 16 Apr 2006 20:24:35 GMT
Daniel,

>I'm really at a loss here. Can anyone tell me what I'm doing wrong? 

>I've read through all the relevant sections of the Derby documentation, and
I'm not really sure what to do next. 

Try loading the database properties from a configuration file in the jar
file.  

The basics of the following code fragments are in the article (the url
below) - the structure and the configuration.properties file definitions
came straight from the code in the article - the original author does a
great job of explaining how it works.
Make the configuration properties file in Notepad.  Save it with the name
"Configuration.properties" - it must be in the same folder as the class file
calling it unless the fully qualified path is used.

There is a very good example at this site:
http://java.sun.com/developer/technicalArticles/J2SE/Desktop/javadb/

The configuration file has the following lines: (there are no spaces on
either side of the equals (=) sign

#Configuration properties resource file
db.path=jar:C:/project/NameList/NameGenerator.jar
db.name=NameDB
db.url=jdbc:derby:
derby.driver=org.apache.derby.jdbc.EmbeddedDriver

/**** This is the class file for connection to the database  ***/
String dbPath;
String dbName; 
String systemDir;
String configProperties = "Configuartion.properties";

public ClassFileName(String dbPath, String dbName)
{
	this.dbPath = dbPath;
	this.dbName = dbName;
	loadMyProperties();
	String driverName = dbProperties.getProperty("derby.driver");
	loadDBDrivers(driverName);
}//end constructor 

//load db properties
public void loadMyProperties()
{
      //load the dbProperties

      //use a try and catch here
      InputStream input =
ClassFileName.class.getResourceAsStream(configProperties); 
      Properites dbProperties = new Properties();

      //set the directory for derby and the database
      systemDir = dbPath + dbName;
      //Try a System.out.println("\nsystemDir: " + systemDir); to make sure
the string has correct syntax for OS
      System.setProperty("derby.system.home", systemDir);

      try
      {
		dbProperties.load(input)
      }//end try
      catch(IOException ex)
      {
		Ex.printStackTrace();
      }//end catch
}//end loadMyProperties

//load the driver
public void loadDBDrivers(String driverName)
{
	//load derby driver
	try
	{
		Class.forName(driverName);
	}//end try
	catch(ClassNotFoundException cne)
	{
		cne.printStackTrace();
      }//end catch
}//end loadDBDrivers

//make the connection
public void makeConnection()
{
	String dbUrl = dbProperties.getProperty("db.url" + dbName);
	Connection dbConnection = null;

	try
	{
		dbConnection = DriverManager.getConnection(dbUrl,
dbProperties);
		//anything else that needs to be done in the connection goes
here
      }//end try
      catch (SQLException se)
      {
		se.printStackTrace();
	}//end catch
}//end makeConnecton


// ... whatever methods needed for the class



>Also, once I get this sorted out, is there a way to include derby.jar
inside NameGenerator.jar so that just the one file could be distributed?

Try putting the derby.jar in the class path of the NameGenerator.jar file to
include it 

I use the NetBeans 5.0 platform for developing java applications - It is
very easy to include external jars in the class path with this IDE.  It can
be found at http://www.netbeans.org - the platform also helps with builds
and versioning - makes a nice distributable jar file after building.
Databases can be created in the IDE or by creating a class file to create
the database - I use the method described in the article above.

Hope this helps.  I have only been doing JDBC for about 6 months - the hard
way - using JDBC-ODBC bridge.  A couple of weeks ago, my husband stumbled
onto Derby. I have started using it and have discovered that it is much
better and easier than using ODBC,

Annetta C. Green 
NettaSoft, Senior Developer/Engineer
(505) 927-0072 mobile
victorianladyturtle@gmail.com



Mime
View raw message