This article implements the data in the old library to move to the new library
1. Get the JDBC Connection
Package com.transferdata;import java.sql.drivermanager;import java.sql.sqlexception;import com.mysql.jdbc.Connection;public class ConnectionUtils {public Connection Getoldconn () { String driver = "Com.mysql.jdbc.Driver"; String url = "jdbc:mysql://localhost:3306/old?useunicode=true&characterencoding =utf-8 "; string username = " root "; string password = "123456"; connection conn = null; try { class.forname (driver); //classLoader, Load corresponding drive conn = (Connection) Drivermanager.getconnection (Url, username, password); } catch ( Classnotfoundexception e)  {  &NBsp; e.printstacktrace (); } catch (SQLException e) { e.printstacktrace (); } return conn;} Public connection getnewconn () { String driver = " Com.mysql.jdbc.Driver "; string url = " Jdbc:mysql://localhost:3306/new? Useunicode=true&characterencoding=utf-8 "; string username = " root "; String password = "123456"; connection conn = null; try { Class.forName (driver); //classloader, load the corresponding driver conn = (Connection) drivermanager.getconnection (Url, username, password); } catch (classnotfoundexception e) { e.printstacktrace (); } catch (sqlexception e) { e.printstacktrace (); } Return conn;} Public void closeconnection (Connection conn) { // determine if Conn is an empty if (conn != null) { try { Conn.close (); // Close database connection } catch (sqlexception e) { &Nbsp; e.printstacktrace (); } } }}
2. Get Old data
package com.transferdata;import java.sql.resultset;import java.sql.sqlexception;import java.util.arraylist;import java.util.list;import com.mysql.jdbc.connection;import com.mysql.jdbc.preparedstatement;import com.entity.customer;public class getolddata { Private connection oldconn;public getolddata (Connection oldconn) {this.oldConn = Oldconn;} Public list<customer> getcustomerlist () { list<customer > customerList = new ArrayList<Customer> (); string sql = "Select * from customer"; preparedstatement pstmt; try { pstmt = ( PreparedStatement) oldconn.preparestatement (SQL); resultset rs = pstmt.executequery (); while (Rs.next ()) { customer customer = new customer (); customer.setid (rs.getstring ("id")); customer.setemail (rs.getString ("email")); customer.setidcardno (rs.getString ("Idcard_no ")); customer.setloginname (rs.getString ("login_name")); customer.setmobileno (Rs.getString (" Mobile_no ")); customer.setpassword ( Rs.getstring ("password")); Customer.setrealname (RS.getstring ("Real_name")); Customer.setrecomermono (rs.getstring ("Recomer_mo_no")); customerlist.add (Customer); } } catch (sqlexception e) { E.printstacktrace (); } return customerlist;}}
3, Entity class
Package com.entity;public class customer {private string id;private string loginname;private string mobileno;private string recomermono;private string realName;private String email;private String idcardNo;private String Password;public string getid () {return id;} Public void setid (String id) {this.id = id;} Public string getloginname () {return loginname;} Public void setloginname (String loginname) {this.loginname = loginname;} Public string getmobileno () {return mobileno;} Public void setmobileno (String mobileno) {this.mobileno = mobileno;} Public string getrecomermono () {return recomermono;} Public void setrecomermono (String recomermono) {this.recomermono = recomermono;} Public string getRealname () {return realname;} Public void setrealname (String realname) {this.realname = realname;} Public string getemail () {return email;} Public void setemail (String email) {this.email = email;} Public string getidcardno () {return idcardno;} Public void setidcardno (String idcardno) {this.idcardno = idcardno;} Public string getpassword () {return password;} Public void setpassword (String password) {this.password = password;}}
4. Insert a new library
With throws Exception, instead of Try/catch, the aim is to have the exception all
package com.transferdata;import java.sql.resultset;import java.sql.sqlexception;import Java.sql.statement;import java.util.date;import java.util.list;import com.mysql.jdbc.connection ;import com.mysql.jdbc.preparedstatement;import com.entity.customer;public class Insertnewschame {private connection newconn;public insertnewschame (Connection newConn ) {This.newconn = newconn;} /* * with throws exception instead of Try/catch, with the intention of throwing the exception all over the outer layer */public boolean insertcustomer (Customer customer, string id) throws exception{// account_id\mobile_app_info\ zhima_score\device_token string sqlinsert = "INSERT INTO adm_ Sys_customer (email,idcard_no,login_name,mobile_no,password,real_name) " + "values (?,?,?,?,?,?)"; preparedstatement pstmt; pstmt = (PreparedStatement) Newconn.preparestatement (Sqlinsert); pstmt.setstring (1, Customer.getemail ()); pstmt.setstring (2, Customer.getidcardno ()); pstmt.setstring (3, Customer.getloginname ()); pstmt.setstring (4, Customer.getmobileno ()); pstmt.setstring (5, Customer.getpassword ()); pstmt.setstring (6, Customer.getrealname ()); pstmt.executeupdate (); pstmt.close (); return true; }}
5. Testing
Perform a rollback operation after the outer layer catches an exception
package com.zkbc.transferdata;import java.sql.sqlexception;import java.util.list;import Com.mysql.jdbc.connection;import com. Entity. customer;public class testtest { /* * Rollback operation after outer catch exception */public static void main (string args[]) {ConnectionUtils Connectionutils = new connectionutils (); Connection oldconn = connectionutils.getoldconn (); Connection newconn = connectionutils.getnewconn (); Getolddata olddata = new getolddata (oldconn); insertnewschame newdata = new insertnewschame (Newconn); Try{oldconn.setautocommit (false); Newconn.setautocommit (false); string cuid = "0"; List<customer> customerlist = olddata.getcustomerlist (); for (Customer customer: customerlist) {cuId = (Integer.parseint (cuId) + 1) + "";//customerTable and Credit Table Newdata.insertcustomer (CUSTOMER,&NBSP;CUID);} Oldconn.commit (); Newconn.commit ();} catch (Exception e) {e.printstacktrace (); Try {oldconn.rollback (); Newconn.rollback ();} catch (SQLEXCEPTION&NBSP;E1) {// TODO Auto-generated catch Blocke1.printstacktrace ();}} Finally {try {oldconn.setautocommit (True); Newconn.setautocommit (true);} catch (sqlexception e) {// TODO Auto-generated catch Blocke.printstacktrace ();} Connectionutils.closeconnection (Oldconn); Connectionutils.closeconnection (Newconn);}}}
JDBC Implements things management