我正在开发一个用于管理项目库存的系统,并试图获取接近其到期日期的库存项目的列表
这是表的创建方式:
Query_Statement.executeUpdate("CREATE TABLE STOCK_PURCHASE ( SP_ID INTEGER PRIMARY KEY AUTOINCREMENT,"
+ "USER_ID INTEGER,STOCK_ID INTEGER,SUPPLIER_ID INTEGER,SP_ENTRY_DATE TEXT,"
+ "SP_PURCHASE_PRICE REAL, SP_SELLBY_DATE TEXT, SP_QUANTITY REAL,"
+ "FOREIGN KEY (USER_ID) REFERENCES USER (USER_ID),"
+ "FOREIGN KEY (STOCK_ID) REFERENCES STOCK (STOCK_ID),"
+ "FOREIGN KEY (SUPPLIER_ID) REFERENCES SUPPLIER (SUPPLIER_ID))");
这是我使用的功能:
public ArrayList<String> GetExpiredStock(){
Connection dbConnection = null;
ResultSet list = null;
ArrayList<String> ls = new ArrayList<String>();
LocalDate currentDate = LocalDate.now();
//System.out.println(today);
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");
try {
dbConnection = DriverManager.getConnection("jdbc:sqlite:"+dbName+".db");
Statement Query_Statement = dbConnection.createStatement();
Query_Statement.setQueryTimeout(10);
list = Query_Statement.executeQuery("SELECT SP_ENTRY_DATE, STOCK_ID FROM STOCK_PURCHASE"); //this works
while (list.next()) {
try {
LocalDate expDate = LocalDate.parse(list.getString("SP_SELLBY_DATE"), formatter);
LocalDate monthAway = expDate.minusMonths(1);
System.out.println(currentDate);
if(currentDate.isAfter(monthAway)) {
int id = list.getInt("STOCK_ID");
ResultSet ids = Query_Statement.executeQuery("SELECT STOCK_NAME FROM STOCK WHERE STOCK_ID=" + id);
ls.add(ids.getString("STOCK_NAME") + "\t\t" +
list.getString("SP_SELLBY_DATE") + getStockQuant(list.getInt("STOCK_ID"),
currentDate));
}
}catch(SQLException e) {
System.err.println(e);
continue;
}
}
} catch (SQLException e) {
System.err.println(e.getMessage());
} finally {
try {
if (dbConnection != null)
dbConnection.close();
} catch (SQLException e) {
System.err.println(e);
}
}
return ls;
}
我希望它可以得到到期日期。但是它一直在说:
java.sql.SQLException: no such column: 'SP_SELLBY_DATE'
编辑:
我将代码更改如下:
public ArrayList<String> GetExpiredStock(){
Connection dbConnection = null;
ResultSet list = null;
ArrayList<String> ls = new ArrayList<String>();
LocalDate currentDate = LocalDate.now();
//System.out.println(today);
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");
try {
dbConnection = DriverManager.getConnection("jdbc:sqlite:"+dbName+".db");
Statement Query_Statement = dbConnection.createStatement();
Query_Statement.setQueryTimeout(10);
list = Query_Statement.executeQuery("SELECT SP_SELLBY_DATE, STOCK_ID FROM STOCK_PURCHASE"); //this works
while (list.next()) {
try {
String da = list.getString("SP_SELLBY_DATE");
int id = list.getInt("STOCK_ID");
System.out.println("Executed on id = " + id);
LocalDate expDate = LocalDate.parse(da, formatter);
LocalDate monthAway = expDate.minusMonths(1);
System.out.println(currentDate);
if(currentDate.isAfter(monthAway)) {
ResultSet ids = Query_Statement.executeQuery("SELECT STOCK_NAME FROM STOCK WHERE STOCK_ID=" + id);
ls.add(ids.getString("STOCK_NAME") + "\t\t" +
da + "\t\t"+ getStockQuant(id,
currentDate));
}
}catch(SQLException e) {
System.err.println(e);
continue;
}
}
} catch (SQLException e) {
System.err.println(e.getMessage());
} finally {
try {
if (dbConnection != null)
dbConnection.close();
} catch (SQLException e) {
System.err.println(e);
}
}
return ls;
但是在第一次迭代后仍然失败
答案 0 :(得分:3)
您的SQL查询没有必填列,请将其添加:
SELECT SP_ENTRY_DATE, STOCK_ID, SP_SELLBY_DATE FROM STOCK_PURCHASE
答案 1 :(得分:0)
我相信您在循环中list.getString("SP_SELLBY_DATE")
对其进行了两次查询,这应该是根本原因。相反,应该在获取变量时使用变量,以避免在光标更改后再次调用该变量。