Clone an example

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.