删除表格中的最后一行。 SQL / Netbeans的

时间:2015-04-27 21:37:27

标签: sql netbeans

在java中,我试图删除数据库的最后一行。数据库有15行,我想删除第15行。这些列称为姓名缩写和分数。

                 Intials         Scores
   rows# 1.         ADS          2343
         2.         DDE          5454      
        15.         TBK           332

我无法选择TBK,因为我希望它删除第15个TBK,无论它是什么,所以可以添加新的TBK。无论我到哪里,它始终必须具体或删除所有行。有人可以帮忙吗?非常感谢那些帮助的人。 :)

2 个答案:

答案 0 :(得分:1)

假设id是一个标识列

DELETE FROM table
WHERE id = (SELECT MAX(id) FROM table)

答案 1 :(得分:0)

  

OP:我正在尝试删除数据库的最后一行

  • 使结果集可更新:ResultSet.CONCUR_UPDATABLE);
  • 将光标设置为最后一条记录:resultSet.last();
  • 删除最后一条记录:resultSet.deleteRow();
  • 要进一步使用rs,您应该设置:resultSet.beforeFirst();
private static int delLastRow(ResultSet resultSet) {
    if (resultSet == null) {
        return 0;
    }
    try {
        resultSet.last();
        int delID = resultSet.getInt(1);
        resultSet.deleteRow();
        System.out.println("Deleted id :" + delID);
        resultSet.beforeFirst();
        return delID;
    } catch (SQLException exp) {
        exp.printStackTrace();
    } finally {
        try {
            resultSet.beforeFirst();
        } catch (SQLException exp) {
            exp.printStackTrace();
        }
    }
    return 0;
}   

   public static void main(String[] args) {
   Connection conn = null;
   Statement stmt = null;
   try{
      Class.forName("com.mysql.jdbc.Driver");
      conn = DriverManager.getConnection(DB_URL,USER,PASS);
      stmt = conn.createStatement(
              ResultSet.TYPE_SCROLL_SENSITIVE,
              ResultSet.CONCUR_UPDATABLE);
      //rs will be scrollable, will not show changes made by others,
      //and will be updatable

      String sql;
      sql = "SELECT * FROM `order details`";
      ResultSet rs = stmt.executeQuery(sql);
      System.out.println("Deleted id :"+ delLastRow(rs));
      ....
  }

enter image description here

相关问题