This is a complete class Main with the methods to store, read and delete Person objects as described before:
package com.intellibo.sample.firststeps;
import com.intellibo.sample.firststeps.Person;
import java.util.*;
import javax.jdo.JDOException;
import javax.jdo.PersistenceManager;
import javax.jdo.PersistenceManagerFactory;
import javax.jdo.Transaction;
import javax.jdo.Query;
import com.signsoft.ibo.client.PersistenceManagerFactoryImpl;
public class Main
{
private PersistenceManagerFactoryImpl pmf = null;
static public void main(String[] args)
{
Main main = new Main();
main.run();
System.exit(0);
}
/**
* inits PersistenceManagerFactoryImpl
* and calls sample methods
*/
public void run()
{
try
{
pmf = new PersistenceManagerFactoryImpl();
this.sampleInsert();
this.sampleQuery();
this.sampleDelete();
}
catch (JDOException e)
{
e.printStackTrace();
System.exit(1);
}
finally
{
pmf.close();
}
}
/**
* insert data
*/
public void sampleInsert()
{
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
tx.begin();
Person p = new Person();
p.setLastName("Edwards");
p.setFirstName("James");
System.out.println("creating " + p);
pm.makePersistent(p);
p = new Person();
p.setLastName("Smith");
p.setFirstName("Jane");
System.out.println("creating " + p);
pm.makePersistent(p);
tx.commit();
pm.close();
}
/**
* read data
*/
public void sampleQuery()
{
PersistenceManager pm = pmf.getPersistenceManager();
Query q = pm.newQuery(Person.class);
Collection c = (Collection) q.execute();
Iterator it = c.iterator();
while (it.hasNext()){
Person p = (Person) it.next();
System.out.println("found " + p.getFirstName()+ " " +p.getLastName());
}
q.close(c);
pm.close();
}
/**
* delete data
*/
public void sampleDelete()
{
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
tx.begin();
Query q = pm.newQuery(Person.class);
Collection c = (Collection) q.execute();
Iterator it = c.iterator();
while (it.hasNext()){
pm.deletePersistent(it.next());
}
System.out.println("all persons deleted ");
q.close(c);
tx.commit();
pm.close();
}
}