Skip to content

Instantly share code, notes, and snippets.

@yusiwen
Last active March 24, 2020 14:42
Show Gist options
  • Save yusiwen/1182cf42104ec01898a8512e1b16d1aa to your computer and use it in GitHub Desktop.
Save yusiwen/1182cf42104ec01898a8512e1b16d1aa to your computer and use it in GitHub Desktop.
[Exporting from oracle database to csv file] Exporting from oracle database to csv file #java #cvs #oracle
import java.io.*;
import java.sql.*;
public class CsvF {
public static void main(String[] args) {
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe", "system", "sandp");
conn.setAutoCommit(false);
Statement st = conn.createStatement();
ResultSet rs = st.executeQuery("select * from tet where to_char(S_DATE,'HH24') = '23'");
ResultSetMetaData rsmd = rs.getMetaData();
FileWriter cname = new FileWriter("D:\\asd.csv");
BufferedWriter bwOutFile = new BufferedWriter(cname);
StringBuffer sbOutput = new StringBuffer();
sbOutput.append("S_DATE");
bwOutFile.append(sbOutput);
bwOutFile.append(System.getProperty("line.separator"));
System.out.println("No of columns in the table:" + rsmd.getColumnCount());
for (int i = 1; i <= rsmd.getColumnCount(); i++) {
String fname = rsmd.getColumnName(i);
}
System.out.println();
while (rs.next()) {
System.out.print(rs.getString(1));
bwOutFile.append(rs.getString(1));
bwOutFile.append(System.getProperty("line.separator"));
bwOutFile.flush();
System.out.println();
}
conn.close();
} catch (SQLException se) {
se.printStackTrace();
} catch (Exception e) {
System.out.println("Unable to connect to database" + e);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment