'当我删除任何行时,我想重新组织和重新排序行。“例如:如果我删除id为5的行,那么第6行的行应该更改为5。
在netbeans上使用Java jsp和mysql。
编辑方法:
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);
}
}
你不应该重新订购ID。您可以使用ROW_NUMBER进行显示
在delete方法中,您可以运行其他查询
String query = "Update News set id = id-1 where id >" + id + ";";