0

我正在通过一个 SQL 查询('SELECT_PROCESS_INITIAL_REQUEST')创建一个结果集,它基本上只是从 SQL 数据库表中获取所有内容。

Statement stmt = conn.createStatement();
String sql = SQLDataAdaptor.SELECT_PROCESS_INITIAL_REQUEST;
ResultSet rsRequest = stmt.executeQuery(sql);

然后我想将该数据转储到与原始数据相同但在不同数据库中的表中。我该怎么做?

4

2 回答 2

0

使用 Java 教程网站上的示例-

public static void viewTable(Connection con, String dbName)
throws SQLException {

    //Modify this code according to 2nd database vendor and credentials- 
            String url = "jdbc:sqlserver://MYPC\\SQLEXPRESS;databaseName=MYDB;integratedSecurity=true";
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            Connection conn2 = DriverManager.getConnection(url);

    Statement stmt = null;
    Statement insertStmt = null;
    String query = "select COF_NAME, SUP_ID, PRICE, " +
               "SALES, TOTAL " +
               "from " + dbName + ".COFFEES";
    try {
        stmt = con.createStatement();
        insertStmt = conn2.createStatement();
        ResultSet rs = stmt.executeQuery(query);
        while (rs.next()) {
            String coffeeName = rs.getString("COF_NAME");
            int supplierID = rs.getInt("SUP_ID");
            float price = rs.getFloat("PRICE");
            int sales = rs.getInt("SALES");
            int total = rs.getInt("TOTAL");

            String insertQuery = "INSERT INTO COFFEES (COF_NAME, SUP_ID, PRICE, sales, total) VALUES (coffeeName,supplierID, proce, sales, total);

           try{
               insertStmt.execute(insertQuery);

           }catch(SQLExeption e){
                e.printStackTrace();
           }
         }
    } catch (SQLException e ) {
        e.printStackTrace();
    } finally {
        if (stmt != null) { stmt.close();}
        if(insertStmt != null) {insertStmt.close();}
    }
}
于 2013-08-21T14:31:12.950 回答
0

将其转储到另一个数据库中意味着 Java 将不得不处理您的结果集。因此,恐怕您必须以编程方式执行此操作。

想想“准备好的语句”、“添加批处理方法”,不要忘记每 n 条记录执行一次插入。

于 2013-08-21T14:28:04.880 回答