Use Java technology to exchange data between two different databases

Source: Internet
Author: User


1. Establish a remote database connection:

public static synchronized Connection getConFromRemote() {
        Connection con = null;
        String url = "jdbc:sqlserver://admin.xxx.xxx.comdbo:1436;databaseName=remote_jadepool";
        String userName = "hkm12345";
        String password = "pwd12345";
        try {
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            con = DriverManager.getConnection(url, userName, password);
        } catch (SQLException ex1) {
            ex1.printStackTrace();
        } catch (ClassNotFoundException ex) {
            ex.printStackTrace();
        }
        return con;
    }

2. Establish a connection to the local database

 public static synchronized Connection getConFromLocal() {
        Connection con = null;
        String url = "jdbc:sqlserver://127.0.0.1dbo:1436;databaseName=jadepool";
        String userName = "hkm123";
        String password = "pwd123";
        try {
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            con = DriverManager.getConnection(url, userName, password);
        } catch (SQLException ex1) {
            ex1.printStackTrace();
        } catch (ClassNotFoundException ex) {
            ex.printStackTrace();
        }
        return con;
    }

3. Save data from the remote database to the local database.

 public void backupDB() {
        Jade j0 = new Jade(getConFromRemote(), cn.jadepool.sql.DbConnectionType.USING_DB_01);
        List<Map> v0 = j0.query("select * from jade_book");
        List<Map> v1 = j0.query("select * from jade_book_cs");
        j0.commit();
        
        Jade j1 = new Jade(getConFromLocal(), cn.jadepool.sql.DbConnectionType.USING_DB_02);
        j1.delete("delete from jade_book");
        j1.insert("jade_book", v0);
        j1.delete("delete from jade_book_cs");
        j1.insert("jade_book_cs", v1);
        j1.commit();
    }

Here we demonstrate two identical databases. Using different databases does not affect the results, but the structure of the two tables must be consistent.


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.