Java extracts the Data Object List from the Database Using Reflection similar to hibernate.

Source: Internet
Author: User

I have been writing the framework for the past two days. I think this is very practical and I will share it with you!

The main function is to read the object field based on the returned result set, call the set method, and put the corresponding value into the object.

People who have used hibernate should know that the functions are the same.

Code:

CollectionsHelper .java

Package Org. pan. SQL; import Java. lang. reflect. field; import Java. lang. reflect. method; import Java. SQL. resultset; import Java. util. arraylist; import Java. util. list; import Org. pan. convetor. fieldconvertor; import Org. pan. util. methodsuitl; import Org. pan. util. stringuitl;/*** set assistant to query the list of objects returned by the database * @ author Pan **/public class collectionshelper {private list <Object> List = new arraylist <Object> (); public Collections Helper (Object object, resultset RS) {try {string [] colunms = columnhelper. getcolumn (RS); While (RS. next () {// create a new object and instantiate object Ob = Class. forname (object. getclass (). getname ()). newinstance (); method [] Methods = ob. getclass (). getmethods (); For (INT I = 0; I <colunms. length; I ++) {string value = Rs. getstring (colunms [I]). trim (); string label = colunms [I]; If (methodsuitl. exist (methods, "set" + label) {If (Label! = NULL) {field = OB. getclass (). getdeclaredfield (stringuitl. lowercase (Label); If (field! = NULL) {fieldconvertor. convertor (OB, field, value) ;}}} list. add (OB) ;}} catch (exception e) {system. err. println (e) ;}} public list <Object> getlist () {return list ;}}

In addition to the code above, a class is also required, that is, the type converter. If no type conversion is performed, an error is thrown.

Type converter:

Package Org. pan. convetor; import Java. lang. reflect. field; import Java. SQL. timestamp; import Org. pan. exception. fieldconversionexception;/*** type converter * @ author Pan **/public class fieldconvertor {public static void Convertor (Object object, field, string value) throws fieldconversionexception {// set the field to be accessible. Otherwise, an error is returned. setaccessible (true); string type = field. getType (). getname (); try {If (type. equals ("Java. Lang. string ") {field. set (object, value. tostring ();} else if (type. equals ("Java. lang. integer ") {field. set (object, integer. parseint (value);} else if (type. equals ("int") {field. set (object, integer. parseint (value);} else if (type. equals ("Java. lang. double ") {field. set (object, double. parsedouble (value);} else if (type. equals ("double") {field. set (object, double. parsedouble (value);} else if (ty PE. equals ("Java. lang. float ") {field. set (object, float. parsefloat (value);} else if (type. equals ("float") {field. set (object, float. parsefloat (value);} else if (type. equals ("Java. lang. boolean ") {field. set (object, Boolean. parseboolean (value);} else if (type. equals ("Boolean") {field. set (object, Boolean. parseboolean (value);} else if (type. equals ("Java. lang. long ") {field. set (object, long. parselo Ng (value);} else if (type. equals ("Boolean") {field. set (object, long. parselong (value);} else if (type. equals ("char") | type. equals ("Java. lang. character ") {field. set (object, value. charat (0);} else if (type. equals ("Java. lang. short ") | type. equals ("short") {field. set (object, short. parseshort (value);} else if (type. equals ("Java. SQL. timestamp ") {field. set (object, timestamp. valueof (value);} catch (Exception e) {// throw new fieldconversionexception (// "field type conversion exception! Field: "// + field. getname (); system. Err. println (e );}}}

Note: Please indicate the source for reprinting. May 23, 2013 22:09:12

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.