我在将 SQL 结果集与 JDBC 一起使用时遇到问题



这是我的代码:

ResultSet res = con.query("USE VSM; SELECT max(EmailTime) FROM Emails;");
if( ! res.next()) { lastEmailTime = new Date(0); } // if table empty set last email time to 1970
else { lastEmailTime = SQLServer.sdf.parse(res.getString(1)); } //SQL is 1-indexed

我的表当前为空,但 else 语句中的代码正在运行。我知道这一点,因为我在尝试访问 res.getString(1( 时收到一个空指针异常。为什么它不符合 if 条件?

在 jdbc url 连接中已经定义了您正在使用哪个数据库。您不需要为每个请求都设置数据库;

    ResultSet res = con.query("SELECT max(EmailTime) FROM Emails;");
if( ! res.next() || res.getString(1) == null) { lastEmailTime = new Date(0); } // if table empty set last email time to 1970
else { lastEmailTime = SQLServer.sdf.parse(res.getString(1)); } //SQL is 1-indexed

最新更新