Hello Derby

2 simple console program.


Introduction

2 simple console programs to show usage of Derby database.

The first database is on disk, the database is in Read-Write mode. The second database is in the exe file, the database is in Readonly mode.

Before the demo steps, make a directory "X:\demos\database", and create a derby database "firstdb".


1. Edit a java source file: "X:\demos\src\hello\derby\DemoDerby1.java"

package hello.derby;
 
import java.sql.*;
 
/**
 * In this demo, the Derby database file is on disk.
 *
 * The database file is in the relative directory "./database" of exe file.
 *
 * @author sswater
 */
public class DemoDerby1 {
 
    public static void main(String[] args) throws Exception {
 
        //
        // Get the exe file directory
        // This property does not exist unless this java program runs as an exe file
        //
        String application_home = System.getProperty("application.home");
        
        // If not set, this program is run directly by java.exe, for test only
        //if(application_home == null) { application_home = "d:/test/"; }
        
        //
        // To set derby directory, relative to exe file. The files is on disk
        //
        System.setProperty("derby.system.home", application_home + "/database");
        
        //
        // load database file at derby.system.home
        //
        Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
        Connection conn = DriverManager.getConnection("jdbc:derby:firstdb");
        
        //
        // read data
        //
        Statement stmt = conn.createStatement();
        ResultSet rs = stmt.executeQuery("SELECT * FROM firsttable");
        while(rs.next()) {
            System.out.println(rs.getString(1) + "\t| " + rs.getString(2));
        }
        conn.close();
        
    }
 
}

2. Enter "X:\demos" directory and compile it to a class file:

X:\>cd demos
X:\demos>javac src/hello/derby/DemoDerby1.java

3. Make a jar file:

X:\demos>jar -cvf demoderby1.jar -C src hello/derby/DemoDerby1.class

4. Use Jar2Exe to generate exe file for Windows and Linux:

X:\demos>j2ewiz demoderby1.jar /m hello.derby.DemoDerby1
X:\demos>j2ewiz demoderby1.jar /m hello.derby.DemoDerby1 /platform linux

5. Edit a configuration file to set classpath: "X:\demos\demoderby1.cfg"

classpath lib/derby.jar

Now we can execute the "demoderby1.exe".

6. Download the program for reference.


1. Edit a java source file: "X:\demos\src\hello\derby\DemoDerby3.java"

package hello.derby;

import java.sql.*;

/**
 * In this demo, the Derby database file is internal in the exe file. 
 * 
 * We can select HIDE or ENCRYPT when we use Jar2Exe, 
 * and we can also add 'derby.jar' into the exe file too.
 * 
 * @author sswater
 */
public class DemoDerby3 {

    public static void main(String[] args) throws Exception {
        
        //
        // The database file is in exe file
        //
        Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
        Connection conn = DriverManager.
                    getConnection("jdbc:derby:classpath:database/firstdb");
        
        //
        // read data
        //
        Statement stmt = conn.createStatement();
        ResultSet rs = stmt.executeQuery("SELECT * FROM firsttable");
        while(rs.next()) {
            System.out.println(rs.getString(1) + "\t| " + rs.getString(2));
        }
        conn.close();

    }

}

2. Enter "X:\demos" directory and compile it to a class file:

X:\>cd demos
X:\demos>javac src/hello/derby/DemoDerby3.java

3. Make a jar file:

X:\demos>jar -cvf demoderby3.jar -C src hello/derby/DemoDerby3.class

4. Pack database to be a jar file:

X:\demos>jar -cvf database.jar database

5. Use Jar2Exe to generate exe file for Windows and Linux:

We can adding 'derby.jar' and 'database.jar' together like this:

X:\demos>j2ewiz demoderby3.jar /m hello.derby.DemoDerby3 /embed database.jar /embed lib/derby.jar
X:\demos>j2ewiz demoderby3.jar /m hello.derby.DemoDerby3 /embed database.jar /embed lib/derby.jar /platform linux

Now we can execute 'demoderby3.exe' without database directory and derby.jar.

6. Download the program for reference.


See Also

  • See Depended Jars page for more details about packing depended jar files.