Java 打印HashSet集合中的对象
我试图打印hashSet集合中的对象。控制台仅显示最后一个对象(一个对象)。当我用同样的方法使用ArrayList时,我能够打印所有对象。我使用了迭代器方法来打印集合集,请参阅附带的测试Java 打印HashSet集合中的对象,java,hash,collections,set,Java,Hash,Collections,Set,我试图打印hashSet集合中的对象。控制台仅显示最后一个对象(一个对象)。当我用同样的方法使用ArrayList时,我能够打印所有对象。我使用了迭代器方法来打印集合集,请参阅附带的测试 public Set getAllCoupouns()引发异常{ 优惠券=新优惠券(); Set优惠券=新HashSet(); //打开连接 conn=DriverManager.getConnection(Utils.getDBUrl()); //定义执行查询 java.sql.Statement stmt=
public Set getAllCoupouns()引发异常{
优惠券=新优惠券();
Set优惠券=新HashSet();
//打开连接
conn=DriverManager.getConnection(Utils.getDBUrl());
//定义执行查询
java.sql.Statement stmt=null;
试一试{
stmt=conn.createStatement();
//构建SQL查询
String sql=“从优惠券中选择*”;
//从数据库中设置结果
ResultSet ResultSet=stmt.executeQuery(sql);
//构造函数,从结果中检索属性
while(resultSet.next()){
优惠券.setId(resultSet.getLong(1));
优惠券.setTitle(resultSet.getString(2));
息票设置开始日期((日期)结果设置开始日期(3));
优惠券.setEndDate((日期)resultSet.getDate(4));
息票设置金额(resultSet.getInt(5));
CouponType type=CouponType.valueOf(resultSet.getString(6));//将字符串转换为枚举
优惠券.setType(类型);
优惠券.setMessage(resultSet.getString(7));
优惠券定价(resultSet.getDouble(8));
优惠券.setImage(resultSet.getString(9));
优惠券。添加(优惠券);
}
}捕获(SQLE异常){
抛出新异常(“重试所有优惠券失败”);
}最后{
//最后是用于关闭资源的块
试一试{
如果(stmt!=null)
康涅狄格州关闭();
}捕获(SQLSE异常){
//无所事事
}
试一试{
如果(conn!=null)
康涅狄格州关闭();
}捕获(SQLSE异常){
se.printStackTrace();
}
}
退票;
}
当您在循环过程中从外部初始化优惠券时,它会每次不断添加相同的对象,因此,覆盖只会显示最后一个结果
您需要做的是在while
循环中实例化优惠券
,例如:
public Set<Coupon> getAllCoupouns() throws Exception {
Set<Coupon> coupons = new HashSet<Coupon>();
// Open a connection
conn = DriverManager.getConnection(Utils.getDBUrl());
// Define the Execute query
java.sql.Statement stmt = null;
try {
stmt = conn.createStatement();
// build The SQL query
String sql = "SELECT * FROM COUPON";
// Set the results from the database
ResultSet resultSet = stmt.executeQuery(sql);
// constructor the object, retrieve the attributes from the results
while (resultSet.next()) {
Coupon coupon = new Coupon();
coupon.setId(resultSet.getLong(1));
coupon.setTitle(resultSet.getString(2));
coupon.setStartDate((Date) resultSet.getDate(3));
coupon.setEndDate((Date) resultSet.getDate(4));
coupon.setAmount(resultSet.getInt(5));
CouponType type = CouponType.valueOf(resultSet.getString(6)); // Convert String to Enum
coupon.setType(type);
coupon.setMessage(resultSet.getString(7));
coupon.setPrice(resultSet.getDouble(8));
coupon.setImage(resultSet.getString(9));
coupons.add(coupon);
}
} catch (SQLException e) {
throw new Exception("Retriev all the coupons failed");
} finally {
// finally block used to close resources
try {
if (stmt != null)
conn.close();
} catch (SQLException se) {
// do nothing
}
try {
if (conn != null)
conn.close();
} catch (SQLException se) {
se.printStackTrace();
}
}
return coupons;
}
public Set getAllCoupouns()引发异常{
Set优惠券=新HashSet();
//打开连接
conn=DriverManager.getConnection(Utils.getDBUrl());
//定义执行查询
java.sql.Statement stmt=null;
试一试{
stmt=conn.createStatement();
//构建SQL查询
String sql=“从优惠券中选择*”;
//从数据库中设置结果
ResultSet ResultSet=stmt.executeQuery(sql);
//构造函数,从结果中检索属性
while(resultSet.next()){
优惠券=新优惠券();
优惠券.setId(resultSet.getLong(1));
优惠券.setTitle(resultSet.getString(2));
息票设置开始日期((日期)结果设置开始日期(3));
优惠券.setEndDate((日期)resultSet.getDate(4));
息票设置金额(resultSet.getInt(5));
CouponType type=CouponType.valueOf(resultSet.getString(6));//将字符串转换为枚举
优惠券.setType(类型);
优惠券.setMessage(resultSet.getString(7));
优惠券定价(resultSet.getDouble(8));
优惠券.setImage(resultSet.getString(9));
优惠券。添加(优惠券);
}
}捕获(SQLE异常){
抛出新异常(“重试所有优惠券失败”);
}最后{
//最后是用于关闭资源的块
试一试{
如果(stmt!=null)
康涅狄格州关闭();
}捕获(SQLSE异常){
//无所事事
}
试一试{
如果(conn!=null)
康涅狄格州关闭();
}捕获(SQLSE异常){
se.printStackTrace();
}
}
退票;
}
cupon
始终是同一个对象。您只创建了类Cupon
的一个对象,因此集合只包含一个对象(您总是添加相同的对象)。您必须在循环中的每次迭代中创建一个新对象。您正在将同一对象添加到集合中。因此,它只包含一个对象。在while循环内初始化优惠券类
public Set<Coupon> getAllCoupouns() throws Exception {
Set<Coupon> coupons = new HashSet<Coupon>();
// Open a connection
conn = DriverManager.getConnection(Utils.getDBUrl());
// Define the Execute query
java.sql.Statement stmt = null;
try {
stmt = conn.createStatement();
// build The SQL query
String sql = "SELECT * FROM COUPON";
// Set the results from the database
ResultSet resultSet = stmt.executeQuery(sql);
// constructor the object, retrieve the attributes from the results
while (resultSet.next()) {
Coupon coupon = new Coupon();
coupon.setId(resultSet.getLong(1));
coupon.setTitle(resultSet.getString(2));
coupon.setStartDate((Date) resultSet.getDate(3));
coupon.setEndDate((Date) resultSet.getDate(4));
coupon.setAmount(resultSet.getInt(5));
CouponType type = CouponType.valueOf(resultSet.getString(6)); // Convert String to Enum
coupon.setType(type);
coupon.setMessage(resultSet.getString(7));
coupon.setPrice(resultSet.getDouble(8));
coupon.setImage(resultSet.getString(9));
coupons.add(coupon);
}
} catch (SQLException e) {
throw new Exception("Retriev all the coupons failed");
} finally {
// finally block used to close resources
try {
if (stmt != null)
conn.close();
} catch (SQLException se) {
// do nothing
}
try {
if (conn != null)
conn.close();
} catch (SQLException se) {
se.printStackTrace();
}
}
return coupons;
}