【发布时间】:2017-12-19 11:35:39
【问题描述】:
我有一个表名“Coupon”,我在 Eclipse 上使用 java。
我有一个方法 getCoupon(long id);通过它的 id 给我优惠券,我是这样写的:
public Coupon getCoupon(long id) {
Connection con = ConnectionPool.getInstance().getConnection();
String sql = "SELECT * FROM Coupon WHERE TYPE=?";
Coupon coupon = new Coupon();
try (PreparedStatement pstmt = con.prepareStatement(sql);){
pstmt.setLong(1, id);
try (ResultSet rs = pstmt.executeQuery();) {
if (rs.next()) {
coupon.setId(rs.getLong(1));
coupon.setTitle(rs.getString(2));
coupon.setStartDate(rs.getDate(3));
coupon.setEndDate(rs.getDate(4));
coupon.setAmount(rs.getInt(5));
coupon.setType(CouponType.valueOf(rs.getString(6)));
coupon.setMessage(rs.getString(7));
coupon.setPrice(rs.getDouble(8));
coupon.setImage(rs.getString(9));
} else {
System.out.println("Coupon ID: " + id + " could not be found\n");
}
}
} catch (SQLException e) {
CouponSystemException ex = new CouponSystemException("Coupon ID: " + id + " could not be retrieved\n", e);
System.out.println(ex.getMessage());
System.out.println(e);
}
ConnectionPool.getInstance().returnConnection(con);
return coupon;
}
我想做另一种方法,它通过它的类型给我优惠券!但是 TYPE COLUMN 不在第一列中,它给了我例外。 有什么建议吗?
【问题讨论】:
-
第一个建议...格式化您的问题
-
您遇到了什么异常?按其他列获取行有什么问题?提供更多细节。