无法在JDBC中获得结果集?

时间:2016-12-08 09:02:32

标签: java sql database jdbc

谁能告诉我自己做错了什么?我有一个数据库,其中包含一个名为henry_author的表,如下所示:

Table Name: henry_author
| author_num | author |
-----------------------
| 0          | james  |
| 1          | jack   |

我试图从henry_author表中获取这两个数字。

以下是我正在做的事情:

public static void main(String[] args) throws Exception {
    Connection conn = null;
    PreparedStatement pstmt = null;
    Statement stmt = null;

    try {
        conn = getConnection(); // this is basically the DB_URL, USER, PASS and DriverManager.getConnection() method/function I made
        String query = "SELECT author_num FROM henry_author";
        ResultSet rs = stmt.executeQuery(query);

        while (rs.next()) {
            int number = rs.getInt("author_num");
            System.out.println("Author num: + number);
        }
     } catch (SQLException sqle) {
         sqle.printStackTrace();
     }
 }

我一直收到错误:java.lang.NullPointerException。不知道代码有什么问题。

0 个答案:

没有答案