Last active
March 24, 2020 14:42
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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