我有一個文本文件的下一行數據庫:插入或更新基於文件
其中包含的信息與書的順序在一個單一的訂單可以包含多個副本一本或多本書。
我想計算客戶在單個訂單中花費的總金額,因此numCopies * bookPrice爲訂單中的每個ISBN。第一個命令是(1 * 15.99)= 15.99。第二個(10 * 11.99 + 20 * 14.99)= 419.70。第三個(1 * 15.99)。
我正在瀏覽帶有掃描器的文本文件,隨着文本文件的添加,將文本文件的元素添加到文本文件中。爲了計算訂單總額,我試圖累積每本書在一行上出現的成本,直到達到orderNum發生變化的一行爲止。然後將該總訂單數量插入數據庫。
不確定我可以正確檢查何時應該釋放訂單總額並重置爲下一個訂單號並將此總計插入到數據庫中。或者基本上按照我的計算總計,然後在讀完所有訂單數據後更新數據庫。
相關代碼:
class TestConnect {
static final String DROP_TABLE_BOOKORDER = "drop table if exists bookorder;";
static final String CREATE_TABLE_BOOKORDER =
"create table bookorder (" +
" ordernumber VARCHAR(13) NOT NULL," +
" customername VARCHAR(100)," +
" orderdate DATE NOT NULL," +
" discount NUMERIC(8,2) NOT NULL," +
" totalamount INT NOT NULL," +
" PRIMARY KEY (ordernumber));";
static final String INSERT_BOOKORDER_DATA = "insert into bookOrder (orderNumber, customerName, orderDate, discount, totalAmount) values (?, ?, ?, ?, ?);";
createTables();
loadTables();
static void createTables() throws SQLException {
Statement stmt = conn.createStatement();
stmt.execute(DROP_TABLE_BOOKORDER);
stmt.execute(CREATE_TABLE_BOOKORDER);
}
static void loadTables() throws SQLException, FileNotFoundException, ParseException {
Scanner lineScanner = new Scanner(new File(orderData));
String headerLine = lineScanner.nextLine();
float totalOrderAmount = 0.0f;
String previousKnownOrderNum = null;
while (lineScanner.hasNextLine()) {
String[] retrievedOrderLine = lineScanner.nextLine().split("\t");
// if order number exists
if (!Objects.equals(retrievedOrderLine[0], "")) {
previousKnownOrderNum = retrievedOrderLine[0];
// order number
bookOrderStmt.setString(1, retrievedOrderLine[0]);
//customer name
bookOrderStmt.setString(2, retrievedOrderLine[1]);
//order date
//handle date format conversion
String orderDate = retrievedOrderLine[2];
System.out.println("now processing book order date as " + retrievedOrderLine[2]);
SimpleDateFormat formattedDate = new SimpleDateFormat("yyyy-MM-dd");
java.util.Date date = formattedDate.parse(orderDate);
java.sql.Date sqlFormattedDate = new java.sql.Date(date.getTime());
bookOrderStmt.setDate(3, sqlFormattedDate);
// discount
bookOrderStmt.setFloat(4, Float.parseFloat(retrievedOrderLine[3]));
}
// if line contains a book isbn
if(!Objects.equals(retrievedOrderLine[4], "")) {
// if next line contains an orderNumber (the length of the line = 10), then insert accumulated orderTotal into database
if (lineScanner.nextLine().length() == 10) {
float thisLinesAmount = Float.parseFloat(retrievedOrderLine[5]) * Float.parseFloat(retrievedOrderLine[6]);
totalOrderAmount = totalOrderAmount + thisLinesAmount;
bookOrderStmt.setFloat(5, totalOrderAmount);
// reset total amount
totalOrderAmount = 0.0f;
// else continue to accumulate
} else {
// insert a temp value for totalAmount
totalOrderAmount = totalOrderAmount + Float.parseFloat(retrievedOrderLine[5]) * Float.parseFloat(retrievedOrderLine[6]);
bookOrderStmt.executeUpdate();
bookOrderStmt.close();
}
}
}
}
}
測試文件:
OrderNum CustName OrderDate Discount ISBN NumCopies BookPrice ShipmentID ShipmentDate ShipCopies
N201700001 John Doe 2017-4-24 1 1234567891 1 15.99 S0000003 2017-4-25 1
N201700002 Jane Doe 2017-3-1 41.97 1234567890 10 11.99 S0000001 2017-3-2 5
S0000002 2017-3-15 5
1234567891 20 14.99 S0000001 2017-3-2 15
S0000002 2017-3-15 5
N201700003 John Jones 2017-5-1 0 1234567891 1 15.99
負載的每一行文件放入臨時表並從中填充主表。 –