JDBC remotely reads data from a table in a MySQL database (this database needs to be connected by SSH Tunnel, about 8W), and then inserted into two tables in another database, respectively.

Source: Internet
Author: User
Tags ssh access ssh server

Package Com.eeepay.lzj.db;import Java.sql.connection;import Java.sql.date;import java.sql.drivermanager;import Java.sql.preparedstatement;import Java.sql.resultset;import Java.sql.sqlexception;import com.jcraft.jsch.JSch; Import Com.jcraft.jsch.session;public class Changedb {public static int lport = 33102;//Local port (random fetch) public static Strin G Rhost = "172.***.***.***";//remote MySQL server public static int rport = 3306;//remote MySQL service port public static void Go () {String user = "* * *";//ssh connection user name string password = "******";//ssh connection Password string host = "120.132.***.***";//ssh server int port = * * * * *;//ssh Access Port try {Jsch Jsch = new Jsch (); Session session = Jsch.getsession (user, host, port), Session.setpassword (password); Session.setconfig (" Stricthostkeychecking "," no "); Session.connect (); System.out.println (Session.getserverversion ());//Here Print SSH server version information int assinged_port = SESSION.SETPORTFORWARDINGL ( Lport, Rhost, Rport); System.out.println ("localhost:" + assinged_port + "+" + rhost + ":" + Rport);} catch (ExCeption e) {e.printstacktrace ();}} public static void Main (string[] args) {string Mobileno;//mobile_username string Accountname;//account_name string A Ccountno;//account_no string Cnaps;//cnaps_no string bankname;//bank_name string Realname;//lawyer int sta Tus;//open_status string Password;//mobile_password string idcard;//id_card_no int Realnameauth;//real_flag Da        Te Createtime;go (); try {///1, Load Driver Class.forName ("Com.mysql.jdbc.Driver");        } catch (ClassNotFoundException e) {//TODO auto-generated catch block E.printstacktrace ();        }//2, create connection Connection conn = null;         Connection conn2 = null;        try {conn = drivermanager.getconnection ("Jdbc:mysql://localhost:33102/posp", "* * * *", "* * *");        CONN2 = Drivermanager.getconnection ("Jdbc:mysql://115.29.***.***:3306/bag", "* * * *", "* * * * *");         } catch (SQLException e) {System.out.println ("Database Not Connected");   E.printstacktrace ();        } PreparedStatement pstmt = null;        PreparedStatement pstmt2 = null;        PreparedStatement pstmt22 = null;        PreparedStatement pstmt3 = null; try {String sql = "Select Mobile_username,account_name,account_no,cnaps_no,bank_name,lawyer,open_status,mobile        _password,id_card_no,real_flag,create_time from Pos_merchant ";            String sql = "SELECT * from Settle_account where id=1"; String sql2 = "INSERT INTO Bag_login (mobile_no,status,real_name,create_time,password,pay_password,idcard,real_name_            Auth) VALUES (?,?,?,?,?,?,?,?) ";            String sql22 = "SELECT * from Bag_login where mobile_no=?"; String sql3 = "INSERT into Settle_account (mobile_no,account_name,account_no,cnaps,bank_name,create_time) VALUES (?,?,            ?,?,?,?)";            pstmt = conn.preparestatement (sql);            PSTMT2 = Conn2.preparestatement (SQL2);            pstmt22 = Conn2.preparestatement (sql22); PSTMT3 = Conn2.preparesTatement (SQL3);            ResultSet rs = Pstmt.executequery ();            int i=1;            while (Rs.next ()) {System.out.println (i++);            Createtime = Rs.getdate ("Create_time");            Mobileno = rs.getstring ("Mobile_username");            AccountName = rs.getstring ("account_name");            Accountno = rs.getstring ("Account_no");            Cnaps = rs.getstring ("Cnaps_no");            Bankname = rs.getstring ("Bank_name");            Realname = rs.getstring ("lawyer");            Status = Rs.getint ("Open_status");            Password = rs.getstring ("Mobile_password");            Idcard = rs.getstring ("Id_card_no");            Realnameauth = Rs.getint ("Real_flag");            Pstmt3.setstring (1, Mobileno);            Pstmt3.setstring (2, AccountName);            Pstmt3.setstring (3, Accountno);            Pstmt3.setstring (4, cnaps);            Pstmt3.setstring (5, bankname);            Pstmt3.setdate (6, createtime);                   Pstmt3.execute ();     Pstmt22.setstring (1, Mobileno);                if (!pstmt22.execute ()) {pstmt2.setstring (1, Mobileno);                Pstmt2.setint (2, status);                Pstmt2.setstring (3, realname);                Pstmt2.setdate (4, createtime);                Pstmt2.setstring (5, password);                Pstmt2.setstring (6, password);                Pstmt2.setstring (7, Idcard);                Pstmt2.setint (8, Realnameauth);            Pstmt2.execute ();            }}} catch (SQLException e) {//TODO auto-generated catch block        E.printstacktrace ();                    } finally{try {pstmt.close ();                    Conn.close ();                    Pstmt2.close ();                    Conn2.close ();                                     Pstmt3.close ();           } catch (SQLException e) {//TODO auto-generated catch block E.printstacktrace ();     }        }}} 

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.