获取表格最大ID的最佳方法是什么?

时间:2015-08-03 02:47:33

标签: jdbc

获取表格最大ID的最佳方法是什么?下面我粘贴了错误和代码。所以我打算使用afterLast()方法来获取最大ID,但是我得到了一个错误。

错误:

SQLException: feature not supported

代码:

public class ex03 {
    public static void main(String[] args) {
    String url = "jdbc:ucanaccess://C:/Users/dave_000/My_WorkSpace/Eclipse_Workspaces/workspace-jsp/T_01_JDBC_01.accdb";

    Connection con;

    // Get Max ID
    Statement stmt0;
    String query0 = "select * from user";

    try {
        Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
    } catch (java.lang.ClassNotFoundException e) {
        System.err.print("ClassNotFoundException: ");
        System.err.println(e.getMessage());
    }

    try {
        con = DriverManager.getConnection(url, "", "");
        stmt0 = con.createStatement();


        // Get last ID
        ResultSet rs = stmt0.executeQuery(query0);
        rs.afterLast();
        int maxID = rs.getInt("ID");
        System.out.println(maxID);


        pstmt1.close();
        con.close();
    } catch (SQLException ex) {
        System.err.println("SQLException: " + ex.getMessage());
    }
}
}

1 个答案:

答案 0 :(得分:0)

使用SQL查找最大值会更有效:

select max(id) from user