【问题标题】:How to update row id when previous row is deleted? Im using mysql query with jsp删除前一行时如何更新行ID?我在 jsp 中使用 mysql 查询
【发布时间】:2019-04-11 08:11:25
【问题描述】:

'我想在删除任何行时重新组织和重新排序行。” 例如:如果我删除 id=5 的行,则第 6 个 id 的行应更改为 5。

Java jsp 和 mysql 在 netbeans 上使用。

编辑方法:

public void edit(int id, String title, String description, String detail, String category, String image){
     String sql = "update News set title = ?, description=?, Details=?, category=?, image=?" + " where id=? ";   
     PreparedStatement ps;
        try {
            ps = DBUtils.getPreparedStatement(sql);
            ps.setString(1,title);
            ps.setString(2,description );
            ps.setString(3,detail);
            ps.setString(4,category );
            ps.setString(5,image);
            ps.setInt(6,id);
            ps.executeUpdate();
        } catch (ClassNotFoundException | SQLException ex) {
            Logger.getLogger(DataAccess.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

删除方法:

public void delete(int id){
    try {
        String sql = "delete from News where id = ?";
        PreparedStatement ps = DBUtils.getPreparedStatement(sql);
        ps.setInt(1, id);
        ps.executeUpdate();
    } catch (ClassNotFoundException | SQLException ex) {
        Logger.getLogger(DataAccess.class.getName()).log(Level.SEVERE, null, ex);
    }
}

【问题讨论】:

    标签: java mysql jsp


    【解决方案1】:

    您不应重新排序 ID。您可以将 ROW_NUMBER 用于显示目的

    【讨论】:

      【解决方案2】:

      在删除方法中,您可以运行附加查询

      String query = "Update News set id = id-1 where id >" + id + ";";
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-07-16
        • 1970-01-01
        • 1970-01-01
        • 2021-03-11
        相关资源
        最近更新 更多