PriDE Quickstart


Up and working in 15 minutes

This short tutorial allows a quick introduction into the general working principles of PriDE, based on a simple example. It takes less than 15 minutes to set up a primitive PriDE application which allows to perform basic operations on a simple database table. The directory examples/quickstart contains the complete source code für the tutorial example.
Setting up an application includes the following steps:

I.e. there's only 3 minutes time for each step now, so let's hurry up ;-)


Preparing the environment

Working with PriDE requires to add the library pride.jar into the CLASSPATH of the working environment, as well as the JDBC driver of the database to access. E.g. in case of a MySQL 4 database this is the library mysql-connector-java-2.0.14-bin.jar, for Oracle 9 the file ojdbc14.jar.

Database configuration

In the next step, the required database tables must be created. PriDE does not provide its own tool for that but assumes one being included in the database installation. If nothing appropriate is available, the open source tool iSQL can be used which is suitable for configuration of all common databases. The tutorial examples uses a database table according to the following definition:
 
create table customer (
  id integer not null primary key,
  name varchar(20),
  first_name varchar(30)
);


Writing or generating entity classes

Accessing a table via PriDE requires a corresponding entity class (usually a simple Java Bean) and a mapping descriptor object. 1:1 mappings of a database table to a Java class can be generated with a code generator provided with PriDE. For the table CUSTOMER above, the source code for an entity class Customer.java and an incorporated descriptor can be generated by the following call:
 
java de.mathema.pride.util.CreateTableTemplate <driver class> <database url> <user> <password>  customer Customer > Customer.java

The generated class must usually be cleaned up a little by hand. For the tutorial example under examples/quickstart the settled entity class Customer.java looks like this:
 
package quickstart;

import de.mathema.pride.*;

public class Customer extends MappedObject {
    private int id;
    private String name;
    private String firstname;

    public int getId() { return id; }
    public String getFirstname() { return firstname; }
    public String getName() { return name; }

    public void setId(int val) { id = val; }
    public void setFirstname(String val) { firstname = val; }
    public void setName(String val) { name = val; }

    public Customer(int id, String name, String firstname) {
        this.id = id;
        this.name = name;
        this.firstname = firstname;
    }

    public static RecordDescriptor red = new RecordDescriptor
        (Customer.class, "customer", null, new String[][] {
            { "id", "getId", "setId" },
            { "first_name", "getFirstname", "setFirstname" },
            { "name", "getName", "setName" },
        });
    public RecordDescriptor getDescriptor() { return red; }

}


Writing application classes

Based on the entity classes, the actual application can be designed. First of all the PriDE runtime library must be initialized as demonstrated for J2SE applications by the following source code.
 
Properties props = System.getProperties();
ResourceAccessorJ2SE accessor = new ResourceAccessorJ2SE(props);
DatabaseFactory.setResourceAccessor(accessor);
DatabaseFactory.setDatabaseName(props.getProperty("pride.db"));


The actual database operations are performed by invoking corresponding member functions of the entity classes, e.g.
 
private void create(int id, String name, String firstName)
    throws SQLException {
    Customer c = new Customer(id, name, firstName);
    c.create();
}

private void update(int id, String name, String firstName)
    throws SQLException {
    Customer c = new Customer(id, name, firstName);
    c.update();
}

private void queryByName( String name )
    throws SQLException {
    Customer c = new Customer(0, name, null);
    ResultIterator ri = c.query(new String[] { "name" });
    do {
        System.out.println(c.getId() + ": " + c.getName() + "," + c.getFirstname());
    } while(ri.next());
}


The tutorial example under examples/quickstart includes the file CustomerClient.java, providing an interactive test client.

Calling the application

The PriDE initialization above is based on system properties which must be passed in the application invokation. E.g. calling the interactive test client for a local MySQL 4 test database might look like this:
 
java -Dpride.driver=com.mysql.jdbc.Driver -Dpride.db=jdbc:mysql://localhost/test quickstart.CustomerClient


That's it!
The tutorial example already illustrates the most important basic principles of PriDE. To understand what's going on behind the scenes of the 5 steps above, it is recommended to carry on with the detailed introduction chapter.


Home Introduction Javadoc