logo (1K)

Getting started

This section describes an example of the use of jLips that will get you started on using jLips.

The database

This example works with a simple database that has two tables:

  • Person that holds information about a person
  • Address that is related to the Person table and holds information about the addresses of the person (1 to many relationship)
The SQL needed for creating the tables is:
CREATE TABLE person
  (
    id           INTEGER        ,
    name         VARCHAR(40)    ,
    birthday     DATE           ,
    PRIMARY KEY(id)
  );
CREATE TABLE address
  (
    id           INTEGER        ,
    personid     INTEGER        ,
    street       VARCHAR(40)    ,
    PRIMARY KEY(id),
    foreign key(personid) references person(id)
  );


Persist interface

The first thing to do is create a persist interface. This interface must inherit from the PersistEntity interface.
The purpose of the interface is to create methods to access and manipulate the fields in the datebase.
When we want to be able to get a field we create a method called get<fieldname> returning the type of the field and when we want to be able to change a field we create a set<fieldname> method.

import net.toften.jlips.persist.PersistEntity;
import java.sql.Date;
public interface Person extends PersistEntity {
    Integer getId();
    
    String getName();
    void setName(String newName);
    
    void setBirthday(Date day);
    Date getBirthday();
    
    java.util.Collection getAddress();
}
                   

Database properties

The database properties class provides information about how jLips should connect to a database. The properties are the information needed to create a JDBC connection.

public class DBHardProperties extends DBAbstractProperties implements DBProperties {
    public DBHardProperties() {        
        dbProp.put(DBProperties.DB_DRIVER, "com.mckoi.JDBCDriver");
        dbProp.put(DBProperties.DB_URL, "jdbc:mckoi://127.0.0.1/JLIPS");
        dbProp.put(DBProperties.DB_USERNAME, "jlips");
        dbProp.put(DBProperties.DB_PASSWORD, "jlips");
        dbProp.put(DBProperties.DB_SCHEMA, "APP");
    }
}
The class must implement the DBProperties interface. Jlips provide an abstract class DBAbstractProperties, which can assist you in creating the Database Properties class. When extending the abstract class the only thing needed is to store the propeties in the provided dbInitProb attribute.
The keys for the properties are defined in the DBProperties interface.

Register the properties class and the persist interfaces

Before jLips can be used it needs to be initialised. This is done using the static init method in the EntityFactory class. This method take two parameters:

  • A Propeties object specifying the database propeties; and
  • A Map object mapping Persist Interfaces to the database tables
  try {
    EntityFactory.init(dbProp, tableMap);
  } catch (Exception e){
    System.out.println(e.toString());
  }     
The database properties object contains two properties:
  • The name of the Database properties class; and
  • The name of the connection manager class
  Properties dbProp = new Properties();
  dbProp.put(EntityFactory.EF_DB_CON_HAND, "net.toften.jlips.persist.db.DBPooledConnection");
  dbProp.put(EntityFactory.EF_DB_PROP_HAND, "net.toften.jlips.demo.DBHardProperties");
The Map object mapping the database tables can be created like this:
  HashMap tableMap = new HashMap();
  tableMap.put("net.toften.jlips.demo.Person", "person");
  tableMap.put("net.toften.jlips.demo.Address", "address");
In the Map object the name of the Persist Interface is mapped to a database table. The table must conform to the naming rules and must map precisely to the method names in the Persist Interface

Go!

After jLips has been initalised the data in the database can be accessed using the methods in the EntityFactory and in the Persist interfaces.

Creating a record

Using the EntityFactory you can create a new record like this:

  Person p = (Person)EntityFactory.create(Person.class);
This will return an object that represents the record in the database.

Manipulating the fields

The object implements the Person interface we created before, which means we can manipulate the fields in the newly create record like this:

  p.setName("Thomas");


Getting field values

The field values of a record are also accessed using the Person interface:

  String name = p.getName();