I am also playing reflection (reflect of Java)

Source: Internet
Author: User

If you don't talk about P, directly upload the Code:

 

Package COM. hzmagix. utils; <br/> Import Java. io. file; <br/> Import Java. io. fileinputstream; <br/> Import Java. io. filenotfoundexception; <br/> Import Java. io. ioexception; <br/> Import Java. lang. reflect. constructor; <br/> Import Java. lang. reflect. invocationtargetexception; <br/> Import Java. text. dateformat; <br/> Import Java. text. decimalformat; <br/> Import Java. text. simpledateformat; <br/> Import Java. util. da Te; <br/> Import Org. apache. poi. hssf. usermodel. hssfcell; <br/> Import Org. apache. poi. hssf. usermodel. hssfdateutil; <br/> Import Org. apache. poi. hssf. usermodel. hssfrow; <br/> Import Org. apache. poi. hssf. usermodel. hssfsheet; <br/> Import Org. apache. poi. hssf. usermodel. hssfworkbook; <br/> Import COM. hzmagix. model. flight; <br/> public class excelparsetutils {<br/> Public static simpledateformat format = new simp Ledateformat (<br/> "yyyy-mm-dd hh: mm: SS "); <br/> // todo <br/> Private Static file exlfile = new file ("F: // flight_example.xls"); <br/> Private Static int celllenth = 11; <br/> private static object PARSET (Object clazz, string vars) {<br/> system. out. println (vars); <br/> class clz = clazz. getclass (); <br/> string STRs [] = vars. split (","); <br/> constructor [] = clz. getdeclaredconstructors (); <br/> try {<Br/> for (constructor C: ctor) {<br/> class CX [] = C. getparametertypes (); <br/> If (Cx. length = 0) {<br/> system. out. println (format. format (new date () <br/> + "Debug: No parameter-free constructor can be parsed !!! "); <Br/> continue; <br/>}< br/> If (Cx. length! = STRs. length + 1) {// do not construct the id <br/> system. out. println (format. format (new date () <br/> + "error: parameter input error !!! "); <Br/> continue; <br/>}< br/> Object ARGs [] = new object [Cx. length]; <br/> for (INT I = 1; I <Cx. length; I ++) {// ignore id <br/> If (CX [I]. isinstance (New Long (321) {<br/> ARGs [I] = new long (STRs [I]); <br/>} else if (CX [I]. isinstance (new string ("string test") {<br/> ARGs [I] = new string (STRs [I]); <br/>} else if (CX [I]. isinstance (new date () {<br/> ARGs [I] = new date (); // temporarily not maintained <br/>}< br/> OBJ Ect obj = Class. forname (clazz. getclass (). getname () <br/>. getconstructor (CX ). newinstance (ARGs); <br/> system. out. println (OBJ); <br/> return OBJ; <br/>}< br/>} catch (classnotfoundexception e) {<br/> E. printstacktrace (); <br/>}catch (illegalargumentexception e) {<br/> E. printstacktrace (); <br/>} catch (securityexception e) {<br/> E. printstacktrace (); <br/>} catch (instantiationexception e) {<br/> E. Printstacktrace (); <br/>} catch (illegalaccessexception e) {<br/> E. printstacktrace (); <br/>} catch (invocationtargetexception e) {<br/> E. printstacktrace (); <br/>} catch (nosuchmethodexception e) {<br/> E. printstacktrace (); <br/>}< br/> return NULL; <br/>}< br/> Public static void PARSET (File exlfile, object clazz, int cellength) {<br/> try {<br/> fileinputstream in = new fileinputstream (exlfile. ge Tpath (); <br/> hssfworkbook workbook = new hssfworkbook (in); <br/> hssfsheet sheet = workbook. getsheetat (0); <br/> int rows = sheet. getphysicalnumberofrows (); <br/> decimalformat df = new decimalformat ("#"); <br/> dateformat format = new simpledateformat ("yyyy-mm-dd "); <br/> for (INT I = 1; I <rows; I ++) {// first behavior title <br/> // traffic Wt = new traffic (); <br/> hssfrow ROW = sheet. getrow (I); <br/> If (row! = NULL) {<br/> // start to retrieve columns under the row <br/> stringbuffer sb = new stringbuffer (); <br/> for (short J = 0; j <cellength; j ++) {<br/> hssfcell cell = row. getcell (j); <br/> date = new date (); <br/> string stringvalue = new string (); <br/> If (cell! = NULL) {<br/> // determines the type of data <br/> switch (cell. getcelltype () {<br/> case hssfcell. cell_type_numeric: <br/> stringvalue = DF. format (cell <br/>. getnumericcellvalue (); <br/> If (hssfdateutil. iscelldateformatted (cell) {<br/> double D = cell. getnumericcellvalue (); <br/> date = hssfdateutil. getjavadate (d); <br/>}< br/> break; <br/> case hssfcell. cell_type_string: <br/> stringvalue = cell. getstringcellvalue (); <Br/> break; <br/>}< br/> Sb. append (stringvalue); <br/> Sb. append (","); <br/>}< br/> string S = sb. substring (0, sb. tostring (). length ()-1); <br/> // todo fff <br/> PARSET (clazz, S ); <br/>}< br/> // For (Traffic wt: trafficlist) {<br/> // trafficmanager. save (wt); // save <br/> //} <br/>} catch (filenotfoundexception E) {<br/> // todo auto-generated Catch Block <br/> E. printstacktrace (); <br />} Catch (ioexception e) {<br/> // todo auto-generated Catch Block <br/> E. printstacktrace (); <br/>}< br/> // toexl (); <br/> system. out. println ("end !!! "); <Br/>}< br/> Public static void main (string [] ARGs) {<br/> flight TR = new flight (); <br/> PARSET (exlfile, TR, celllenth); <br/>}< br/>}

 

 

In fact, the function to be implemented is very simple. The Excel form is written into the database in batches, but every time the same code is written based on different beans, it is really inefficient and it is a waste of resources on the computer, in the end, it is a very difficult task for programmers to maintain ($ _ $ nonsense !), Previously, reflection was used. You can generate an instance based on the given class. After obtaining the instance, you can call hibernate saveorupdate to maintain the persistence layer. (The Specific Retention method is suitable for humans .) so I spent an afternoon writing this. I don't know if it's useless. It's easy to use!

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.