我有一个问题,我试图获取每个折扣的百分比值,但复杂的部分是有两种类型的折扣。灵活折扣和固定折扣。我试图获取所有折扣的百分比,具体取决于折扣的类型,例如修复了我进入固定折扣表并从FlexiBand折扣表中检索百分比的百分比。
我的表及其属性:
Discount:
DiscountID,
Name,
Type
FixedDiscount:
DiscountID,
Percentage
FlexibleDiscount:
DiscountID
FlexiBand:
UpperBound,
PercentageRate,
DiscountID
我收到错误java.sql.SQLException: Column 'PercentageRate' not found.
当我这样做时:
public ArrayList<Discount> getDiscounts() throws SQLException {
Connection con = null;
PreparedStatement stmt = null;
ResultSet rs = null;
ArrayList<Discount> discount = new ArrayList<Discount>();
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/abpp034?user=abpp034&password=120001772");
stmt = con.prepareStatement("SELECT Discount.DiscountID, Discount.Type, FixedDiscount.Percentage\n" +
"FROM Discount\n" +
"INNER JOIN FixedDiscount\n" +
"ON Discount.DiscountID = FixedDiscount.DiscountID\n" +
"\n" +
"UNION\n" +
"\n" +
"SELECT Discount.DiscountID, Discount.Type, FlexiBand.PercentageRate\n" +
"FROM Discount\n" +
"INNER JOIN FlexiBand\n" +
"ON Discount.DiscountID = FlexiBand.DiscountID");
try {
while (rs.next()) {
Discount d = new Discount();
Fixed_Discount fd = new Fixed_Discount();
FlexiBand fb = new FlexiBand();
d.setDiscountId(rs.getInt("DiscountID"));
d.setType(rs.getString("Type"));
fd.setPercentage(rs.getInt("Percentage"));
fb.setPercentageRate(rs.getInt("PercentageRate"));
discount.add(d);
discount.add(fd);
discount.add(fb);
}
} finally {
if (rs != null) {
try {
rs.close();
} catch (SQLException se) {
System.out.println(se.getErrorCode());
}
}
if (stmt != null) {
try {
stmt.close();
} catch (SQLException se) {
System.out.println(se.getErrorCode());
}
}
if (con != null) {
try {
con.close();
} catch (SQLException se) {
System.out.println(se.getErrorCode());
}
}
}
return discount;
}
奇怪的是,PercentageRate确实存在于FlexiBand表中,但我认为问题出在UNION之后,下一个语句没有被正确识别。
答案 0 :(得分:0)
您可能希望从连接字符串中删除用户名和密码信息。 独立运行时,两个查询都会干净地启动吗?你的RDBMS是mysql还是sql-server?