【发布时间】:2015-06-27 15:43:19
【问题描述】:
我目前正在一个 servlet 中创建一个私有方法。但是我的PreparedStatement 不断返回null。
private ArrayList<String> emails(String id) {
ArrayList<String> email= new ArrayList<String>();
try {
PreparedStatement st = null;
ResultSet data = null;
DriverManager.getConnection(
"jdbc:postgresql://localhost/test",
"test", "test");
String sql = "SELECT email FROM hdpr.email_table WHERE id='"
+ id+ "'";
data = st.executeQuery(sql);
while (data.next()) {
email.add(data.getString("email"));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NullPointerException e) {
e.getMessage();
}
return email;
}
【问题讨论】:
-
您将其 (
st) 初始化为 null 并且永远不会重新分配它。你期待什么? -
您好 null_logic - 您对我的回答满意还是需要其他帮助? :)