1. Operating environment: JDK, Oracle9i or above, Castor package. It is recommended to use the JBuilde tool to develop 2. Usage Environment: Use ORM (Relational Object Mapping) to operate database data for development in Application and Web mode.
3. Function: Provide queries, update, delete, insert (did not implement automatic generation of primary keys), ("Pipe is temporarily not implemented). 4. Use the steps: First create a CAT table in the database, the field is CAT_ID; Name; SEX; Weight; Type 1, 4 is digital, 2, 3 is a string type.
Secondly establish two profiles (defined by yourself): repository.xml configuration information: XML Version = "1.0" Encoding = "UTF-8"?>
CAT table> cat_id title> fielddsc> name title> fielddesc> sex title> FieldDesc> Weight title> fileDesc> TableDesc> repository> configuration file design description is as follows: TableDesc means a table statement of. Execid is the best and the table name in general (easy to find the correspondence), but it is not allowed to appear. Note that this class name is not a full path. The primary key in the DentifyField table, if the composite primary key is separated by a comma table name sex title> fieldDesc> Here Is a description of the fields in the table, name is the field name. FieldType is the type of database field corresponding to Java, only 4 types of fill: java.lang.string, java.lang.long, java.util.date, java.lang.doubleTitle is not. If there is multiple tables Then add the table description between TableDesc> and repository>.
Pool-config.xml configuration information: XML Version = "1.0" encoding = "GB2312"?> 10 maxactive> 5 maxidle> 10000 maxwait> Test username> test password> Oracle.jdbc.driver.OracleDriver driverclassname> JDBC: Oracle: Thin: @ 192.168.0.90: 1521: Forecast url > True removeabandoned> 60 limitedabandONEDTimeout> True logabandoned> resourceParams> Save the above information as an XML file. Not much explanation for this file.
Determine that two files already exist, then create a table CAT. There is also a table corresponding to the table. Below is the VO I created in an example of Hibernate. (Borrow) Package com.cea.boat.dao; import java.util. *; Public class cat information cloneable {// declare field private java.lang.string catid; private java.lang.string name; private java.lang. String sex; private java.lang.long weight; // Get method public java.lang.string getcatid () {return.string getName () {return.String GetSex () {Return SEX;} public java.lang.long getWeight () {return weight;} // set method public void setcatid (java.lang.string catid) {this.catid = catid;} public void setname (Java. Lang.string name) {this.name = name;} public void setsex (java.lang.string sex) {this.sex = sex;} public void setWeight (java.lang.long weight) {this.Long Weight } Public object clone () {Object o = null; try {= super.clone ();} catch (clonenotsupportedexception ex) {system.out.println (o); } Return O;} public boolean equals (object obj) {cat o = (cat) obj; boolean results = true && (catid == o.catid || catid.equals (o.catid)) && (name == O .name || Name.Equals (.name)) && (Sex == O.SEX || SEX.EQUALS (O.SEX)) && (Weight == O.Weight || Weight.Equals (O.WEight) ); Return Result;}} The thing that has been completed is completed, and below is an example of use.
Declare a connection manager, manage database resources, the start of transaction, its life cycle is ended in an end of a transaction, of course, you can also continue to use this instance when this transaction is finished, to create the beginning of the next transaction . Not recommended so use. ConnectionManage DataManage = New ConnectionManage (); Activation To use classes (there must be CAT in the configuration) .boat.dao.cat cat ");
Here only needs to give SQL query conditions, such as SQL is Select * from cat cat where cat.catid = '1' then SELECT * from cat cat where here is already finished, you need to do it, if not The condition is empty. Query query = connectionManage.createQuery ("cat. Catid = '1'");
Query.hasNext () The role is equivalent to see if there is still a record, query.next () returns an object array here if Activity is multiple objects, then the Objects will sequentially store the corresponding object instance, It is possible to force transformation when using it. While (query.hasnext ()) {Object [] Objects = query.next (); Cat Cat = (CAT) Objects [0];} Used, release the database resource connectManage.close ();
l New way: Since there is no primary key automatically generated, the primary key can only be maintained by yourself, to ensure that the CATID is 111 does not exist in the table, otherwise it will appear. Void testinsert () throws exception {cat (); Cat. Setcatid ("111"); CAT. Setsex ("f"); Cat. SetName ("CATM"); connectionManage cm = new connectionManage (); CM.CREATE (CAT); cm.commit (); cm.close ();
l Update: There is a cat that has a Catid 111 in the form, otherwise it will not be updated to update the message void testUpdate () throws exception {cat cat = new cat (); Cat. setcatid ("111"); CAT Setsex ("f"); Cat. SetName ("CATMM"); connectionManage cm = new connectionManage (); cm.Update (cat); cm.commit (); cm.close ();
l Delete mode: Delete mode is relatively simple, as long as the object is given, and the key value is not empty, you can delete the table record. Void Testdelete () throws exception {cat Cat = new cat (); Cat. Setcatid ("111"); connectionManage cm = new connectionManage (); cm.delete (cat); cm.commit (); cm.close () }