我需要帮助使用Java数组或ArrayList重新编写此方法

我需要帮助使用Java数组或ArrayList重新编写此方法,java,mysql,Java,Mysql,我有一个名为trading_history的数据库表,如下所示: +----------+---------+---------+-----------+------------+ | table_id | item_id | user_id | item_type | phone | +----------+---------+---------+-----------+------------+ | 1 | 4564704 | 629 |

我有一个名为trading_history的数据库表,如下所示:

 +----------+---------+---------+-----------+------------+
 | table_id | item_id | user_id | item_type | phone      |
 +----------+---------+---------+-----------+------------+
 |        1 | 4564704 |     629 |         2 | 3656284    |
 |        2 | 4564705 |     629 |         1 | 3656284    |
 |        3 | 4564773 |     569 |         1 | 8111111    |
 |        4 | 4564792 |     351 |         2 | 0804609120 |
 |        5 | 4564825 |     569 |         1 | 8111111    |
 |        6 | 4564853 |     646 |         1 | 1111144    |
 |        7 | 4564874 |     646 |         1 | 1111144    |
 |        8 | 4564944 |     646 |         1 | 1111144    |
 |        9 | 4564964 |     105 |         2 | 3614794    |
 |       10 | 4564965 |     105 |         1 | 3614794    |
 +----------+---------+---------+-----------+------------+
CREATE TABLE IF NOT EXISTS trading_history_archive LIKE trading_history;

INSERT INTO trading_history_archive
SELECT table_id, item_id , user_id,item_type,phone FROM trading_history
WHERE item_id NOT IN (
SELECT item_id 
FROM trading_history as tdg 
WHERE 20 > ( 
SELECT count(*) 
FROM trading_history AS tdg1 
WHERE tdg.user_id = tdg1.user_id 
AND tdg.item_id > tdg1.item_id ) 
ORDER BY item_id DESC );

DELETE a FROM trading_history a INNER JOIN trading_history_archive b ON    a.table_id = b.table_id;
字段
table\u id
item\u id
是唯一的。该表记录用户销售的商品。Item_ID对于每个用户都是顺序的,这意味着用户最上面的商品是他最先售出的商品,而最下面的商品代表他最近售出的商品

从上表中,我希望定期删除所有项目,只保留用户最近出售的20个项目。也就是说,如果一个用户卖出了100件物品,我想删除80件最旧的物品,留给他20件。对于那些在此期间售出少于20辆车的人,任何东西都不会被删除

我通过编写如下SQL语句实现了这一点:

 +----------+---------+---------+-----------+------------+
 | table_id | item_id | user_id | item_type | phone      |
 +----------+---------+---------+-----------+------------+
 |        1 | 4564704 |     629 |         2 | 3656284    |
 |        2 | 4564705 |     629 |         1 | 3656284    |
 |        3 | 4564773 |     569 |         1 | 8111111    |
 |        4 | 4564792 |     351 |         2 | 0804609120 |
 |        5 | 4564825 |     569 |         1 | 8111111    |
 |        6 | 4564853 |     646 |         1 | 1111144    |
 |        7 | 4564874 |     646 |         1 | 1111144    |
 |        8 | 4564944 |     646 |         1 | 1111144    |
 |        9 | 4564964 |     105 |         2 | 3614794    |
 |       10 | 4564965 |     105 |         1 | 3614794    |
 +----------+---------+---------+-----------+------------+
CREATE TABLE IF NOT EXISTS trading_history_archive LIKE trading_history;

INSERT INTO trading_history_archive
SELECT table_id, item_id , user_id,item_type,phone FROM trading_history
WHERE item_id NOT IN (
SELECT item_id 
FROM trading_history as tdg 
WHERE 20 > ( 
SELECT count(*) 
FROM trading_history AS tdg1 
WHERE tdg.user_id = tdg1.user_id 
AND tdg.item_id > tdg1.item_id ) 
ORDER BY item_id DESC );

DELETE a FROM trading_history a INNER JOIN trading_history_archive b ON    a.table_id = b.table_id;
但是,这需要是由调度器管理的Java应用程序的一部分。我尝试使用SQL语句创建一个方法,但它看起来很笨拙,而且我担心SQL注入:

public class ArchiveTrading {
    private final static Logger LOG = Logger.getLogger (ArchiveTrading.class);
    private int sessionsArchived = 0;
.....
.....
.....
archiveTradingHistory(con,  yymm); 
.....
.....
private void archiveTradingHistory(Connection con, String yymm) {
    try {

        SqlUtil.runSqlUpdate(con, "CREATE TABLE IF NOT EXISTS trading_history_archive LIKE trading_history");
                    LOG.info("Finihsed creating table");
        int inserted = SqlUtil.runSqlUpdate(con, "INSERT INTO trading_history_archive " +
                        " SELECT table_id, item_id , user_id,item_type,phone FROM trading_history "+
                        " WHERE item_id NOT IN ( "+
                        " SELECT item_id  ( " +
                        " FROM trading_history as tdg  " +
                        " WHERE 20 > ( "+
                        " SELECT count(*) "+
                        " FROM trading_history AS tdg1  "+
                        " WHERE tdg.user_id = tdg1.user_id "+
                        " AND tdg.item_id > tdg1.item_id ) "+
                        " ORDER BY item_id DESC ) ");
        LOG.info("trading_history Inserted: " + inserted);
        int deleted = SqlUtil.runSqlUpdate(con, "DELETE a FROM trading_history a INNER JOIN trading_history_archive b ON a.table_id = b.table_id");
        LOG.info("trading_history Deleted: " + deleted);
    } catch (NumberFormatException e) {
        LOG.error(e.getMessage()); 
    } catch (CustomDAOException e) {
        LOG.error(e.getMessage()); 
    }
}
.....
......
}

有人帮我改进这一点,这样我就可以在工作政治中生存。

存档真的是你想要的吗?考虑使用数据库特性(索引等)和更严格的<代码>选择< /COD>语句或“视图”。这降低了复杂性,使您更加灵活(即将限制提高到30变得容易),并且您不需要调度程序。这让我觉得数据库设计和使用都很糟糕。为什么要从表中删除任何内容?只需将视图写入表中,即可显示最后二十个条目。对于定期的数据库查询,我强烈建议使用“操作”方法:使用crontab(*nix)或AT(windows)。使用java实现这一目的是浪费时间和精力,而且经常会遇到问题。是的,我可以使用bash脚本和简单的cron作业实现所有这些,但正如我所指出的,这必须与现有的公司应用程序架构相适应。“我必须”编写它以使用调度程序。我们使用的数据库系统因响应速度慢而臭名昭著,因此不鼓励使用。这是一个地狱般的环境。10年前我也会说同样的话。但是一位经验丰富的DBA会告诉您,走进一家拥有600个表的数据库、支持8个应用程序、5000多名用户的公司;2700万客户和平均每天120万个电话期望数据库设计良好,这简直是天方夜谭。平衡ORM支持和错误报告不像4NF那么容易。在应用程序持久性速度和MIS报告效率之间有一个折衷。上表仅作为图示创建。