Table replication between data sources-JAVA Implementation (LP)

Source: Internet
Author: User

1. Table or result to arraylist: query the content of the tablename table in the Conna data source and put it in an arraylist. Private Static arraylist tabletoarraylist (string tablename, connection Conna) {statement stmt = NULL; arraylist list = new arraylist (); try {stmt = Conna. createstatement (); string SQL = "select * from" + tablename; system. out. println (SQL); resultset result = stmt.exe cutequery (SQL); int columncount = result. getmetadata (). getcolumncount (); // Number of columns. Integer [] columntype = new integer [columncount]; // numeric representation of the Data Type of the column. For (INT I = 0; I <columncount; I ++) {columntype [I] = new INTEGER (result. getmetadata (). getcolumntype (I + 1);} List. add (columntype); While (result. next () {object [] columnvalue = new object [columncount]; // column value. For (INT I = 0; I <columncount; I ++) {columnvalue [I] = result. getObject (I + 1);} List. add (columnvalue) ;}} catch (exception e) {e. printstacktrace ();} finally {try {If (stmt! = NULL) stmt. Close (); If (Conna! = NULL) Conna. close ();} catch (sqlexception e) {e. printstacktrace () ;}} return list;} 2. arraylist to table inserts the content in the List into the tablename table of the connb data source. Private Static void arraylisttotable (arraylist list, connection connb, string tablename) {statement stmt = NULL; preparedstatement pstmt = NULL; try {stmt = connb. createstatement (); // Insert the data in the list to the tablename table. // If data already exists in the tablename table, remove the data. String querysql = "select * from" + tablename; stmt = connb. createstatement (); resultset rs = stmt.exe cutequery (querysql); If (RS. next () {string deletesql = "delete from" + tablename; stmt.exe cute (deletesql);} integer [] columntype = (integer []) list. get (0); object [] columnvalue = NULL; int columncountnew = columntype. length; stringbuffer presql = new stringbuffer (""); For (INT I = 0; I <colu Mncountnew-1; I ++) {presql. append ("?, ");} Presql. append ("? "); String insertsql =" insert into "+ tablename +" values ("+ presql +") "; system. out. println ("pre insertsql is:" + insertsql); pstmt = connb. preparestatement (insertsql); system. out. println ("parametercount:" + pstmt. getparametermetadata (). getparametercount (); // Insert the data by row. for (INT I = 1; I <list. size (); I ++) {columnvalue = (object []) list. get (I); int type, J; For (j = 0; j <Co Lumncountnew; j ++) {type = columntype [J]. intvalue (); int temp = J + 1; if (columnvalue [J] = NULL) {pstmt. setnull (temp, type);} else {Switch (type) {Case Java. SQL. types. bigint: pstmt. setlong (temp, (long) columnvalue [J]); break; Case Java. SQL. types. integer: pstmt. setlong (temp, (long) columnvalue [J]); break; Case Java. SQL. types. float: pstmt. setfloat (temp, (float) columnvalue [J]); break; Case ja Va. SQL. types. double: pstmt. setdouble (temp, (double) columnvalue [J]); break; Case Java. SQL. types. date: pstmt. setdate (temp, (date) columnvalue [J]); break; Case Java. SQL. types. time: pstmt. settime (temp, (time) columnvalue [J]); break; Case Java. SQL. types. timestamp: pstmt. settimestamp (temp, (timestamp) columnvalue [J]); break; default: pstmt. setstring (temp, columnvalue [J]. tostring (); break ;}} else E M. out. println ("the row" + I + ":" + insertsql); pstmt.exe cuteupdate () ;}} catch (sqlexception e) {e. printstacktrace ();} finally {try {If (stmt! = NULL) stmt. Close (); If (connb! = NULL) connb. Close () ;}catch (sqlexception e) {e. printstacktrace () ;}} 3. Copy a table from one data source to another. Private Static void importdata (string tablename, connection Conna, connection connb) {arraylist list = new arraylist (); List = tabletoarraylist (tablename, Conna); arraylisttotable (list, connb, tablename );}

 

Related Article

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.