使用 Eclipse 从数据库中检索列表时出现问题
Posted
技术标签:
【中文标题】使用 Eclipse 从数据库中检索列表时出现问题【英文标题】:Having an issue retrieving list from database using Ecplise 【发布时间】:2021-02-08 18:03:15 【问题描述】:我目前正在做一个优惠券项目。在处理我的外观类时,我遇到了一个问题,通过从带有外键的表中输入客户的 ID 来获取客户已购买的优惠券列表。
我使用的方法:
@Override
public List<Coupon> getPurchasedCoupons(int customerID) throws SQLException
Connection connection = pool.getConnection();
ArrayList<Coupon> customerCoupon = new ArrayList<>();
ArrayList<Long> customerCouponID = new ArrayList<>();
Statement stmt = null;
long coupID = 0;
stmt = connection.createStatement();
ResultSet resultSet = stmt.executeQuery("SELECT * FROM `couponsystem`.`customers_vs_coupons` WHERE (`CUSTOMER_ID` = '?')");
while ((resultSet != null) && (resultSet.next()))
coupID = resultSet.getLong("COUPON_ID");
customerCouponID.add(coupID);
Iterator<Long> myIterator = customerCouponID.iterator();
while (myIterator.hasNext())
Long couponID = myIterator.next();
resultSet = stmt.executeQuery(
"SELECT * FROM `couponsystem`.`customers_vs_coupons` where COUPON_ID = " + couponID);
while (resultSet.next())
Coupon coupon = new Coupon(resultSet.getInt(1), resultSet.getInt(2),
Category.categoryFor(resultSet.getInt(3)), resultSet.getString(4), resultSet.getString(5),
resultSet.getDate(6), resultSet.getDate(7), resultSet.getInt(8), resultSet.getDouble(9),
resultSet.getString(10));
customerCoupon.add(coupon);
ConnectionPool.getInstance().restoreConnection(connection);
return customerCoupon;
优惠券类别:
import java.util.Date;
public class Coupon
private int id;
private int companyID;
private Category category;
private String title;
private String description;
private Date startDate;
private Date endDate;
private int amount;
private double price;
private String image;
public Coupon(int companyID, Category category, String title, String description, Date startDate, Date endDate,
int amount, double price, String image)
this.companyID = companyID;
this.category = category;
this.title = title;
this.description = description;
this.startDate = startDate;
this.endDate = endDate;
this.amount = amount;
this.price = price;
this.image = image;
public Coupon(int id, int companyID, Category category, String title, String description, Date startDate,
Date endDate, int amount, double price, String image)
this.id = id;
this.companyID = companyID;
this.category = category;
this.title = title;
this.description = description;
this.startDate = startDate;
this.endDate = endDate;
this.amount = amount;
this.price = price;
this.image = image;
public Coupon()
super();
public void setId(int id)
this.id = id;
public void setCompanyID(int companyID)
this.companyID = companyID;
public Category getCategory()
return category;
public void setCategory(Category category)
this.category = category;
public String getTitle()
return title;
public void setTitle(String title)
this.title = title;
public String getDescription()
return description;
public void setDescription(String description)
this.description = description;
public Date getStartDate()
return startDate;
public void setStartDate(Date startDate)
this.startDate = startDate;
public Date getEndDate()
return endDate;
public void setEndDate(Date endDate)
this.endDate = endDate;
public int getAmount()
return amount;
public void setAmount(int amount)
this.amount = amount;
public double getPrice()
return price;
public void setPrice(double price)
this.price = price;
public String getImage()
return image;
public void setImage(String image)
this.image = image;
public int getId()
return id;
public int getCompanyID()
return companyID;
@Override
public String toString()
return "Coupon: ID = " + id + ", Company ID = " + companyID + ", Category = " + category + ", Title = " + title
+ ", Description = " + description + ", Start Date = " + startDate + ", End Date = " + endDate
+ ", Amount = " + amount + ", Price = " + price + ", IMAGE = " + image;
调用方法:
public List<Coupon> getCustomerCoupons() throws SQLException
return coup.getPurchasedCoupons(customerID);
我的 SQL 表:
Coupon_vs_Customer 表仅包含 2 行。它们都是其他表的外键。 CustomerID 连接到“CUSTOMERS”表,而 coupon_ID 连接到“COUPONS”表
CustomerID Coupon_ID
1 1
1 2
1 3
正如您在上面看到的,ID 为 1 的客户拥有 3 张优惠券,我正在尝试在我的 eclipse 项目的列表中阅读它们。
我没有得到任何异常,但是我得到了一个空数组列表。我似乎无法解决这个问题,因为我对 JDBC 很陌生。
【问题讨论】:
'?'
字面上是一个带问号的字符串,它不是参数。即使是这样,您也没有使用准备好的语句,因此您没有填充参数。此外,您的代码表现出所谓的 N+1 查询问题,您应该改用单个查询。
【参考方案1】:
您的第一个查询尝试查找 id = "?" 的客户。也许这是试图创建一个准备好的语句,但是你需要更多的步骤
String query = "SELECT COUPON_ID FROM couponsystem.customers_vs_coupons WHERE CUSTOMER_ID = ?";
PreparedStatement statement = conn.prepareStatement(query);
statement.setInt(1, customerID);
ResultSet resultSet = statement.executeQuery();
【讨论】:
我应该在我的代码的哪一部分添加这一步,我应该用我的第一个查询替换它吗? 是的,这不是您的第一个查询 现在我得到一个异常“线程中的异常”main“java.sql.SQLException:列索引超出范围,3 > 2。”就像它试图访问一个不存在的列一样。事情是。我正在尝试从与我一起购买的客户的另一张桌子上获取所有优惠券。 我猜这是来自您的第二个查询?尝试以与第一个相同的方式重写它以使用准备好的语句,并且不要执行“SELECT *”,而是按名称列出要包含在查询中的列。 我认为问题在于这一行 'coupID = resultSet.getInt("COUPON_ID");'这条线导致异常,我不知道为什么。我使用了你的方法,它给了我那条线的错误。以上是关于使用 Eclipse 从数据库中检索列表时出现问题的主要内容,如果未能解决你的问题,请参考以下文章
无限滚动 UITableView 在从服务器检索数据后向上滚动时出现故障