有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!


共 (2) 个答案

  1. # 1 楼答案

    我正在开发您要求的解决方案。 我在eclipse 2库中添加了构建路径:

    mysql-connector-java-5.1.18-bin。jar(用于MySQL) http://www.mysql.it/downloads/connector/j/

    jtds-1.3.0。jar(用于MSSQL) http://jtds.sourceforge.net/

    如果您使用Eclipse,最好下载SQL资源管理器,并在该浏览器下尝试连接。 必须用于连接的字符串为:

     String url="jdbc:jtds:sqlserver://IP-SERVER:1433/DB";
     String url2="jdbc:mysql://IP-SERVER:3306/DB";
    

    在此之前,您必须注意,如果您不在localhost下,MySQL和MSSQL接受外部连接。 对于MySQL,您必须在配置文件中修改默认值为127.0.0.1的参数bind,在0.0.0.0(不安全)中或使用允许的IP列表进行更改。 对于MSSQL,您必须确保TCP-IP已启用,并且默认端口为1433

    在java中,您可以轻松地打开两个不同的连接,并在不同的数据库之间复制记录。 如果数据很大,最好使用PreparedStatement,例如,如果需要从MSSQL本地复制到MySQL远程,则可以提高性能。 这里是一段无异常处理程序的代码,我删除了它以使消息更简短

    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.SQLException;
    
    java.sql.Statement s;
    java.sql.ResultSet rs;
    java.sql.Statement s2;
    java.sql.ResultSet rs2;
    java.sql.PreparedStatement psmtMySQL;
    
    con = DriverManager.getConnection(url, id, pass);
    con2 = DriverManager.getConnection(url2, id2, pass2);
    
    if(con!=null && con2!=null){
        try{
    s = con.createStatement();
    rs = s.executeQuery(sql);        
    
    sql ="INSERT INTO TABLE (FIELD1, FIELD2, FIELD3) " + "VALUES (?,?,?);"; 
    
    con2.setAutoCommit(false);
    psmtMySQL = con2.prepareStatement(sql);        
    con2.setAutoCommit(true);
    while( rs.next() ){
        try {             
            psmtMySQL.setLong(1, rs.getLong("Field1"));
            psmtMySQL.setLong(2, rs.getLong("Field2"));
            psmtMySQL.setString(3, rs.getString("Field3"));
            psmtMySQL.addBatch()    ;
        } catch (SQLException sqle){         
            sqle.printStackTrace();               
        }
    }
    psmtMySQL.executeBatch();
    con2.setAutoCommit(true);
    psmtMySQL.close();
    rs2.close();
    s2.close();
    conn2.close();
    rs.close();
    s.close();
    conn.close();
    
  2. # 2 楼答案

    您可以计划一个批处理作业,该批处理作业将使用JDBC(或hibernate)从SQL Server读取数据并将其插入MySQL服务器