0
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 块没有被执行。

4

1 回答 1

1

就您现在的代码而言,只有一行会插入到 mysql 中。第二个 while 循环在 select 语句的结果集上循环。因此,当它到达那里时,它只会耗尽输出第二个、第三个等结果的结果集。

如果该 while 块没有被执行,那么您可能只有一行与 select 语句匹配。

于 2013-08-12T12:04:23.257 回答