Hi guys I am trying to print all the transactions from a sales table (Oracle) into a text file. unfortunately, it is writing only one record while the table has many records that meet the criteria. can someone please tell me what the issue is?
here is my code:
public void actionPerformed(ActionEvent ae) {
Object obj = ae.getSource();
if (obj == btnSave) {
Connection conn = null;
String receno = Lrecno.getText();
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE", "MUNGAI", "gatungo87");
Statement st = conn.createStatement();
String recp = "select PART_DESC, QUNTY, UNIT_SALE, TOTAL_SALE from sales where session_id = '" + receno + "'";
ResultSet rs = st.executeQuery(recp);
while (rs.next()) {
File f = new File("Sale Receipts");
f.mkdir();
BufferedWriter bw = new BufferedWriter(new FileWriter("Sale Receipts/Sale" + Lrecno.getText() + ".TXT"));
String receipt = rs.getString("PART_DESC") + " " + rs.getString("QUNTY") + " "
+ rs.getString("UNIT_SALE") + " " + rs.getString("TOTAL_SALE");
String[] rece = receipt.split("\n");
for (int i = 0; i < rece.length; i++) {
bw.write(receipt);
bw.flush();
bw.newLine();
}
JOptionPane.showMessageDialog(this, "Item sold successfully",
"Success", JOptionPane.PLAIN_MESSAGE);
}
} catch (Exception ad) {
JOptionPane.showMessageDialog(this, ad,
"Error", JOptionPane.PLAIN_MESSAGE);
}
}