Sun's JDO Standard

xiaoxiao2021-03-06  65

Source:

http://blog.9cbs.net/legenDinfo/archive/2004/11/29/197392.aspx

JDO allows us to use Java objects to support transactions and multiple users. Unlike ODBC, it makes us no need to consider SQL and other things related to the database. It is also different from serialization because it supports multiple users and transactions. JDO allows Java developers to use their data model as a data model without having to spend a lot of time in the transfer between "data terminals", "object end". A variety of products, including Cocobase, WebGain Toplink, and Castor JDO, can be implemented. Since there is a standard method, we can only learn one of them, just like an ODBC, we can use any database that provides the driver. In this article, we will use the OpenFusion JDo of Prism Technology. The reader will find that only a small part of the code uses the PrismTech API, and the other parts use the standard JDO standard. Creating a Person object We will first create a Person object that follows the practice of JavaBean, which can perform GET and SET operations on its properties. It should be noted that although we are creating this class, it doesn't matter, it doesn't inherit or implement any basic classes. The requirements for a maintenance class are: 1, all domains must be able to be accessed by JDO (public or set *) 2, the data type of the domain must comply with the JDO specification. 3. You cannot support some types of fields (such as Thread, File, Socket and other fields that cannot be serialized).

Here is Person.java meet these requirements: public class Person {private String name; private String address; private String ssn; private String email; private String homePhone; private String workPhone; // constructor allows us to use to create Person objects public Person (String name, String address, String ssn, String email, String homePhone, String workPhone) {this.name = name; this.address = address; this.ssn = ssn; this.email = email; this.homePhone = homePhone; this.workPhone = workPhone;} // methods public String getName () {return name;} public String getAddress () {return address;} public String getSsn () {return ssn;} public String getEmail () {return email;} public String getHomePhone () {return homePhone;} public String getWorkPhone () {return workPhone;} public void setName (String name) {this.name = name;} public void setAddress (String address) {this.address = address;} Public void setssn (string ssn) {this.ssn = SSN;} public void setemail (string email) {this.email = email;} public void setHomePhone (String Homephon) e) {this.homePhone = homephone;} public void setworkphone (String Workphone) {this.Workphone = Workphone;}} Creating a PersonPersist object management Stapleability Now there is already a Person object, we need to create some code to manage this Retention. Below we will discuss these codes in detail, and learn how to: 1. Initialize the JDO Holder Manager. 2. Enter the information of three people in the database. 3. Display people's information from the database. 4. Modify the name of one of them. 5. Delete a person's information from the database. 6. Related processing in the main () method. Step 1: Initializing JDO Optimacy We import standard JDO classes from the OpenFusion implementation. Of course, we can also abstract them into an independent class. The constructor sets the connection agent using the Javax.jdo.PersistenceManagerFactoryClass property, which is similar to the properties of setting up the database driver in the JDBC.

package addressbook; import java.util *;. import javax.jdo *;. import com.prismt.j2ee.connector.jdbc.ManagedConnectionFactoryImpl; public class PersonPersist {private final static int SIZE = 3; private PersistenceManagerFactory pmf = null; private PersistenceManager PM = null; private transaction transaction = null; // Need to hold the array private person [] people; // existing object identifier of vector private vector id = new vector (size); public personPERSIST () {Try { Properties props = new Properties (); props.setProperty ( "javax.jdo.PersistenceManagerFactoryClass", "com.prismt.j2ee.jdo.PersistenceManagerFactoryImpl"); pmf = JDOHelper.getPersistenceManagerFactory (props); pmf.setConnectionFactory (createConnectionFactory ()) Catch (Exception EX) {EX.PrintStackTrace (); System.exit (1);}} The connection agent is created in the static method of the name CreateConnectionFactory (), which requires JDBC URL, JDBC driver, user Name and password. public static Object createConnectionFactory () {ManagedConnectionFactoryImpl mcfi = new ManagedConnectionFactoryImpl (); Object connectionFactory = null; try {mcfi.setUserName ( "scott"); mcfi.setPassword ( "tiger"); mcfi.setConnectionURL ( "jdbc: oracle: thin : @localhost: 1521: thedb "); mcfi.setDBDriver (" oracle.jdbc.driver.OracleDriver "); connectionFactory = mcfi.createConnectionFactory ();} catch (Exception e) {e.printStackTrace (); System.exit ( 1);} Return ConnectionFactory; . The first thing we want is to get a retention manager through getPersistenceManager () and then create a transaction that performs our task. In order to maintain this object map, we simply call the MakePersistentall (Object []) method. The FOR () loop at the bottom of the code gets the unique ID of each maintained object and saves it later.

Public void personistpeople () {// Create a pople = new person [size]; // Create 3 people Information people people [0] = New Person ("Gary Segal", "123 FooBar Lane", "123 -123-1234, "Gary@segal.com", "(608) 294-0192" (608) 029-4059 "); people [1] = New Person (" Michael Owen ", 222 Bazza Lane , "111-222-3333", "michael@owen.com", "(720) 111-2222" (303) 222-3333 "); People [2] = New Person (" Roy Keane "," 222 Traford Ave, Manchester, Mn "," 234-235-3830 "," Roy@keane.com "," (720) 940-9049 "," (303) 309-7599) / / Keep these three people's information PM = pmf.getPersistenceManager (); transaction = pm.currentTransAction (); pm.makepersistentall (person); transaction.commit (); // Get Object ID for the Object (INT I = 0; I

Public void display (int end) {person Person; int max = end <= size? end: size; // get a new retention manager PM = PMF.GetPersistenceManager (); // Get objects from the database and Display for for (int i = 0; i

If you run this program, you will see the address book that the program runs to each step. public static void main (String [] args) {System.out.println ( "Create PersonPersist"); PersonPersist personPersist = new PersonPersist (); System.out.println ( "Setup and persist a group of people"); personPersist. PersiStPeople (); System.Out.println ("Display the Persisted People"); PersonPersist.display (Size); System.Out.println ("Change A Name"); personPersist.change (); personPersist.display (size) System.out.println ("Delete a Person"); personPersist.delete (); person - 1);} JDOENHANCER: Create JDOENHANCER JDO Descriptor Now, we have written the source of the entire application Code, the next step is to create a JDO descriptor that JDOENHANCER will use. What is reader will ask JDOENHANCER? The JDO architecture is based on the following concept: a JDO implementation can obtain the bytecode of the class, process them, add some necessary functions. For example, JDoenHancer will enable the class to implement the PersistanceCapable interface (so we don't have to program this interface), and some of the methods in this interface can be implemented. Therefore, after compiling the code, we will find that we must run JDOENHANCER to properly handle the bytecode. We need to create a descriptor file that gives the information we need to keep the class, this file is as follows: Now we have written Code and JDO Descriptor files, we will integrate them and discuss how to build the entire system. To establish an entire system, we only need to make a few steps: 1, compile code. 2, run JDOENHANCER. 3, use JDOENCER output creates a database. 4, running the application. Step 1: Compile code I want the vast number reader to know how to run Javaac. Before running Javac, we only guarantee the correct settings of ClassPath. Below is An example of running Javac on a Windows platform:% set openfusion_dir = d: / apps / openfusionjdo% set classpath =% openfusion_dir% / lib / connection_dir% / lib / connection_dir% / lib / connection_dir% / lib / connection_dir;% / p en _ _;;; Log4j.jar;% openfusion_dir% / l ib / xerces.jar;% openfusion_dir% / lib / class12.zip;% openfusion_dir% / lib / jdo.jar;% openfusion_dir% / lib / jta- spec1_0_1.jar;% OpenFusion_DIR% /LIB/ofjdo.jar ;.% javac -d. Person * .java Step 2: Run JDOENHANCER JDOENHANCER You need to use the bytecode obtained in the previous step and our previously established JDO descriptor file. Below is OpenFusion Jdoenhancer Full syntax: java com.prismt.j2ee.j DO.EnHancer.jdoenhancer Command Options: -cp Start Search The basic path of the class that needs to be strengthened, unlike classpath, it is a directory-IC storage-IC storage-IC storage-IC storage-PD JDO descriptor file Option: -db Specifies the target database [Oracle, Sybase, etc.] The directory of the SQL script below is an example of running JDOENHANCER for establishing our application:% java com.prismt.j2ee.jdo.enaster.jdoenhancer - Oc. -pd person.jdo -db oracle -od db -cp. Step 3: Establishing a database using the JDB and -OD options using the JDB and -OD options, JDOENHANCER can create a database script that establishes a database. It can create many scripts, but one of them is load_all.sql, open the file and load it into a SQL prompt.

转载请注明原文地址:https://www.9cbs.com/read-86574.html

New Post(0)