Using Java technology to handle data exchange between two different databases

Source: Internet
Author: User
Tags commit

1, establish the 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 the Local database connection

    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, the remote database to save the data 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 ();
    }
    

The two same databases are shown here, and the results are not affected by using a different database, but the structure of both 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.