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");
}
}
我正在尝试将数据从远程 sql 服务器插入到本地 mysql 。插入开始发生,但现在没有发生,你能告诉我代码到底哪里错了吗?
我在控制台本身上打印的while 块没有被执行。