如何在JDBC中更新mysql

时间:2019-05-07 02:09:18

标签: java mysql jdbc

我必须从MySQL表中检索数据并将其存储在ArrayList中,然后将它们放入另一个表中,但是问题来了,更新失败,如何解决此问题?谢谢

我已经修改了数据类型,但是我不知道正确与否

public class DailySales {

    public class Record {

        public int deviceId;

        public int goodsVerId;

        public Date orderTime;

        public int latest30DSales;
    }

    public void updateDailySales() {
        Connection conn = null;

        try {

           //after left join table and store those data in ArrayList

            ArrayList<Record> records = new ArrayList<>();

            if (stmt.execute(sql)) {
                ResultSet rs = stmt.getResultSet();
                // Extract data from result set
                while (rs.next()) {
                    ResultSetMetaData metaData = rs.getMetaData();
                    System.out.println();
                    Record record = new Record();
                    record.deviceId = rs.getInt(1);
                    record.goodsVerId = rs.getInt(2);
                    record.orderTime = rs.getDate(3);
                    record.latest30DSales = rs.getInt(4);
                    records.add(record);
                }
                rs.close();
            }

            // put the data in Mysql and update the table

            for (Record record : records) {
                sql = String.format(
                        "UPDATE OM_DAILY_SALES SET DEVICE_ID = %d, GOODS_VER_ID = %d, ORDER_TIME = %s, LATEST_30D_SALES = %d;",
                        record.deviceId, record.goodsVerId, record.orderTime, record.latest30DSales);
                System.out.println(sql);

                System.out.println(stmt.executeUpdate(sql));
            }

            stmt.close();
            conn.close();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public static void main(String[] args) {

        DailySales dailySales = new DailySales();
        dailySales.updateDailySales();

    }
}
UPDATE OM_DAILY_SALES SET DEVICE_ID = 585159, GOODS_VER_ID = 565198, ORDER_TIME = 2019-05-02, LATEST_30D_SALES = 2;

0 个答案:

没有答案