import org.datanucleus.api.jdo.JDOPersistenceManagerFactory; public class JPOXDemo { public static void main(String[] args) { JDOPersistenceManagerFactory pmf = new JDOPersistenceManagerFactory(); pmf.setProperty("javax.jdo.option.ConnectionURL", "jdbc:mysql://localhost:3306/mydatabase"); pmf.setProperty("javax.jdo.option.ConnectionDriverName", "com.mysql.jdbc.Driver"); pmf.setProperty("javax.jdo.option.ConnectionUserName", "root"); pmf.setProperty("javax.jdo.option.ConnectionPassword", "password"); PersistenceManager pm = pmf.getPersistenceManager(); try { pm.currentTransaction().begin(); MyObject obj = new MyObject(); obj.setName("John"); obj.setAge(25); pm.makePersistent(obj); pm.currentTransaction().commit(); Query query = pm.newQuery(MyObject.class); List<MyObject> results = (List<MyObject>) query.execute(); for (MyObject result : results) { System.out.println(result.getName() + " - " + result.getAge()); } } finally { pm.close(); } } } import javax.jdo.annotations.*; @PersistenceCapable public class MyObject { @PrimaryKey private String name; @Persistent private int age; }


上一篇:
下一篇:
切换中文