This is a Simple DAO Tutorial (Data Access Object) using a vector, or an HSQLDB or MySQL. You can find simple examples for DB processing and vector processing in this Tutorial. I will put the complete code here but if you would like to have it as JAR please contact me at arsaral (at) yahoo.com
drivePersonDAO.java is a Factory Pattern which creates the personDAO object. The personDAO object points at the selected database either vector, HSQLDB or MySQL that lies underneath. After creating the personDAO object it tests the DAO.
drivePersonDAO.java
-------------------
import mainPackage.*;
import daoPackage.*;
/**
*
* @author Ali Riza SARAL
*/
public class drivePersonDAO {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
PersonDAO personDAO = null;
String nameVal = "", lastVal = "", hobbyVal = "", idVal = "";
int DAOtype = 3; //1:Vector 2:HSQLDB 3:MySQL
try {
switch (DAOtype) {
case 1:
personDAO = new PersonDAOVectorImpl();
break;
case 2:
personDAO = new PersonDAOHSQLDBImpl();
break;
case 3:
personDAO = new PersonDAOMySQLDBImpl();
break;
default:
}
} catch (Exception e) {
System.out.println("ARSmsg: personDAO could not be instantiated.");
}
Person person = personDAO.readRec(1);
nameVal = person.getName();
lastVal = person.getLast();
hobbyVal = person.getHobby();
idVal = String.valueOf(person.getId());
System.out.println("-------\nName:" + nameVal);
System.out.println("LastName:" + lastVal);
System.out.println("Hobby:" + hobbyVal);
System.out.println("Id:" + idVal);
personDAO.createRec(6, "Ali", "KIDIK", "gazetecilik");
personDAO.findByPrimaryKey(5);
personDAO.findByLastName("SARAL");
// personDAO.findTotalNumberOfPersons();
// personDAO.updateRec(3, "TURAN", "xxx", "qqq");
// personDAO.readRec(3);
// personDAO.deleteRec(3);
}
}
personDAO is defined abstractly in PersonDAO.java.
PersonDAO.java
--------------
package daoPackage;
import java.util.Collection;
import mainPackage.*;
public interface PersonDAO {
public void createRec(int id, String firstName,
String lastName, String hobby)
throws PersonDAOSysException;
public Person readRec(int personId)
throws PersonDAOSysException;
public void updateRec(int id, String firstName,
String lastName, String hobby)
throws PersonDAOSysException;
public void deleteRec(int personId)
throws PersonDAOSysException;
public boolean findByPrimaryKey(int personId)
throws PersonDAOSysException;
public Collection findByLastName(String lastName)
throws PersonDAOSysException;
public int findTotalNumberOfPersons()
throws PersonDAOSysException;
}
Person.java defines the Person object which is used to hold the data that is recorded in the personDAO.
Person.java
-----------
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package mainPackage;
/**
*
* @author Ali Riza SARAL
*/
public class Person {
private String name;
private String last;
private String hobby;
private int id;
public Person(){}
public Person(int id, String name, String last, String hobby){
this.name = name;
this.last = last;
this.hobby = hobby;
this.id = id;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public String getLast() {
return this.last;
}
public void setLast(String last) {
this.last = last;
}
public String getHobby() {
return this.hobby;
}
public void setHobby(String hobby) {
this.hobby = hobby;
}
public int getId() {
return this.id;
}
public void setId(int id) {
this.id = id;
}
}