In the general development process, it is often necessary to retrieve the data from the database to form a value object (usually called VO), put it in the list set, and return it to the call end. When the program has not been exposed to clone, it is often necessary to construct a Vo instance in the while block. However, this is not recommended and should even be avoided, because it may cause low program efficiency. After using the clone technology, the program running efficiency has been improved. The following is a common method, which is helpful to you.
The supported clone VO looks like this:
Package com. xxx;
Import java. util .*;
Public class tempjob implements cloneable {// declare that it can be cloned
Private string jbnr = NULL;
Private string jbdw = NULL;
Public object clone (){
Try {
Return super. Clone (); // returns the cloned object.
}
Catch (clonenotsupportedexception e ){
System. Out. println (E. tostring ());
}
Public void setjbnr (string jbnr ){
This. jbnr = jbnr;
}
Public String getjbnr (){
Return jbnr;
}
Public void setjbdw (string jbdw ){
This. jbdw = jbdw;
}
Public String getjbdw (){
Return jbdw;
}
}
For classes that call Vo, such:
Package com. xxx;
Import java. SQL .*;
Import java. util .*;
Public class dojob {
Private connection con;
Private statement stmt;
Private resultset RS;
Private arraylist Lis = new arraylist ();
Public arraylist query (string ){
String SQL = "";
SQL = "select a. *, B. name from temp_job A, user_basic B"
+ "Where a. userid = B. userid order by A. wcqk ";
Try {
Stmt = con. createstatement ();
Rs1_stmt.exe cutequery (SQL );
Tempjob job = new tempjob (); // you only need one instance.
While (Rs. Next ()){
Job. setjbnr (Rs. getstring (1 ));
Job. setjbdw (Rs. getstring (2 ));
Lis. Add (job. Clone (); // returns the cloned object
}
}
Catch (sqlexception e ){
System. Out. println (E. tostring ());
}
Finally {
This. rs = NULL;
This. stmt = NULL;
Con. Close ();
}
Return Lis;
}
}
(Please note! This document shall indicate the original author Rosen Jiang and its source:Http://blog.csdn.net/rosen)