java数据割接:Sql server2005数据迁移至Sql server2008

来源:互联网 发布:公司域名怎么起 编辑:程序博客网 时间:2024/06/05 11:20
/** * 数据割接 */private String login(HttpServletRequest request,HttpServletResponse response) {// userInfoService.add();//数据割接System.out.println("开始割接===");// 数据库一:String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";String url = "jdbc:sqlserver://192.168.1.218:1433;databaseName=DB1";String user = "sa";String password = "000000";Connection conn = null;// 数据库二:String driver2 = "com.microsoft.sqlserver.jdbc.SQLServerDriver";String url2 = "jdbc:sqlserver://localhost:1433;databaseName=DB2";String user2 = "sa";String password2 = "000000";Connection conn2 = null;//try {Class.forName(driver);// 加载驱动conn = DriverManager.getConnection(url, user, password);// 打开数据库连接1Statement stmt = conn.createStatement();ResultSet rs = stmt.executeQuery("select RoleId,DeptId,NationId from Users ");//Class.forName(driver2);// 加载驱动conn2 = DriverManager.getConnection(url2, user2, password2);// 打开数据库连接2PreparedStatement pstmt = conn2.prepareStatement("insert into Forecast(so2,no2,pm10) values(?,?,?)");// 循环装入数据while (rs.next()) {System.out.println("输出RoleId:" + rs.getInt("RoleId"));System.out.println("输出DeptId:" + rs.getInt("DeptId"));System.out.println("输出NationId:" + rs.getInt("NationId"));pstmt.setDouble(1, rs.getInt("RoleId"));pstmt.setDouble(2, rs.getInt("DeptId"));pstmt.setDouble(3, rs.getInt("NationId"));pstmt.executeUpdate();}System.out.println("割接成功");// 释放资源rs.close();stmt.close();pstmt.close();conn.close();conn2.close();} catch (Exception e) {System.out.println("割接失败");e.printStackTrace();}return index;}


/** * 数据割接2 */private String login2(HttpServletRequest request,HttpServletResponse response) {/** * 1.PreparedStatement是预编译的,对于批量处理可以大大提高效率。 * 2.在对数据库只执行一次性存取的时侯,用 Statement 对象进行处理。 * PreparedStatement 对象的开销比Statement大,对于一次性操作并不会带来额外的好处。 */System.out.println("开始割接===");// 数据库一:String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";String url = "jdbc:sqlserver://192.168.1.218:1433;databaseName=DB1";String user = "sa";String password = "000000";Connection conn = null;// 数据库二:String driver2 = "com.microsoft.sqlserver.jdbc.SQLServerDriver";String url2 = "jdbc:sqlserver://localhost:1433;databaseName=DB2";String user2 = "sa";String password2 = "000000";Connection conn2 = null;//try {Class.forName(driver);// 加载驱动conn = DriverManager.getConnection(url, user, password);// 打开数据库连接1PreparedStatement stmt = conn.prepareStatement("select RoleId,DeptId,NationId from Users ");ResultSet rs = stmt.executeQuery();//Class.forName(driver2);// 加载驱动conn2 = DriverManager.getConnection(url2, user2, password2);// 打开数据库连接2PreparedStatement pstmt = conn2.prepareStatement("insert into Forecast(so2,no2,pm10) values(?,?,?)");// 循环装入数据while (rs.next()) {System.out.println("输出RoleId:" + rs.getInt("RoleId"));System.out.println("输出DeptId:" + rs.getInt("DeptId"));System.out.println("输出NationId:" + rs.getInt("NationId"));pstmt.setDouble(1, rs.getInt("RoleId"));pstmt.setDouble(2, rs.getInt("DeptId"));pstmt.setDouble(3, rs.getInt("NationId"));pstmt.addBatch();//一个一个加入到批处理命令中}int[] count=pstmt.executeBatch();//一把提交给数据库执行System.out.println("割接成功"+count.length);// 释放资源rs.close();stmt.close();pstmt.close();conn.close();conn2.close();} catch (Exception e) {System.out.println("割接失败");e.printStackTrace();}return index;}
0 0
原创粉丝点击