Introduction
Class Diagram
JAVADOC
Download
ComtorDAO page at sf.net
|
INTRODUCTION
This is most easy to use DAO.
It's uses reflexion to map objects on
tables.
This library implements insert, delete, update and find
actions. You do not require xml configuration files, you don't need to
write any insert , delete or update handly.
SAMPLE STEP BY STEP
STEP 0
Create table in your database.
create table person (
id varchar(20) primary key;
age int;
name varchar(100);
);
STEP 1
Create a class to be mapped on table.
Person.java
public class Person{
private String id ;
private int age;
private String name;
}
STEP 2
Converts the class in a java bean. You can use your IDE to do it:
public class Person{
private String id ;
private int age;
private String name;
/**
* @return the id
*/
public String getId() {
return id;
}
/**
* @param id the id to
set
*/
public void setId(String id)
{
this.id = id;
}
/**
* @return the age
*/
public int getAge() {
return age;
}
/**
* @param age the age
to set
*/
public void setAge(int age) {
this.age = age;
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name
to set
*/
public void setName(String
name) {
this.name = name;
}
}
STEP 3
Insert element in Database
Main.java
import net.comtor.dao.*;
public class Main{
public static void main(String args[]){
Person p = new
Person();
p.setId("787878");
p.setAge(33);
p.setName("Peter
Parker");
ComtorDao dao = new
ComtorJDBCDao("com.mysql.jdbc.Driver", // Driver
"jdbc:mysql://127.0.0.1/database" , // URL
"peter", // user
"spider" //password);
// You needs a ComtorDaoDescriptor to map class
and table
ComtorDaoDescriptor
desc = new GenericJDBCDaoDescriptor("person", Person.class);
desc.getField("id").setFindable(true);
dao.insertElement(p, desc);
}
}
STEP 4
Find Element
import net.comtor.dao.*;
public class Main{
public static void main(String args[]){
Person p = new
Person();
ComtorDao dao = new
ComtorJDBCDao("com.mysql.jdbc.Driver", // Driver
"jdbc:mysql://127.0.0.1/database" , // URL
"peter", // user
"spider" //password);
// You needs a ComtorDaoDescriptor to map class
and table
ComtorDaoDescriptor
desc = new GenericJDBCDaoDescriptor("person", Person.class);
desc.getField("id").setFindable(true);
ComtorDaoKey key
= new ComtorDaoKey("id",new String(" 787878 "));
Object
obj = dao.findElement(key ,
this.getDaoDescriptor());
p = (Person) obj;
}
}
ADVANCED
You can create objects that extends ComtorDaoElementAutoDescriptor
to insert, update, delete elements easiest than
Person p ;
//.....
p.insertInDAO();
|
|