Reputation: 123
There are no errors thrown. It will work if I specify the exact path but not if I just say "output.csv"
ResultSet rs = statement.executeQuery("select * from geninfo");
try {
CSVWriter writer = new CSVWriter(new FileWriter(new File("output.csv")));
writer.writeAll(rs, true);
writer.flush();
writer.close();
} catch (IOException ex) {
Logger.getLogger(SQLite.class.getName()).log(Level.SEVERE, null, ex);
}
Upvotes: 1
Views: 2066
Reputation: 1
Here is a complete solution: this executes a query then sends the result sets to a output csv file.
import java.sql.*;
import java.io.IOException;
import java.io.FileWriter;
import java.io.File;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import au.com.bytecode.opencsv.CSVWriter;
public class TableExport {
public static void main(String[] args) {
try{
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE","name","password");
conn.setAutoCommit(false);
Statement statement = conn.createStatement();
ResultSet resultData = statement.executeQuery("select * from your_table");
CSVWriter writer = new CSVWriter(new FileWriter(new File("D:/Uploads/Output5.csv")), '|');
writer.writeAll(resultData, true);
writer.close();
}catch (Exception e){
System.out.println("Error" +e);
}
}
}
please give me comments if you like this code or it helps your purpose.....
Upvotes: 0
Reputation: 5875
If you just say output.csv it should write to the working directory, which is usually the directory you started Java from, but that can vary depending on if you're starting from an IDE.
You can check the actual file it's using though with:
new File("output.csv").getAbsolutePath()
Upvotes: 5