Resultset To List null


public List<Order> getAllOrdersByCustomerId(int customerId) throws SQLException {
List<Order> AllOrdersByCustomerId = new ArrayList<>();
Connection con = null;
PreparedStatement pstmt = null;
ResultSet rs = null;

try {
String sqlQuery = "SELECT * FROM dbo.Orders WHERE customer_id = ?";
con = JDBCConnection.getConnection();
pstmt = con.prepareStatement(sqlQuery);
pstmt.setInt(1, customerId);
pstmt.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (con != null) {
JDBCConnection.closeConnection(con);
}
if (rs != null) {
JDBCConnection.closeResultSet(rs);
}
}
return AllOrdersByCustomerId;
}

//线程"main"java.lang.NullPointerException:无法调用java.util.List.iterator()"因为"lo"为空

您应该使用ResultSet来读取SQL查询的Order,并将Order添加到您的列表中,如下所示:

public List<Order> getAllOrdersByCustomerId(int customerId) throws SQLException {
List<Order> allOrdersByCustomerId = new ArrayList<>();
String sqlQuery = "SELECT * FROM dbo.Orders WHERE customer_id = ?";
try (Connection con = JDBCConnection.getConnection();
PreparedStatement pstmt = con.prepareStatement(sqlQuery)) {
pstmt.setInt(1, customerId);
try (ResultSet rs = pstmt.executeQuery()) {
while (rs.next()) {
Order order = new Order();
String id = rs.getString("ORDER_ID");
order.setId(id);
// get and set your values ...
allOrdersByCustomerId.add(order);
}
}
} catch (Exception e) {
e.printStackTrace();
}
return allOrdersByCustomerId;
}

相关内容

  • 没有找到相关文章

最新更新