Monday, 12 August 2013

Inserting from remote sql server to local mysql server

Inserting from remote sql server to local mysql server

import java.sql.*;
public class jdbc_test {
public static void main(String[] args)
{
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
System.out.println("Driver loaded sql server");
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
System.out.println("Driver loaded mysql");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
System.out.println("not loaded");
}
String connectionUrl = "jdbc:sqlserver://172.*.*.*:1433;"
+"databaseName=Interface;user=user;password=pwd";
String connectionUrl2="jdbc:odbc:Occupancy_Mysql";
Connection con=null;
Connection con2=null;
Statement stmt = null;
ResultSet rs = null;
PreparedStatement pstmt = null;
try {
con = DriverManager.getConnection(connectionUrl);
System.out.println("CONNECTED sql server");
con2 = DriverManager.getConnection(connectionUrl2);
System.out.println("CONNECTED2 mysql");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
stmt = con.createStatement();
rs=stmt.executeQuery("select * FROM
[Interface].[dbo].[VwZoneCount]");
System.out.print("Select executed");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
while (rs.next()) {
String zoneName = rs.getString("Zone Name");
int zonecount = rs.getInt("Zone Count");
String phasename = rs.getString("Phase Name");
String insertSql = "insert into
occupancy.occupancy_phase_2(ZoneName,ZoneCount,PhaseName,time_stamp)values('"+zoneName+"',"+zonecount+",'"+phasename+"',now())";
pstmt = con2.prepareStatement(insertSql);
pstmt.executeUpdate();
}
while(rs.next())
{
System.out.print(rs.getString("Zone Name")+"\t");
System.out.print(rs.getInt("Zone Count")+"\t");
System.out.println(rs.getString("Phase Name"));
}
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
System.out.println("out insert done");
}
}
I am trying to insert the data from remote sql server to local mysql . The
insert was happening at first , but now its not happening ,can you tell me
where exactly is the code wrong .
The while block where i am printing on the console itself is not getting
executed .

No comments:

Post a Comment