简体   繁体   English

如何使用Java代码将Sql Server的数据(行)实时复制到MySql中?

[英]How to Copy Data (Rows) of Sql Server into MySql in Real-time Using Java Code?

I have one GPS tracker device whose data are inserted into Sql Server by Default but problem is I want to create Database in MySql .. 我有一个GPS跟踪器设备,其数据默认情况下已插入Sql Server,但问题是我想在MySql中创建数据库。

So give suggesttion about how to copy or migrate Data of Sqlserver into MySql in Real time-. 因此,建议如何实时将Sqlserver的数据复制或迁移到MySql中。

I'm developing the solution that you're asking for. 我正在开发您要的解决方案。 I've added to my build path in eclipse two library: 我在eclipse两个库中添加了构建路径:

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

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

If you use Eclipse it's a good idea download SQL Explorer and try the connection under that. 如果您使用Eclipse,则最好下载SQL Explorer,然后尝试使用它进行连接。 The string that you must use for the connections are: 您必须用于连接的字符串是:

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

Before than you must pay attention that MySQL and MSSQL accept external connection if you aren't under localhost. 在此之前,如果您不在本地主机下,则必须注意MySQL和MSSQL接受外部连接。 For MySQL you must modify in the configuration file the parameter bind that have 127.0.0.1 as default value, change it in 0.0.0.0 (not secure) or with a list of allowed IP. 对于MySQL,您必须在配置文件中修改默认值为127.0.0.1的参数bind,将其更改为0.0.0.0(不安全)或使用允许的IP列表进行更改。 For MSSQL you must be sure that TCP-IP it's enabled and that the defaults ports are 1433. 对于MSSQL,您必须确保已启用TCP-IP,并且默认端口为1433。

In java you can easily open two different connection and copy records between different DB. 在Java中,您可以轻松地打开两个不同的连接,并在不同的DB之间复制记录。 It's a good idea if the data are big use the PreparedStatement that improve the performance if you, for example, need to copy from MSSQL local to MySQL remote. 例如,如果您需要从本地的MSSQL复制到MySQL的远程数据库,则最好使用PreparedStatement来提高性能,这是一个好主意。 Here a piece of code without exception handler that I've removed for make a message more short. 在这里,我删除了一段没有异常处理程序的代码,以使消息更简短。

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();

您可以安排一个批处理作业,该作业将使用JDBC(或休眠)从SQL Server读取数据并将其插入MySQL Server。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM