Export Oracle table to CSV using OpenCSV and Resultset - java

I'm trying to export oracle table into csv file and I have created a Class doing so but the output file format was as follow:
12345
1002988846
1
Salary is Here
67891
1009007305
0
Ma3ash is Here!
55555
1095003139
0
Ma3ash is Here!
77777
1023456789
1
Salary is Here
and here is the class:
import java.io.*;
import java.sql.*;
public class newway {
public void mymethod() throws Exception {
try
{
Class.forName("oracle.jdbc.OracleDriver");
Connection conn= DriverManager.getConnection("jdbc:oracle:thin:#172.17.60.225:1521/FRSTEST", "TRASSET", "TRASSET");
conn.setAutoCommit(false);
Statement st=conn.createStatement();
ResultSet rs=st.executeQuery("Select * from Table1");
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())
{
for(int i=1; i<5;i++){
System.out.print(rs.getString(i));
bwOutFile.append(rs.getString(i));
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);
}
}
}
I want the output to be separated by comma and each record in a line.
Any Help Please?!

Where are you using openCSV?
Here is one of the test from CSVWriter in openCSV that tests out writing records from ResultSet. Just modify it to meet your needs.
#Test
public void testResultSetWithHeaders() throws SQLException, IOException {
String[] header = {"Foo", "Bar", "baz"};
String[] value = {"v1", "v2", "v3"};
StringWriter sw = new StringWriter();
CSVWriter csvw = new CSVWriter(sw);
csvw.setResultService(new ResultSetHelperService());
ResultSet rs = MockResultSetBuilder.buildResultSet(header, value, 1);
int linesWritten = csvw.writeAll(rs, true); // don't need a result set since I am mocking the result.
assertFalse(csvw.checkError());
String result = sw.toString();
assertNotNull(result);
assertEquals("\"Foo\",\"Bar\",\"baz\"\n\"v1\",\"v2\",\"v3\"\n", result);
assertEquals(2, linesWritten);
}

Related

importing last row of mysql database in all rows of jtable (Java Swing)

I'm trying to import all of the data from mysql database into a jtable using arraylists but something isn't working right, as i get the number of rows right but they're all values of the last row
Here's the code
public ArrayList<medicaments> medicaments_list() {
ArrayList<medicaments> medicament_lists = new ArrayList<medicaments>();
String select_nom_type_med = "select * from medicaments where login=?";
PreparedStatement stmt;
ResultSet rs2;
medicaments med;
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "");
stmt = con.prepareStatement(select_nom_type_med);
stmt.setString(1, Utilisateur.getLogin());
rs2 = stmt.executeQuery();
while (rs2.next()) {
emptytable = false;
med = new medicaments(rs2.getInt("med_id"), rs2.getString("login"), rs2.getString("med_nom"), rs2.getString("med_type"), rs2.getString("date_debut"), rs2.getString("date_fin"), rs2.getString("frequence"), rs2.getString("temps_1"), rs2.getString("temps_2"), rs2.getString("temps_3"), rs2.getString("temps_4"), rs2.getString("temps_5"), rs2.getString("Stock"), rs2.getString("rappel_stock"));
medicament_lists.add(med);
}
} catch (ClassNotFoundException e1) {
System.out.println(e1);
} catch (SQLException e1) {
System.out.println(e1);
}
return medicament_lists;
}
public void populate_jTable_from_db() {
ArrayList<medicaments> dataarray = medicaments_list();
model = (DefaultTableModel) jTable1.getModel();
model.setRowCount(dataarray.size());
int row = 0;
for (medicaments data : dataarray) {
model.setValueAt(data.get_nom_med(), row, 0);
model.setValueAt(data.get_type_med(), row, 1);
row++;
}
jTable1.setModel(model);
}
and here's the result :(there's 3 rows in my database and po is the last one i added)
Make sure your recordset actually contains something and it's what you really want:
while (rs2.next()) {
if (rs2.getString("login").equals(Utilisateur.getLogin())) {
emptytable = false;
med = new medicaments(rs2.getInt("med_id"), rs2.getString("login"),
rs2.getString("med_nom"), rs2.getString("med_type"),
rs2.getString("date_debut"), rs2.getString("date_fin"),
rs2.getString("frequence"), rs2.getString("temps_1"),
rs2.getString("temps_2"), rs2.getString("temps_3"),
rs2.getString("temps_4"), rs2.getString("temps_5"),
rs2.getString("Stock"), rs2.getString("rappel_stock"));
medicament_lists.add(med);
}
}

Exporting database to csv file with java

so i found this code on the internet, basically what supposedly it can do is backup all the tables from a db, my question is on this line:
res = st.executeQuery("select * from xcms." + tableName);
i get the following excpetion exception: SQLException information
what does xcms. stands for? what else can i put here?
res = st.executeQuery("select * from " + tableName);
btw if i erase xcms. and put it like this ^, i can save only the first table not all the tables, thx
the sourcecode webpage:
https://gauravmutreja.wordpress.com/2011/10/13/exporting-your-database-to-csv-file-in-java/#comment-210
public static void main(String[] args) {
Connection con = null;
String url = "jdbc:mysql://localhost:3306/";
String db = "gg";
String driver = "com.mysql.jdbc.Driver";
String user = "root";
String pass = "";
FileWriter fw;
try {
Class.forName(driver);
con = DriverManager.getConnection(url + db, user, pass);
Statement st = con.createStatement();
ResultSet res = st.executeQuery("SELECT table_name FROM INFORMATION_SCHEMA.TABLES WHERE table_schema = 'gg'");
List<String> tableNameList = new ArrayList<String>();
while (res.next()) {
tableNameList.add(res.getString(1));
}
String filename = "C:\\Users\\Angel Silva\\Documents";
for (String tableName : tableNameList) {
int k = 0;
int j = 1;
System.out.println(tableName);
List<String> columnsNameList = new ArrayList<String>();
res = st.executeQuery("select * from " + tableName);
int columnCount = getColumnCount(res);
try {
fw = new FileWriter("C:\\Users\\Angel Silva\\Documents\\popo1121.csv");
for (int i = 1; i <= columnCount; i++) {
fw.append(res.getMetaData().getColumnName(i));
fw.append(",");
}
fw.append(System.getProperty("line.separator"));
while (res.next()) {
for (int i = 1; i <= columnCount; i++) {
if (res.getObject(i) != null) {
String data = res.getObject(i).toString();
fw.append(data);
fw.append(",");
} else {
String data = "null";
fw.append(data);
fw.append(",");
}
}
fw.append(System.getProperty("line.separator"));
}
fw.flush();
fw.close();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
con.close();
} catch (ClassNotFoundException cnfe) {
System.err.println("Could not load JDBC driver");
cnfe.printStackTrace();
}catch (SQLException sqle) {System.err.println("SQLException information");}
}
public static int getRowCount(ResultSet res) throws SQLException {
res.last();
int numberOfRows = res.getRow();
res.beforeFirst();
return numberOfRows;
}
public static int getColumnCount(ResultSet res) throws SQLException {
return res.getMetaData().getColumnCount();
}
}
This is what I used:
public void sqlToCSV (String query, String filename){
log.info("creating csv file: " + filename);
try {
FileWriter fw = new FileWriter(filename + ".csv");
if(conn.isClosed()) st = conn.createStatement();
ResultSet rs = st.executeQuery(query);
int cols = rs.getMetaData().getColumnCount();
for(int i = 1; i <= cols; i ++){
fw.append(rs.getMetaData().getColumnLabel(i));
if(i < cols) fw.append(',');
else fw.append('\n');
}
while (rs.next()) {
for(int i = 1; i <= cols; i ++){
fw.append(rs.getString(i));
if(i < cols) fw.append(',');
}
fw.append('\n');
}
fw.flush();
fw.close();
log.info("CSV File is created successfully.");
conn.close();
} catch (Exception e) {
log.fatal(e);
}
}
The xms stands for the Database name, in your Connection in the java program you already are establishing the connection to the Database:
(DriverManager.getConnection(url + db, user, pass);
The string db is the name of the DB to connect to.
So no need to have the xms. .just for example use this query:
"SELECT * FROM "+tableName+";"
This is executed in the database you are connected to, for example ggin your code.
For writting the CSV file chillyfacts.com/export-mysql-table-csv-file-using-java/ may help!
SELECT * FROM <MENTION_TABLE_NAME_HERE> INTO OUTFILE <FILE_NAME> FIELDS TERMINATED BY ',';
Example :
SELECT * FROM topic INTO OUTFILE 'D:\5.csv' FIELDS TERMINATED BY ',';
use opencsv dependency to export SQL data to CSV using minimal lines of code.
import com.opencsv.CSVWriter;
import java.io.FileWriter;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class CsvWriter {
public static void main(String[] args) throws Exception {
CSVWriter writer = new CSVWriter(new FileWriter("filename.csv"), '\t');
Boolean includeHeaders = true;
Statement statement = null;
ResultSet myResultSet = null;
Connection connection = null;
try {
connection = //make database connection here
if (connection != null) {
statement = connection.createStatement();
myResultSet = statement.executeQuery("your sql query goes here");
writer.writeAll(myResultSet, includeHeaders);
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}

Reading data in java form MySQL

I'm writing java code that connects to a database in MySQL. I have a connection but I can't get the data to display in a label in my JFrame. So I'm connected to the database software in my XAMPP database but I can't get the data to display in the label
ResultSet rs;
ResultSetMetaData rsmd = null;
int colCount = 0;
String[] colNames = null;
try {
rs = engine.executeQuery("select * from music");
rsmd = rs.getMetaData();
colCount = rsmd.getColumnCount();
colNames = new String[colCount];
for (int i = 1; i <= colCount; i++) {
colNames[i - 1] = rsmd.getColumnName(i);
}
String[] currentRow = new String[colCount];// array to hold the
// row data
while (rs.next()) { // move the rs pointer on to the next record
// (starts before the 1st)
for (int i = 1; i <= colCount; i++) {
currentRow[i - 1] = rs.getString(i);
}
}
//System.out.println(authenticated);
}
catch (SQLException a)
{
System.err.println("SQLException: " + a.getMessage());
}
use this code its working
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.ResultSetMetaData;
public class Db {
public static void main(String arg[]) throws SQLException,
ClassNotFoundException {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection(
"jdbc:mysql://localhost/dbname", "root", "");
java.sql.Statement stmt = conn.createStatement();
String query = "select * from music";
ResultSet rs = stmt.executeQuery(query);
ResultSetMetaData rsmd = rs.getMetaData();
int columncount = rsmd.getColumnCount();
for(int i = 1 ; i < columncount ; i ++)
System.out.print(rsmd.getColumnName(i));
System.out.println();
while (rs.next()) {
for(int i = 1 ; i < columncount ; i ++)
{
System.out.print(rs.getString(i));
}
System.out.println();
}
conn.close();
}
}
Since you want to have separate class for DB connection and use this connection somewhere else you could do something like this.
public class SQLConnect {
public static Connection ConnectDb() {
try {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = null;
try {
conn = DriverManager.getConnection("jdbc:mysql://localhost/databasename", "root", "");
} catch (SQLException ex) {
Logger.getLogger(SQLConnect.class.getName()).log(Level.SEVERE, null, ex);
}
return conn;
}
catch (ClassNotFoundException e) {
System.out.println("not connected");
e.printStackTrace();//print extra info if error is thrown
return null;
}
}
}
And use this class as follows
Connection conn = null;
ResultSet rs = null;
PreparedStatement pst = null;
try {
conn = SQLConnect.ConnectDb();
String sql = "SELECT * FROM somedatabase WHERE somevalue = ? ";
pst = conn.prepareStatement(sql);
pst.setString(1, CriminalID);
rs = pst.executeQuery();
if(rs.next()) {
/*
* set values of labels to those from database
*/
jLabel.setText(rs.getString("Column name"));
}
this will get you data from database where some value is equal to whatever you specify

Writing SQL query result to csv fail : uncomplete line

I am trying to write some query results in a csv file.
I thought it was working fine, until I saw the last line of the file after it's been written:
value;nettingNodeData;BLE57385;CVAEngine;BLE;;.4;;BDR;NA;ICE;;RDC;;CVAEngine;;4841263;RDC
value;ne
The part where I am writing the file :
public int getLeNodes(String runId, File file) {
Connection connect = null;
PreparedStatement ps = null;
ResultSet rs = null;
int lines = 0;
try {
connect = newConnection();
ps = connect.prepareStatement(HIER_NTT.replace("?", runId));
ps.setFetchSize(1500);
rs = ps.executeQuery();
lines += nnpw.writeCore(file, rs);
} catch (Exception e) {
e.printStackTrace();
} finally {
close(rs, ps, connect);
}
return lines;
}
public int writeCore(File file, ResultSet rs) throws FileNotFoundException, IOException {
int count = 0;
try {
BufferedWriter output = new BufferedWriter(new FileWriter(file, true));
ResultSetMetaData rsmd = rs.getMetaData();
int colCount = rsmd.getColumnCount();
while (rs.next()) {
for (int col = 1; col <= colCount; col++) {
try {
String val = rs.getString(col);
if (rs.wasNull()) {
val = "";
}
output.append(val);
} catch (ArrayIndexOutOfBoundsException e) {
String dec = rs.getBigDecimal(col).toPlainString();
System.err.println(String.format("%s %d %s %s %d %s %d", rs.getString(1), col,
rsmd.getColumnTypeName(col), file, count, dec, dec.length()));
output.append(dec);
}
if (col < colCount) {
output.append(";");
}
}
output.newLine();
count++;
}
} catch (Exception e) {
e.printStackTrace();
}
return count;
}
And the SQL (HIER_NTT):
select 'value', 'nettingNodeData', 'BLE' || d.deal_numadm,
'CVAEngine', 'BLE', '', (1-ntt.lgd_cp), '', 'BDR', 'NA', 'ICE', '', 'RDC', '',
'CVAEngine', '', d.ntt_id, 'RDC' from ntt ntt
join deals d on d.ntt_id = ntt.ntt_id and d.deal_scope='Y'
join dt_runs r on r.run_id = ntt.run_id
where r.run_id=? and d.deal_numadm!=0 group by d.deal_numadm, d.deal_nummas, d.ntt_id, ntt.lgd_cp
So, why does my file end abruptly like this?
You should call output.close() when you are done writing to the file. The missing output is probably still in the buffer when the java process exits

changing Set<String> values from database to string values

How do i make my program read a Set value from the database one word by one word.
import java.util.*;
import java.sql.*;
public class SetProblem {
public static void main(String[] args) {
Connection con = null;
Statement st = null;
ResultSet rs = null;
Set<String> nums = new HashSet<>();
nums.add("1");
nums.add("2");
try
{
Class.forName("org.sqlite.JDBC");
con = DriverManager.getConnection("jdbc:sqlite:src/database/set.db");
st = con.createStatement();
//st.execute("create table data(word, synonyms);");
//st.executeUpdate("insert into data values('figure', '"+nums+"');");
rs = st.executeQuery("select * from data;");
Set<String> set = new TreeSet<>();
while(rs.next())
{
set.add(rs.getString(2));
}
for(String s:set)
{
System.out.print(s + "");
}
}
catch(ClassNotFoundException e)
{
System.out.println("Driver not found");
}
catch(SQLException s)
{
System.out.println("wrong sql command");
}
}
}
my problem is that it prints [1, 2] instead of
1
2
which i want. How can i achieve this?
You're inserting one row, but what you want (I think) is to insert two rows. You see [1, 2] because it is exactly what you're inserting in the second column (you're forcing a nums.toString()).
Here the code I think you were looking for:
//...
Set<String> nums = new HashSet<>();
nums.add("1");
nums.add("2");
try
{
Class.forName("org.sqlite.JDBC");
con = DriverManager.getConnection("jdbc:sqlite:src/database/set.db");
st = con.createStatement();
//st.execute("create table data(word, synonyms);");
//for(String i: nums) //HERE YOU NEED TO CYCLE
// st.executeUpdate("insert into data values('figure', '"+i+"');");
rs = st.executeQuery("select * from data;");
Set<String> set = new TreeSet<>();
while(rs.next())
{
set.add(rs.getString(2));
}
for(String s:set)
{
System.out.print(s + "");
}
}
//...

Categories