Exporting database to csv file with java - 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();
}
}
}

Related

get results as CSV in a loop

I want to print the result set obtained from JDBC connection and want it to print each result set in aloop.So my code is below:
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.sql.*;
import java.util.*;
public class zone {
List<model> listOfBlogs;
public zone() {
Connection conn = null;
listOfBlogs = new ArrayList<model>();
// connect to the database
conn = connectToDatabaseOrDie();
// get the data
populateListOfTopics(conn, listOfBlogs);
//print the data
printTopics(listOfBlogs);
}
public List<model> printTopics(List<model> listOfBlogs) {
Iterator<model> it = listOfBlogs.iterator();
while (it.hasNext()) {
model blog = (model) it.next();
System.out.println(blog.day + " " + blog.hour + " " + blog.zone_id);
}
return listOfBlogs;
}
private void populateListOfTopics(Connection conn, List<model> listOfBlogs) {
try {
for (int j = 9; j < 10; j++) {
for (int i = 10; i < 12; i++) {
String sql = "SELECT day,hour,zone_id FROM public.zone_block_table where day=? and hour=?";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, j);
pstmt.setInt(2, i);
ResultSet rs = pstmt.executeQuery();
while (rs.next()) {
model blog = new model();
blog.day = rs.getInt("day");
blog.hour = rs.getInt("hour");
blog.zone_id = rs.getInt("zone_id");
listOfBlogs.add(blog);
}
convertToCsv(rs, i, j);
rs.close();
pstmt.close();
}
}
} catch (SQLException se) {
System.err.println("Threw a SQLException creating the list of state.");
System.err.println(se.getMessage());
} catch (Exception e) {
System.out.println("Err");
e.printStackTrace();
}
}
private Connection connectToDatabaseOrDie() {
Connection conn = null;
try {
Class.forName("org.postgresql.Driver");
String url = "jdbc:postgresql://localhost:5432/mvc_data_base";
conn = DriverManager.getConnection(url, "postgres", "122333");
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
public void convertToCsv(ResultSet rs, int k, int l) throws SQLException, FileNotFoundException {
PrintWriter csvWriter = new PrintWriter(new File("state_" + k + "_" + l + ".csv"));
ResultSetMetaData meta = rs.getMetaData();
int numberOfColumns = meta.getColumnCount();
String dataHeaders = "\"" + meta.getColumnName(1) + "\"";
for (int i = 2; i < numberOfColumns + 1; i++) {
dataHeaders += ",\"" + meta.getColumnName(i) + "\"";
}
csvWriter.println(dataHeaders);
while (rs.next()) {
String row = "\"" + rs.getString(1) + "\"";
for (int i = 2; i < numberOfColumns + 1; i++) {
row += ",\"" + rs.getString(i) + "\"";
}
csvWriter.println(row);
}
csvWriter.close();
}
}
But the resultset is printing perfectly.But I could not find any csv files generated in any of the destination.Any help is appreciated.
There is a Direct method to create CSV file from resultset using opencsv. Try that
CSVWriter writer = new CSVWriter(new FileWriter(csvtemp), ','
,CSVWriter.NO_QUOTE_CHARACTER , CSVWriter.NO_ESCAPE_CHARACTER , "\r\n");
writer.writeAll(myResultSet, true);
writer.close();

Export Oracle table to CSV using OpenCSV and Resultset

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

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

Getting empty resultSet for simple query

gurus,
I am new to Java SQL, and need some help.
I'm trying to get a parameter from MS SQL Server 2008. The data is definitely there - it is a current and valid DB, and I'm trying to use the users records to get cridentials for another application.
I asserted the following query:
String query = "SELECT [USER].qc_number FROM [USER] WHERE "[USER].login_name = '"
+ userNameInput + "' AND [USER].password = '" + passWordInput + "';";
Where userNameInput and passWordInput are received from the user. The URL, query and driver class are definitely correct: I checked the DB schema both from the application and from the server views. Furthermore, I verified all the Exceptions systems by changing parameters one by one, resulting in correct Exceptions messages. However, I get a resultSet with 1 column and 0 rows.
The code is below:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class trOdbc
{// database URL
final String DB_URL = "***";
final String Class_URL = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
private Connection connection = null; // manages connection
private Statement statement = null; // query statement
private ResultSet resultSet = null; // manages results
private Boolean connectedToDatabase = false;
// ----------------------------------------------------------
public void createJdbcConnection()
{ // connect to database books and query database
if (connectedToDatabase)
{ return; }
try
{ // connectedToDatabase is false - establish the connection
Class.forName(Class_URL);
connection = DriverManager.getConnection
(DB_URL, "***", "***" );
statement = connection.createStatement
(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
connectedToDatabase = true;
}
catch (SQLException ex)
{ System.out.println ("SQL Exception in connection establishment: " + ex); }
catch (ClassNotFoundException ex)
{ System.out.println ("Class not found exception in query process: " + ex); }
}
// ----------------------------------------------------------
public String [][] processJdbcQuery (String query)
{
createJdbcConnection ();
if (!connectedToDatabase)
{ return null; }// the connection wasn't established
try
{// query database
resultSet = statement.executeQuery(query);
int columns = resultSet.getMetaData().getColumnCount();
int rows = 0;
if (resultSet != null)
{
resultSet.beforeFirst();
resultSet.last();
rows = resultSet.getRow();
}
String [][] tempData = new String[rows][columns];
resultSet.beforeFirst();
rows = 0;
while (resultSet.next())
{
for (int x = 1; x <= columns; x++)
{
tempData [rows][x - 1] = resultSet.getString (x);
}
rows++;
}
CloseJdbcConnection ();
return tempData;
}
catch (SQLException ex)
{
System.out.println ("SQL Exception in query process: " + ex);
CloseJdbcConnection ();
return null;
}
} // end processJdbcQuery
// ----------------------------------------------------------
public void CloseJdbcConnection()
{
if ( connectedToDatabase )
{// close Statement and Connection. resultSet is closed automatically.
try
{
statement.close();
connection.close();
connectedToDatabase = false;
}
catch (SQLException ex)
{ System.out.println ("SQL Exception in connection closure: " + ex); }
} // end if
} // end method CloseJdbcConnection
} // end class trOdbc
Why don't you use Prepared Statement instead ?
Here is a good tutorial for using prepared statement in java
In your case it would be :
String query = "SELECT [USER].qc_number FROM [USER] " +
"WHERE [USER].login_name = ? AND [USER].password = ?;";
And then set it with different values each time you execute it like :
PreparedStatement ps = connection.prepareStatement(query);
ps.setString(1, userNameInput);
ps.setString(2, passWordInput);
resultSet = ps.executeQuery();

Categories