Home > Blockchain >  write the result set into .csv file using java
write the result set into .csv file using java

Time:05-02

I'm trying to write the result set into .csv file using java, for that I wrote code, this created a .csv file but the result set data is not coming into .csv file, I want what ever the result set printed in console should be write in to .csv file. Could some one help me out of this.

public class JdbcEmployee {
    public static void main(String[] args) throws FileNotFoundException {
        try (Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/print_list", "root", "7012");

                Statement stmt = conn.createStatement();) {

            String strSelect = "select * from employee";
            ResultSet rset = stmt.executeQuery(strSelect);

            //int colCount = 0;
            while (rset.next()) {
                String userName = rset.getString("username");
                String firstName = rset.getString("firstname");
                String lastName = rset.getString("lastname");
                String emailId = rset.getString("emailid");
                String address = rset.getString("address");
                System.out.println(userName   ", "   firstName   ", "   lastName);

            }

        } catch (SQLException ex) {
            ex.printStackTrace();
        }

        ArrayList<Employee> employee = new ArrayList<>();
        File csvFile = new File("employee.csv");
        PrintWriter out = new PrintWriter(csvFile);

        for (Employee employeeList : employee) {
            out.println(((Employee) employeeList).getLastName());
        }
        out.close();
    }

    private String getFirstName() {

        return null;
    }
}

CodePudding user response:

You can check the Apache Commons CSV library.

Examples can be found here: https://www.baeldung.com/apache-commons-csv

CodePudding user response:

Just print output to csv within while loop.

Note that you must include ResultSet and PrintWriter into try-with-resources statement as well to ensure the resource is closed after the program is finished.

public static void main(String[] args) {
    File csvFile = new File("employee.csv");
    String strSelect = "select * from employee";
    try (Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/print_list", "root", "7012");
        Statement stmt = conn.createStatement();
        ResultSet rset = stmt.executeQuery(strSelect);
        PrintWriter out = new PrintWriter(csvFile)) {
        //int colCount = 0;
        while (rset.next()) {
            String userName = rset.getString("username");
            String firstName = rset.getString("firstname");
            String lastName = rset.getString("lastname");
            String emailId = rset.getString("emailid");
            String address = rset.getString("address");
            out.println(userName   ", "   firstName   ", "   lastName   ","   emailId   ","   address);
        }
    } catch (FileNotFoundException | SQLException e) {
        e.printStackTrace();
    }
}
  • Related