使用JDBC读取数据库

问题描述 投票:0回答:2

我在其中一个程序中使用JDBC和SQLite创建了一个表。现在,我试图从数据库中读取特定的值。我正在尝试获取最后的订单号,但出现错误。我把我的代码放在下面。不知道出了什么问题:

try {
    String sql = "SELECT ordernumber FROM orderTable ORDERBY ordernumber LIMIT 1 DESC";
    Connection connection = DriverManager.getConnection(url);
    Statement stmt = connection.createStatement();
    ResultSet rs = stmt.executeQuery(sql);
    System.out.println(rs.getInt("ordernumber"));
} catch (SQLException e) {
    System.out.println(e.getMessage());
}
java database jdbc
2个回答
0
投票

更改

String sql = "SELECT ordernumber FROM orderTable ORDERBY ordernumber LIMIT 1 DESC";

String sql = "SELECT ordernumber FROM orderTable ORDER BY ordernumber LIMIT 1 DESC";

0
投票

也许您可以将SQL更新为正确的语法,然后运行它。

String sql = "SELECT ordernumber FROM orderTable ORDERBY ordernumber LIMIT 1 DESC";

(右一个):

String sql = "SELECT ordernumber FROM orderTable ORDER BY ordernumber DESC LIMIT 1 ";
© www.soinside.com 2019 - 2024. All rights reserved.