将oracle表转储到csv。日期转换

时间:2013-12-13 09:28:36

标签: java opencsv ojdbc

将oracle表转储到csv文件时出现问题。 问题在于日期转储 我使用当前代码获得“2009年6月23日”的日期,但我需要类似“2009-06-23 20:18:44”的内容。 我得到正确的约会日期,如果把代码

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

我无法理解转换日期的方式和位置 我的代码是

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 个答案:

答案 0 :(得分:0)

有关解释,请参阅

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

我通过添加

解决了这个问题
-Doracle.jdbc.V8Compatible="true" 

在我的java命令ine

答案 1 :(得分:-1)

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);
    }
}
}

你需要&#34; oracle-jdbc.jar&#34;和&#34; opencsv.jar&#34;这个代码可以工作。