|
|||||||||||||||||
|
Getting startedThis 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:
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.
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 propertiesThe 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 interfacesBefore 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:
try { EntityFactory.init(dbProp, tableMap); } catch (Exception e){ System.out.println(e.toString()); }The database properties object contains two properties:
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 recordUsing 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 fieldsThe 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 valuesThe field values of a record are also accessed using the Person interface: String name = p.getName(); |
$Date: 2004/11/17 23:50:52 $ |