/** * 数据割接 */ 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);// 打开数据库连接1 Statement stmt = conn.createStatement(); ResultSet rs = stmt .executeQuery("select RoleId,DeptId,NationId from Users "); // Class.forName(driver2);// 加载驱动 conn2 = DriverManager.getConnection(url2, user2, password2);// 打开数据库连接2 PreparedStatement 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);// 打开数据库连接1 PreparedStatement stmt = conn.prepareStatement("select RoleId,DeptId,NationId from Users "); ResultSet rs = stmt.executeQuery(); // Class.forName(driver2);// 加载驱动 conn2 = DriverManager.getConnection(url2, user2, password2);// 打开数据库连接2 PreparedStatement 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; }