Thirunavukkarasu
Thirunavukkarasu

Reputation: 208

Resultset into CSV?

I am using opencsv library to dump oracle data into csv file.

I am using below code:

    private void generateCSVFile() {

    try {
        Class.forName("oracle.jdbc.driver.OracleDriver");
        String url = "jdbc:oracle:thin:@localhost:1521:XE";
        Connection con = DriverManager.getConnection(url, "system", "admin");
        PreparedStatement ps = con.prepareStatement("select * from T_USER_DETAILS",ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE,ResultSet.HOLD_CURSORS_OVER_COMMIT);
        ResultSet rs = ps.executeQuery();
        if(rs.next()){
            System.out.println("Name = "+rs.getString(2));
        }

        rs.beforeFirst();
        CSVWriter writer = new CSVWriter(new FileWriter("Test.csv"),'\t');

        writer.writeAll(rs, true);
        JOptionPane.showMessageDialog(null, "CSV Created!","Success!",JOptionPane.INFORMATION_MESSAGE);

    } catch (Exception ex) {
        Logger.getLogger(BackupDB.class.getName()).log(Level.SEVERE, null, ex);
    }

}

but, when i executing this code. csv file is created but there is no content in that file. (note result set prints the 2 column value in console).

Thanks in Advance.

Upvotes: 1

Views: 3426

Answers (1)

Jegg
Jegg

Reputation: 549

Couple of things you may try:

  1. Put writer.close() at the end of try block.
  2. make sure result set is not empty
  3. if still doesn't work comment out sytem.out.println part as well as rs.beforeFirst() part and try again
  4. This is the last thing you want to do, if the first 3 steps still failed. you can replace result set with List<String[]test> in the constructor to give it a try. if it works that means your result set has problems.

Upvotes: 3

Related Questions