我正在尝试根据"replace"的数据更新数据库。 replace的数据列很少,我想相应地更新数据库abcd中的那些列。

但是,当我运行此代码时,只有最后一列表示数据库中的最新数据已更新,我猜这种情况下迭代不正确。

所以请帮帮我。感谢您的建议。

private static void updateDB(HashMap<String, HashMap<Integer, String>> map) throws ConfigException, SQLException {
        MConfig config = ScriptsTools.init();
        ConnPool select = ScriptsTools.openPool("database", config);
        Connection write = select.getWrite();
         StringBuilder replace = new StringBuilder();

         replace.append("REPLACE INTO abcd (a,b,c,d,e) values ");

         Iterator<Entry<String, HashMap<Integer, String>>> it = map.entrySet().iterator();
            while (it.hasNext()) {
                Map.Entry<String, HashMap<Integer, String>> pair = it.next();

                replace.append("('");
                replace.append(pair.getKey());
                replace.append("','");
                replace.append(pair.getValue().get(2));
                replace.append("','");
                replace.append(pair.getValue().get(11));
                replace.append("',");
                replace.append("now()");
                replace.append(",");
                replace.append("now()");
                replace.append("), ");


            }

            replace.delete(replace.length()-2, replace.length());
            //System.out.println("Query : "+replace.toString());
            String words = replace.toString();
            System.out.println(words);

            Statement st = write.createStatement();
            st.executeUpdate(replace.toString());
         }

最佳答案

因为使用Statement而不是PreparedStatement很容易使用以下命令打印要在数据库上执行的查询

System.out.println(replace.toString());


采取它并在您首选的数据库客户端中执行它,并检查它是否按预期工作。

09-11 19:41
查看更多