一台MySql服务器不同数据库之间数据同步_解决方案(Java)

原创!

前提:目前MySql数据库中有两个数据库,名称分别为:db01和db02。db01中的人员表叫t_user_info;db02中的人员表叫t_user。需求:将表t_user_info(id,name,type,content,createtime)的数据导入到表t_use( userid,username,usertype, usercontent,user createtime)中。

第一步:运用JDBC建立数据源

 /**

  * JDBC连接数据库(db01) bgy

  * @return Connection

  */

     public static Connection getConnection01() throws               SQLException,java.lang.ClassNotFoundException

    {

       //第一步:加载MySQL的JDBC的驱动

       Class.forName("com.mysql.jdbc.Driver");

       //取得连接的url,能访问MySQL数据库的用户名,密码;db01:数据库名

       String url = "jdbc:mysql://192.168.88.1:3306/db01";       

       String username = "bgy01";

       String password = "123456";

       //第二步:创建与MySQL数据库的连接类的实例

       Connection con01 = (Connection) DriverManager.getConnection(url, username, password);       

       return con01;       

    }

   

    /**

     * JDBC连接数据库(db02) bgy

     * @return Connection

     */

    public static Connection getConnection02() throws SQLException,java.lang.ClassNotFoundException

    {

       //第一步:加载MySQL的JDBC的驱动

       Class.forName("com.mysql.jdbc.Driver");

       //取得连接的url,能访问MySQL数据库的用户名,密码;db02:数据库名

       String url = "jdbc:mysql://192.168.88.2:3306/db02";       

       String username = "bgy02";

       String password = "123456";

       //第二步:创建与MySQL数据库的连接类的实例

       Connection con02 = (Connection) DriverManager.getConnection(url, username, password);       

       return con02;       

    }

 

第二步:同步数据实现详细(方法dSynchronous())

/**

     * 同步数据 bgy

     * @return String

     * @throws ClassNotFoundException

     * @throws SQLException

     */

    public String dSynchronous() throws SQLException, ClassNotFoundException

    {

       //前台传来值

       String param = request.getParameter("param");

       try

       {

           //设置“db01”数据源

           Connection con01 = getConnection01();           

           Statement sql_statement01 = (Statement) con01.createStatement();

          

           //设置“db02”数据源

           Connection con02 = getConnection02();           

           Statement sql_statement02 = (Statement) con02.createStatement();

          

           //人员信息1

           if("1".equals(param))

           {

              //定位至db01

              sql_statement01.executeUpdate("use db01");

              //定位至db02

              sql_statement02.executeUpdate("use db02");

             

              //删除“db02”用户表t_user的数据

              String query1 = "TRUNCATE TABLE t_user;"; //TRUNCATE比DELETE效率高太多

              sql_statement02.executeUpdate(query1);

              String query2 = "ALTER TABLE t_user AUTO_INCREMENT=1;"; //自增序列设为1

              sql_statement02.executeUpdate(query2);

             

              //向“db02”用户表t_user插入数据

              int tag = sql_statement02.executeUpdate("INSERT INTO db02.t_user (id, name,type,content,createtime) SELECT userid, username, usertype, usercontent, usercreatetime FROM db01.t_user_info");

                 String jsondata = null;

              if(tag > 0)

              {

                  jsondata = "{'flag':'ok'}";

              }

              else

              {

                  jsondata = "{'flag':'error'}";

              }

           }

           //“其余”

           if("2".equals(param))

           {

              //业务逻辑代码

           }

           //“其余”

           else

           {

              //业务逻辑代码

           }

           //关闭数据源

           sql_statement01.close();

           conWly.close();

           sql_statement02.close();

           conYxl.close();

       }

       catch(java.lang.ClassNotFoundException e)

       {

            //加载JDBC错误,所要用的驱动没有找到

            System.err.print("ClassNotFoundException");

            //其他错误

            System.err.println(e.getMessage());

       }

       catch(SQLException ex)

       {

           //显示数据库连接错误或查询错误

           System.err.println("SQLException: " + ex.getMessage());

       }

       return SUCCESS;

    }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java 实现 Mysql 数据同步可以通过以下步骤实现: 1. 连接源数据库和目标数据库,获取连接对象; 2. 查询源数据库中需要同步数据; 3. 将查询结果写入到目标数据库中; 4. 关闭连接对象。 具体的实现过程可以参考以下代码: ``` import java.sql.*; public class MysqlSync { public static void main(String[] args) { Connection sourceConn = null; Connection targetConn = null; Statement sourceStmt = null; Statement targetStmt = null; ResultSet rs = null; try { // 连接源数据库 Class.forName("com.mysql.jdbc.Driver"); sourceConn = DriverManager.getConnection("jdbc:mysql://localhost:3306/source_db", "root", "password"); sourceStmt = sourceConn.createStatement(); // 查询源数据库中需要同步数据 rs = sourceStmt.executeQuery("SELECT * FROM source_table"); // 连接目标数据库 targetConn = DriverManager.getConnection("jdbc:mysql://localhost:3306/target_db", "root", "password"); targetStmt = targetConn.createStatement(); // 将查询结果写入到目标数据库中 while (rs.next()) { String name = rs.getString("name"); int age = rs.getInt("age"); targetStmt.executeUpdate("INSERT INTO target_table (name, age) VALUES ('" + name + "', " + age + ")"); } } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } finally { // 关闭连接对象 try { if (rs != null) { rs.close(); } if (sourceStmt != null) { sourceStmt.close(); } if (sourceConn != null) { sourceConn.close(); } if (targetStmt != null) { targetStmt.close(); } if (targetConn != null) { targetConn.close(); } } catch (SQLException e) { e.printStackTrace(); } } } } ``` 这段代码是一个简单的示例,实际应用中需要根据具体需求进行改进和优化。同时,需要注意数据库连接的安全性和性能问题。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值