簡體   English   中英

如何將表值從數據庫復制到另一個數據庫

[英]How to copy table values from a database to another database

我已經實現了此代碼,但是不起作用。 誰能告訴我錯誤在哪里的原因是我找不到它:(這是代碼:

PreparedStatement stmA = connsqlite.prepareStatement("SELECT * FROM  dbTable");
PreparedStatement stmB = connphpmyadmin.prepareStatement("INSERT INTO codes VALUES ('"??????????"')");
stmA.executeQuery("DROP TABLE dbTable");
stmB.executeUpdate();

connsqlite.close();
connphpmyadmin.close();

如您所見,我要同步2個不同的數據庫,一台本地數據庫和一台服務器。 同步完成后,我想從本地數據庫中刪除tha表。 我認為答案在“ ??????”內部 我已經放了,但是我不知道在那寫什么。 如果有人知道,請幫助我理解。 謝謝!

這只是一個示例,說明如何執行此操作。 記得? 標記是要傳遞的一個參數/值。 因此,您需要針對您的目的修改查詢。 我不知道您的表具有哪種類型的列,因此如果需要,可以對此進行修改。

        //Representation of single item in table
        class DbTableItem {
            private String value1;
            private String value2;
            private String value3;
            //... Whatever field that required to exchange information between 2 tables
            public String getValue1() {
                return value1;
            }

            public void setValue1(String value1) {
                this.value1 = value1;
            }

            public String getValue2() {
                return value2;
            }

            public void setValue2(String value2) {
                this.value2 = value2;
            }

            public String getValue3() {
                return value3;
            }

            public void setValue3(String value3) {
                this.value3 = value3;
            }
        }

        Connection connection1 =//your connection to local db;
        Connection connection2 =//your connection to another db;
        PreparedStatement stmA = connection.prepareStatement("SELECT * FROM  dbTable");
        //Executing query to retreive data
        ResultSet resultSet = stmA.executeQuery();

        List<DbTableItem> dbTableList = new ArrayList<>();

        //Setting items and adding to list
        while (resultSet.next()){
            DbTableItem dbTableItem = new DbTableItem();
            dbTableItem.setValue1(resultSet.getString("COLUMN_NAME"));
            dbTableItem.setValue2(resultSet.getString("COLUMN_NAME"));
            dbTableItem.setValue2(resultSet.getString("COLUMN_NAME"));
            dbTableList.add(dbTableItem);
        }
        //Preparing next query for batch process
        PreparedStatement stmB = connection2.prepareStatement("INSERT INTO codes VALUES (?,?,?)");
        //Adding to batch
        for(DbTableItem dbTableItem: dbTableList) {
            stmB.setObject(1, dbTableItem.getValue1());
            stmB.setObject(2, dbTableItem.getValue2());
            stmB.setObject(2, dbTableItem.getValue3());
            //And so on
            stmB.addBatch();
        }
        //Executing batch with query
        stmB.executeBatch();

        //Droping table
        //Closing connection1 and connection2

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM