Question

I have a java function that is meant to take strings from jlist called "readyList" and pulling data from mysql workbench tables with the intent to write a line for each string in a .csv file. With the current code it sucessfully pulls the data one at a time like i intended but it only writes the last line instead of all the lines. I want to have all the lines written in the .csv file. Please help!

int[] selectedIx = readyList.getSelectedIndices(); 
    for (int i = 0; i < selectedIx.length; i++) {
      //  while (i < selectedIx.length) {
           Object sel = readyList.getModel().getElementAt(selectedIx[i]);

    Statement s1 = DBConnect.connection.createStatement();
    String selTable01 = "SELECT Sku As s, Qty As q, Orig_Retail As prce, Orig_Sku As orgsk, Store As strcd "
                + "FROM completed_lines WHERE Form_Name = '" + sel + "' AND Warranty = 'true'";

        s1.execute(selTable01);
        try (ResultSet rs01 = s1.getResultSet()) {
            fWriter = new FileWriter("Exports/Transfers/" + /* frmNm.replace(":", "_") */"EBW_" + GtDates.fdate + ".csv", false);
            writer = new BufferedWriter(fWriter);
            String header = "slip_number,slip_type,out_store,in_store,item_number,quantity,price,comment,to_num";
            writer.write(header);  
            writer.newLine();
            while (rs01.next()) {
                String strcode = rs01.getString("strcd");
                String sku = rs01.getString("s");
                String qty = rs01.getString("q");
                String price = rs01.getString("prce");
                String orgsku = rs01.getString("orgsk");
                //System.out.println(frmNm.split("_")[1] + qty + "," + sku + "," + vendor + "," + desc1 + "," + reas + "," + descdmg + "," + orgR + "," + nwsku + "," + desc2 + "," + qtyI);
                String line = ""+","+"out"+","+strcode+","+"RTV"+","+sku+","+qty+","+price+","+"EBW"+","+orgsku;
                writer.write(line);
                writer.newLine();

            }
        } 
      //   JOptionPane.showMessageDialog(frame, "All Data from Selected Forms has been Exported");
    }
       //  FormCompelted();            

writer.close();

}

}

Was it helpful?

Solution

A few issues with this code. The reason you're only getting the last result is because of this line:

fWriter = new FileWriter("Exports/Transfers/" + /* frmNm.replace(":", "_") */"EBW_" + GtDates.fdate + ".csv", false);

This line is inside your loop. The false as the last parameter tells FileWriter not to append. In other words, a false means overwrite the file if it exists. Since this is in your loop, each result overwrites the file that the last result created. You should create the FileWriter outside of your loop, probably in a try with resources. That will allow you to remove your writer.close() call, which should have been in a finally block anyway.

Not related to your original question but something you should be aware of: You're creating a new Statement with each loop iteration. This can be an expensive operation. You should use a PreparedStatement instead. Create it outside your loop and then just set the parameter and execute it inside the loop. It also implements AutoCloseable, so you can create it in a try with resources too, probably the same one you create your FileWriter in.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top