dump oracle table to csv. date converting

1.1k Views Asked by At

Have a problem with dumping oracle table to csv file. Problem is in date dumping I get date like "23-Jun-2009" with current code, but I need something like "2009-06-23 20:18:44". I get right date date, if put code

while(result.next()) {
   System.out.println(result.getString("D_FORM"));
}

I can not understand how and where dates are converted my code is

package com.my.table2csv;

import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.math.BigDecimal;
import java.net.URL;
import java.sql.*;
import java.util.Calendar;
import java.util.Map;


import au.com.bytecode.opencsv.CSVWriter;

public class Main {

    public static void main(String[] args) throws IOException, SQLException {

        String host = "";
        String user = "";
        String password = "";
        String db = "";
        String table = "";
        String file = "";

        Connection conn = null;

        for(int i=0; i < args.length; i++) {

            if  (args[i].equals("-h")) {
                host = args[++i];
            }

            if (args[i].equals("-u")) {
                user = args[++i];
            }

            if (args[i].equals("-p")) {
                password = args[++i];
            }

            if (args[i].equals("-d")) {
                db = args[++i];
            }

            if (args[i].equals("-t")) {
                table = args[++i];
            }

            if (args[i].equals("-f")) {
                file = args[++i];
            }
        }

        DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
        conn = DriverManager.getConnection("jdbc:oracle:thin:@"+host+":1521:"+db, user, password);
        conn.setAutoCommit(true);

        String queryStr = "SELECT * FROM " + table;

        PreparedStatement preStatementS = conn.prepareStatement(queryStr);

        ResultSet result = preStatementS.executeQuery();

        //while(result.next()) {
        //    System.out.println(result.getString("D_FORM"));
        //}

        CSVWriter writer = new CSVWriter(new FileWriter(file), ',');
        writer.writeAll(result, `enter code here`false);
        writer.close();
    }
}
2

There are 2 best solutions below

0
On

For an explanation see

http://www.oracle.com/technetwork/database/enterprise-edition/jdbc-faq-090281.html#08_01

I solved the issue by adding

-Doracle.jdbc.V8Compatible="true" 

Onto my java command ine

0
On
import java.sql.*;
import java.io.*;
import au.com.bytecode.opencsv.CSVWriter;

public class TableExport {
public static void main(String[] args) {
    // TODO code application logic here
    try{
        DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
        Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE","name/id","password");
        conn.setAutoCommit(false);
        Statement statement = conn.createStatement();
        ResultSet resultData = statement.executeQuery("select * from table");

        CSVWriter writer = new CSVWriter(new FileWriter(new File("D:/Uploads/Output.csv")), '|');
        writer.writeAll(resultData, true);
        // writer.flush();
        writer.close();

    }catch (Exception e){
        System.out.println("Error" +e);
    }
}
}

You will need "oracle-jdbc.jar" and "opencsv.jar" for this code to work.