Continuously getting content from table database - java

I have a code here:
package testcode;
import java.sql.*;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;
import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;
public class ProducerClear2 {
public static String vardbserver;
public static String vardbuser;
public static String vardbpassword;
public static String vardbname;
public static void main(String[] args) {
vardbserver = "TestDBtoMQ";
vardbuser = "postgresql";
vardbpassword = "admin";
ConnectionFactory factory = null;
javax.jms.Connection connection = null;
Session session = null;
Destination destination = null;
MessageProducer producer = null;
try {
factory = new ActiveMQConnectionFactory(ActiveMQConnection.DEFAULT_BROKER_URL);
connection = factory.createConnection();
connection.start();
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
destination = session.createQueue("TestQueue");
producer = session.createProducer(destination);
Class.forName("org.postgresql.Driver");
System.out.println("----------------------------");
try (Connection c = DriverManager.getConnection("jdbc:postgresql://localhost:5432/" + vardbserver, vardbuser, vardbpassword);
PreparedStatement stmt = c.prepareStatement("SELECT * FROM MESSAGES where xmin::varchar::bigint > ? and xmin::varchar::bigint < ? ");
PreparedStatement max = c.prepareStatement("select max(xmin::varchar::bigint) as txid from messages")
) {
c.setAutoCommit(false);
Long previousTxId = 0L;
Long nextTxId = 0L;
while (true) {
stmt.clearParameters();
try (ResultSet rs = max.executeQuery()) {
if (rs.next()) {
nextTxId = rs.getLong(1);
}
}
stmt.setLong(1, previousTxId);
stmt.setLong(2, nextTxId + 1);
try (ResultSet rs = stmt.executeQuery()) {
while (rs.next()) {
String message = rs.getString("MESSAGE");
System.out.println("Message = " + message);
TextMessage mssg = session.createTextMessage(message);
System.out.println("Sent: " + mssg.getText());
producer.send(mssg);
}
previousTxId = nextTxId;
}
Thread.sleep(5000);
}
}
} catch (JMSException e) {
e.printStackTrace();
} catch (Exception e) {
System.err.println(e.getClass().getName() + ": " + e.getMessage());
} finally {
if (session != null) {
try {
session.close();
} catch (JMSException ex) {
// ignore
}
}
if (connection != null) {
try {
connection.close();
} catch (JMSException ex) {
// ignore
}
}
}
System.out.println("----------------------------");
System.out.println("Message sent successfully");
}
}
Basically, the app works to get contents inside a database's table and sent it to ActiveMQ. And when the table updated, it will sent the content that just updated (not sending the past that was sent). But this code only works on PostgreSQL
Then i'm planning to create an "if" function. So i can use another database to getting the data (Oracle and MySQL).
Is the xmin still works for Oracle and MySQL? So i just need to change the server URL? Or i need to change the code for Oracle and MySQL?

Simply found the answer, using limit, save it's every limit row to a file and use a date for every daily file....
if(vardbtype.equals("MYSQL")){
Class.forName("com.mysql.jdbc.Driver");
System.out.println("----------------------------");
int limitrowmysql = 0;
LocalDate now = LocalDate.now();
Path path = FileSystems.getDefault().getPath("C:\\Users\\NN\\Documents\\Test\\RowMYSQL\\RowIDMYSQL_" + now.format(DateTimeFormatter.ISO_LOCAL_DATE) + ".txt");
if (Files.exists(path)) {
String latestRowIdFromFile = Files.lines(path).max((e1, e2) -> {
if (((String)e1).isEmpty() || ((String)e2).isEmpty()) {
return -1;
}
return new Long(e1).compareTo(new Long(e2));
}).get(); // read latestRowId from file
if (latestRowIdFromFile != null && !latestRowIdFromFile.isEmpty()) {
limitrowmysql = Integer.valueOf(latestRowIdFromFile);
}
} else {
limitrowmysql = 0;
}
Connection c = DriverManager.getConnection("jdbc:mysql://localhost:3306/"+ vardbserver, vardbuser, vardbpassword);
while(true) {
Statement stmts = c.createStatement();
int countrowmysql = 0;
String sql = ("SELECT * FROM "+ vardbname +" LIMIT "+ limitrowmysql +", 18446744073709551615");
ResultSet rss = stmts.executeQuery(sql);
while(rss.next()) {
String message = rss.getString("MESSAGE");
System.out.println("Message = " + message);
TextMessage mssg = session.createTextMessage(message);
System.out.println("Sent: " + mssg.getText());
producer.send(mssg);
countrowmysql = countrowmysql + 1;
}
rss.close();
stmts.close();
Thread.sleep(batchperiod2);
limitrowmysql = limitrowmysql + countrowmysql;
Files.write(path, ("\n" + limitrowmysql).getBytes()); // write latestRowId to file
}
}

Related

Where is the error in my Java code to connect to a MySQL database?

I am currently programming a Plugin for Minecraft (Spigot). For this Plugin, I need to connect to a MySQL database.
But, within the logs, there is an error in line 35.
[line 35]: prop.setProperty("requireSSL", pbp.getConfigManager().getString("MySQl.ssl"));
thats my DataBaseManager class:
package de._dagobert_duck.pbp.SQL;
import java.sql.ResultSet;
import java.sql.DatabaseMetaData;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.DriverManager;
import java.util.Properties;
import de._dagobert_duck.pbp.PiggyBankPlus;
import java.sql.Connection;
public class DataBaseManagerSQL
{
private Connection connection;
private static final PiggyBankPlus pbp = PiggyBankPlus.getMain();
public DataBaseManagerSQL() {
this.connection = null;
this.connectToDataBase();
this.setupDatabase();
}
private void connectToDataBase() {
pbp.getLogger().info("Connecting to database ...");
try {
Class.forName("com.mysql.jdbc.Driver");
Properties prop = new Properties();
prop.setProperty("user", pbp.getConfigManager().getString("MySQL.username"));
prop.setProperty("password", pbp.getConfigManager().getString("MySQL.password"));
prop.setProperty("autoReconnect", "true");
prop.setProperty("verifyServerCertificate", "false");
/*line35*/prop.setProperty("useSSL", pbp.getConfigManager().getString("MySQL.ssl"));
prop.setProperty("requireSSL", pbp.getConfigManager().getString("MySQl.ssl"));
this.connection = DriverManager.getConnection("jdbc:mysql://" + pbp.getConfigManager().getString("MySQL.host") + ":" + pbp.getConfigManager().getString("MySQL.port") + "/" + pbp.getConfigManager().getString("MySQL.dataBaseName"), prop);
pbp.getLogger().info("Connected to MySQL database successfully. :D");
}
catch (ClassNotFoundException e) {
pbp.getLogger().severe("Unable to locate the drivers for MySQL. Err: " + e.getMessage());
return;
}
catch (SQLException f) {
pbp.getLogger().severe("Unable to connect to MySQL. Err: " + f.getMessage());
return;
}
}
public boolean setupDatabase() {
PreparedStatement query = null;
label71: {
try {
final String data = "CREATE TABLE IF NOT EXISTS `" + pbp.getConfigManager().getString("MySQL.tableName` (id int(10) AUTO_INCREMENT, player_uuid varchar(50) NOT NULL UNIQUE, player_name varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL, money double(30, 2) NOT NULL, last_seen varchar(30) NOT NULL, sync_complete varchar(5) NOT NULL, PRIMARY KEY(id)); ");
query = this.connection.prepareStatement(data);
query.execute();
break label71;
} catch (SQLException g) {
g.printStackTrace();
try {
if (query != null) {
query.close();
}
} catch (Exception h) {
h.printStackTrace();
}
} finally {
try {
if (query != null) {
query.close();
}
} catch (Exception h2) {
h2.printStackTrace();
}
}
return false;
}
this.updateTables();
return true;
}
public Connection getConnection() {
this.checkConnection();
return this.connection;
}
public boolean checkConnection() {
try {
if (this.connection == null) {
pbp.getLogger().warning("Connection failed! Trying to reconnect ...");
return this.reConnect();
}
else if (!this.connection.isValid(3)) {
pbp.getLogger().warning("Connection to database is failed or terminated! Trying to reconnect ... ");
return this.reConnect();
}
else if (this.connection.isClosed()) {
pbp.getLogger().warning("[PiggyBankPlus] Connection to database is closed! Trying to reconnect ...");
return this.reConnect();
}
else
{return true;}
}
catch (Exception i) {
pbp.getLogger().warning("[PiggyBankPlus] Could not reconnect to Database!");
return true;
}
}
public boolean reConnect() {
try {
long start = 0L;
long end = 0L;
start = System.currentTimeMillis();
Class.forName("com.mysql.jdbc.Driver");
final Properties prop = new Properties();
prop.setProperty("user", pbp.getConfigManager().getString("MySQL.username"));
prop.setProperty("password", pbp.getConfigManager().getString("MySQL.password"));
prop.setProperty("autoReconnect", "true");
prop.setProperty("verifyServerCertificate", "false");
prop.setProperty("useSSL", pbp.getConfigManager().getString("MySQL.ssl"));
prop.setProperty("requireSSL", pbp.getConfigManager().getString("MySQL.ssl"));
this.connection = DriverManager.getConnection("jdbc:mysql://" + pbp.getConfigManager().getString("MySQL.host") + ":" + pbp.getConfigManager().getString("MySQL.port") + "/" + pbp.getConfigManager().getString("MySQL.dataBaseName"), prop);
end = System.currentTimeMillis();
final long time = end - start;
pbp.getLogger().info(" successfully reconnected to MySQL server. Time: " + time + " ms");
return true;
}
catch (Exception j) {
pbp.getLogger().warning("[PiggyBankPlus] Reconnection to MySQL failed! Err: " + j.getMessage());
return false;
}
}
public boolean closeDatabase() {
try {
this.connection.close();
this.connection = null;
return true;
}
catch (SQLException k) {
k.printStackTrace();
return false;
}
}
private void updateTables() {
if (this.connection != null) {
DatabaseMetaData dbmd = null;
ResultSet rs1 = null;
ResultSet rs2 = null;
ResultSet rs3 = null;
PreparedStatement query1 = null;
PreparedStatement query2 = null;
PreparedStatement query3 = null;
try {
dbmd = this.connection.getMetaData();
rs1 = dbmd.getColumns(null, null, pbp.getConfigManager().getString("MySQL.tableName"), "sync_complete");
if (!rs1.next()) {
final String data = "ALTER TABLE `" + pbp.getConfigManager().getString("MySQL.tableName") + "` ADD sync_complete varchar(5) NOT NULL DEFAULT 'true';";
query1 = this.connection.prepareStatement(data);
query1.execute();
}
rs2 = dbmd.getColumns(null, null, pbp.getConfigManager().getString("MySQL.tableName"), "player_name");
if (!rs2.next()) {
final String data = "ALTER TABLE `" + pbp.getConfigManager().getString("MySQL.tableName") + "` ADD player_name varchar(50) NOT NULL DEFAULT 'true';";
query2 = this.connection.prepareStatement(data);
query2.execute();
}
rs3 = dbmd.getColumns(null, null, pbp.getConfigManager().getString("MySQL.tableName"), "last_seen");
if (!rs3.next()) {
final String data = "ALTER TABLE `" + pbp.getConfigManager().getString("MySQL.tableName") + "` ADD last_seen varchar(30) NOT NULL DEFAULT 'true';";
query3 = this.connection.prepareStatement(data);
query3.execute();
}
}
catch (Exception l) {
pbp.getLogger().warning(" Error while updating inventory table! Err: " + l.getMessage());
l.printStackTrace();
}
finally {
try {
if (query1 != null) {
query1.close();
}
if (query2 != null) {
query2.close();
}
if (query3 != null) {
query3.close();
}
if (rs1 != null) {
rs1.close();
}
if (rs2 != null) {
rs2.close();
}
if (rs3 != null) {
rs3.close();
}
}
catch (Exception i) {
i.printStackTrace();
}
}
}
}
}
Can someone help me to find the error?
Thank you,
Scrooge McDuck

Java Server MySQL Update won't work

I recently started a big server application in java.
The problem is, that my user-update function usr.updUser(...) is not working.
First here are the files.
Server.java:
import java.net.ServerSocket;
import java.net.Socket;
import java.io.IOException;
import java.io.DataInputStream;
import java.io.DataOutputStream;
public class Server extends Thread {
private ServerSocket srvSock;
SQLdb db;
User usr;
public Server(int port, SQLdb db) throws IOException{
srvSock = new ServerSocket(port);
this.db = db;
}
public void run(){
while(true){
String ret = "";
try{
Socket sock = srvSock.accept();
usr = new User(db);
DataInputStream in = new DataInputStream(sock.getInputStream());
try{
String[] cmd = in.readUTF().split(":");
switch(cmd[0]){
case "user":{
switch(cmd[1]){
case "addUser":{
ret = usr.addUser(cmd[2]);
break;
}
case "getUser":{
ret = usr.getUser(cmd[2]);
break;
}
case "updUser":{
ret = usr.updUser(cmd[2]);
break;
}
case "delUser":{
ret = usr.delUser(cmd[2]);
break;
}
}
break;
}
}
}catch(Exception e){
e.printStackTrace();
}
DataOutputStream out = new DataOutputStream(sock.getOutputStream());
out.writeUTF(ret);
sock.close();
}catch(IOException e){
e.printStackTrace();
break;
}
}
}
public static void main(String[] args){
SQLdb db = new SQLdb("data.db");
User usr = new User(db);
if(args[0] == "new")
usr.setupTable();
usr.updUser("age=6;uid=2"));
try{
Thread server = new Server(5000, db);
server.start();
}catch(IOException e){
e.printStackTrace();
}
Thread ti = new TermInput(db);
ti.start();
Thread time = new Time(usr);
time.start();
}
}
User.java:
import java.sql.SQLException;
public class User {
SQLdb db;
String report;
public User(SQLdb db){
this.db = db;
}
public String setupTable(){
if(
db.addTable("users",
"uid INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, " +
"uname TEXT UNIQUE NOT NULL, " +
"pwd TEXT NOT NULL, " +
"mail TEXT UNIQUE NOT NULL, " +
"age INTEGER NOT NULL, " +
"rank INTEGER DEFAULT 0 , " +
"coins INTEGER DEFAULT 100 , " +
"prem INT DEFAULT 0 , " +
"setg TEXT")
)
report = "ok";
else
report = db.report;
return report;
}
public String addUser(String cmd){
try{
String[] _cmd = cmd.split(";");
if(db.addEntry("users", new String[] {"uname, pwd, mail, age",
"'" + _cmd[0] + "', '" + _cmd[1] + "', '" + _cmd[2] + "', " + _cmd[3]}))
report = "ok";
else
report = db.report;
}catch(Exception e){
report = e.getMessage();
}
return report;
}
public String getUser(String cmd){
try{
String[] _cmd = cmd.split(";");
if(db.getEntry("users", new String[] {_cmd[0], _cmd[1]})){
try{
report = "";
while(db.res.next()){
report += Integer.toString(db.res.getInt("uid")) + ";";
report += db.res.getString("uname") + ";";
report += db.res.getString("pwd") + ";";
report += db.res.getString("mail") + ";";
report += Integer.toString(db.res.getInt("age")) + ";";
report += Integer.toString(db.res.getInt("rank")) + ";";
report += Integer.toString(db.res.getInt("coins")) + ";";
report += Integer.toString(db.res.getInt("prem")) + ";";
report += db.res.getString("setg");
report += "-";
}
db.res.close();
db.stmt.close();
}catch(SQLException e){
report = e.getMessage();
}
}
}catch(Exception e){
report = e.getMessage();
}
return report;
}
public String updUser(String cmd){
try{
String[] _cmd = cmd.split(";");
if(db.updEntry("users", new String[] {_cmd[0], _cmd[1]}))
report = "ok";
else
report = db.report;
}catch(Exception e){
report = e.getMessage();
}
return report;
}
public String delUser(String cmd){
try{
if(db.delEntry("users", cmd))
report = "ok";
else
report = db.report;
}catch(Exception e){
report = e.getMessage();
}
return report;
}
}
SQLdb.java:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class SQLdb {
Connection conn;
Statement stmt;
String report;
ResultSet res;
public SQLdb(String db){
try{
Class.forName("org.sqlite.JDBC");
conn = DriverManager.getConnection("jdbc:sqlite:" + db);
report = "";
}catch(Exception e){
report = e.getMessage();
}
}
public boolean addTable(String table, String struct){
try{
stmt = conn.createStatement();
stmt.executeUpdate("CREATE TABLE " + table + " (" + struct + ");");
stmt.close();
report = "";
return true;
}catch(SQLException e){
report = e.getMessage();
return false;
}
}
public boolean resTable(String table){
try{
stmt = conn.createStatement();
stmt.executeUpdate("DELETE FROM " + table + ";");
stmt.close();
report = "";
return true;
}catch(SQLException e){
report = e.getMessage();
return false;
}
}
public boolean delTable(String table){
try{
stmt = conn.createStatement();
stmt.executeUpdate("DROP TABLE " + table + ";");
stmt.close();
report = "";
return true;
}catch(SQLException e){
report = e.getMessage();
return false;
}
}
public boolean addEntry(String table, String[] entry){
try{
stmt = conn.createStatement();
stmt.executeUpdate("INSERT INTO " + table + "(" + entry[0] + ") VALUES (" + entry[1] + ");");
stmt.close();
report = "";
return true;
}catch(SQLException e){
report = e.getMessage();
return false;
}
}
public boolean getEntry(String table, String[] ident){
try{
stmt = conn.createStatement();
String exec = "SELECT " + ident[0] + " FROM " + table;
if(ident[1].equals("-")){
exec += ";";
}else{
exec += " WHERE (" + ident[1] + ");";
}
res = stmt.executeQuery(exec);
report = "";
return true;
}catch(SQLException e){
report = e.getMessage();
return false;
}
}
public boolean updEntry(String table, String[] data){
try{
stmt = conn.createStatement();
stmt.executeUpdate("UPDATE " + table + " set " + data[1] + " WHERE " + data[0] + ";");
stmt.close();
report = "";
return true;
}catch(SQLException e){
report = e.getMessage();
return false;
}
}
public boolean delEntry(String table, String entry){
try{
stmt = conn.createStatement();
stmt.executeUpdate("DELETE FROM " + table + " WHERE (" + entry + ");");
stmt.close();
report = "";
return true;
}catch(SQLException e){
report = e.getMessage();
return false;
}
}
}
TermInput.java:
import java.util.Scanner;
import java.sql.SQLException;
public class TermInput extends Thread {
Scanner s;
SQLdb sql;
public TermInput(SQLdb sql){
s = new Scanner(System.in);
this.sql = sql;
}
public void run(){
System.out.println("\nUse 'help' to get a list of available commands!\n");
boolean exit = false;
while(true){
System.out.print(">> ");
switch(s.next()){
case "help":
case "h":
case "?":{
System.out.println(
" [h]elp/[?]\tList all commands\n" +
" [s]hutdown\tShutdown the server"
);
break;
}
case "shutdown":
case "s":{
exit = true;
break;
}
case "time":{
System.out.println(Time.currentTime());
break;
}
default:{
System.out.println(
" Unknown command!\n" +
" Use 'help' to get a list of available commands!"
);
break;
}
}
if(exit) break;
}
try{
this.sql.conn.close();
}catch(SQLException e){
e.printStackTrace();
}
System.exit(0);
}
}
Time.java:
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.sql.SQLException;
public class Time extends Thread{
User usr;
public Time(User usr){
this.usr = usr;
}
public void run(){
while(true){
checkTime();
}
}
private void checkTime(){
String ret = "";
ret = usr.getUser("*;-");
String[] prem = ret.split("-");
for(int x = 0; x < prem.length; x++)
System.out.println(prem[x].split(";")[7]);
}
public static int currentTime() {
int d = 0;
SimpleDateFormat s;
Calendar c = Calendar.getInstance();
s = new SimpleDateFormat("DDD");
d = (Integer.parseInt(s.format(c.getTime()))-1)*24*60*60;
s = new SimpleDateFormat("HH");
d += (Integer.parseInt(s.format(c.getTime()))-1)*60*60;
s = new SimpleDateFormat("mm");
d += (Integer.parseInt(s.format(c.getTime()))-1)*60;
s = new SimpleDateFormat("ss");
d += Integer.parseInt(s.format(c.getTime()));
return d;
}
}
You can see at Line 76 of Server.java that i want to change the Age of the User with id 2 to 6 years.
But when i make a getUser request, it isnt changed (see my time endless call function).
So why is the user isnt updated via my function?

Deferencing Null pointer

public class Model
{
public static Connection getConnection()
{
Connection conn = null;
try
{
Class.forName("oracle.jdbc.OracleDriver");
conn = DriverManager.getConnection("jdbc:oracle:thin:#localhost:1521:xe", "System", "system");
}
catch(ClassNotFoundException e)
{
e.printStackTrace();
}
catch(SQLException e)
{
e.printStackTrace();
}
return conn;
}
public static class Cart
{
public String itmName="";
public int howmany=0;
public static long itmQty=0, itmID=0;
public double itmPrice=0.0, itmCost=0.0, totalSum=0.0;
}
public static ArrayList<Cart> getCartDatabase(String user) throws Exception
{
Connection conn = getConnection();
String sql = "select * from userCarts where userID = '" + user + "'";
PreparedStatement pstmt = conn.prepareStatement(sql);
ResultSet rst = pstmt.executeQuery();
ArrayList<Cart> al = null;
Cart crt=null;
while(rst.next())
{
System.out.println("CPoint");
try
{
long p = rst.getLong("itemID");
crt.itmID = p; // This is the line thats creating the error
System.out.println(p + " is long! I guess...");
}
catch(NullPointerException e)
{
System.out.println("NPE Caught in Model");
}
System.out.println("CP 1 " + crt.itmID);
ArrayList<row> alr=null;
try
{
alr = Model.getStoreInventory();
}
catch(Exception e)
{
e.printStackTrace();
}
System.out.println("CP 2");
for(int i=0; i<alr.size(); i++)
{
crt.itmName = alr.get(i).itmName;
crt.itmPrice = alr.get(i).itmPrice;
crt.itmQty = alr.get(i).itmQty;
}
System.out.println("CP 3");
crt.howmany = rst.getInt("howmany");
crt.itmCost = crt.itmPrice*crt.howmany;
al.add(crt);
}
return al;
}
}
When I try to access this method of getCartFromDatabase, it gives a NullPointerException however I don't understand why it would do this. Moreover, I tried to make the class as a non static class too, but still it gave the same error:
"Possible deferencing Null Pointer"
Cart crt=null;
while(rst.next())
{
System.out.println("CPoint");
try
{
long p = rst.getLong("itemID");
crt.itmID = p; // This is the line thats creating the error
System.out.println(p + " is long! I guess...");
}
crt is null when you try to access crt.itemID. You have to assign it an instance first.
I think you may simply change the first line from the snippet to
Cart crt = new Cart();

Using profiling tool to find the exact reason for outOfMemoryError in the java code

Hi i have a java program for accessing remote as well as local databases at the same time using JDBC.
But i am getting this exception:
->Exception in thread "Thread-1964" java.lang.OutOfMemoryError: Java heap space
Now i wanted to use any profiling tool through whic i can get exact reason for this exception in my code.
This is my java program
public class DBTestCases{
Connection localConnection;
Connection remoteConnection;
Connection localCon;
Connection remoteCon;
List<Connection> connectionsList;
String driver = "com.mysql.jdbc.Driver";
String user = "root";
String password = "root";
String dbName = "myDB";
String connectionUrl1= "jdbc:mysql://11.232.33:3306/"+dbName+"?user="+user+"&password="+password+"&useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&failOverReadOnly=false&maxReconnects=10";
String connectionUrl2= "jdbc:mysql://localhost:3306/"+dbName+"?user="+user+"&password="+password+"&useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&failOverReadOnly=false&maxReconnects=10";
public List<Connection> createConnection() {
try {
Class.forName(driver);
localCon = DriverManager.getConnection(connectionUrl2);
if(localCon != null)
System.out.println("connected to remote database at : "+new Date());
remoteCon = DriverManager.getConnection(connectionUrl1);
if(remoteCon != null)
System.out.println("connected to local database at : "+new Date());
connectionsList = new ArrayList<Connection>( 2 );
connectionsList.add( 0 , localCon );
connectionsList.add( 1 , remoteCon );
} catch(ClassNotFoundException cnfe) {
cnfe.printStackTrace();
} catch(SQLException sqle) {
sqle.printStackTrace();
}
return connectionsList;
}
public void insert(){
Runnable runnable = new Runnable(){
public void run() {
PreparedStatement ps1 = null;
PreparedStatement ps2 = null;
String sql = "insert into user1(name, address, created_date)" +
" values('johnsan', 'usa', '2013-08-04')";
if(remoteConnection != null&&localConnection != null) {
System.out.println("Database Connection Is Established");
try {
ps1 = remoteConnection.prepareStatement(sql);
ps2 = localConnection.prepareStatement(sql);
int i = ps1.executeUpdate();
int k = ps2.executeUpdate();
if(i > 0) {
System.out.println("Data Inserted into remote database table Successfully");
}
if(k > 0) {
System.out.println("Data Inserted into local database table Successfully");
}
} catch (SQLException s) {
System.out.println("SQL code does not execute.");
s.printStackTrace();
}catch (Exception e) {
e.printStackTrace();
}
}
System.out.println("Inserting values in db");
}
};
Thread thread = new Thread(runnable);
thread.start();
}
public void retrieve(){
Runnable runnable = new Runnable(){
public void run() {
try {
Statement st1 = localConnection.createStatement();
Statement st2 = remoteConnection.createStatement();
ResultSet res1 = st1.executeQuery("SELECT * FROM user1");
ResultSet res2 = st2.executeQuery("SELECT * FROM user1");
System.out.println("---------------------------Local Database------------------------");
while (res1.next()) {
Long i = res1.getLong("userId");
String s1 = res1.getString("name");
String s2 = res1.getString("address");
java.sql.Date d = res1.getDate("created_date");
System.out.println(i + "\t\t" + s1 + "\t\t" + s2 + "\t\t"+ d);
}
System.out.println("------------------------Remote Database---------------------");
while (res2.next()) {
Long i = res2.getLong("userId");
String s1 = res2.getString("name");
String s2 = res2.getString("address");
java.sql.Date d = res2.getDate("created_date");
System.out.println(i + "\t\t" + s1 + "\t\t" + s2 + "\t\t"+ d);
}
} catch (SQLException s) {
System.out.println("SQL code does not execute.");
s.printStackTrace();
}catch (Exception e) {
e.printStackTrace();
}
}
};
Thread thread = new Thread(runnable);
thread.start();
}
public static void main(String[] args) {
DBTestCases dbTestCases = new DBTestCases();
List l = dbTestCases.createConnection();
dbTestCases.localConnection = (Connection)l.get(0);
dbTestCases.remoteConnection = (Connection)l.get(1);
for(;;) {
dbTestCases.insert();
dbTestCases.countRows();
dbTestCases.retrieve();
}
}
}
PLease can anybody help me which is the best tool to use and how i have to use it,and any links for this.i am using linux operating system.
I think i am starting a new thread for each call of method can anybody suggest how to close thread before starting it again or use thred pool..
Thankyou in advance.
The same problem has happened to me once. The problem is your java reserved heap memory. This heap memory is configurable through one of the java config files; both it's minimum and maximum amount.
There are a lots of profilers.
I have been using Visual VM for a while and that's useful for me, at least. It's easy to use and pretty powerful tool as well.
Here's the link:
http://visualvm.java.net
Not sure which IDE you are using. But there's nothing do do with OutOfMemoryError which mean that it's a RuntimeException.
A little bit of offtopic (because I'm not going to suggest you a profiler), but the problem, I think, comes from these lines
ps1 = remoteConnection.prepareStatement(sql);
ps2 = localConnection.prepareStatement(sql);
int i = ps1.executeUpdate();
int k = ps2.executeUpdate();
and
Statement st1 = localConnection.createStatement();
Statement st2 = remoteConnection.createStatement();
ResultSet res1 = st1.executeQuery("SELECT * FROM user1");
ResultSet res2 = st2.executeQuery("SELECT * FROM user1");
After you create PreparedStatement and use it, you should close it. Either you should use close() method when you don't need it anymore or use try-with-resources (it use close() automatically). Java Tutorial on statements and on try-with-resources. Also consider reading this and this topics.
A would rewrite your code something like this (didn't actually tried it, but it compiles and it supposed to eliminate problems with memory leaks):
public class DBTestCases {
Connection localConnection;
Connection remoteConnection;
Connection localCon;
Connection remoteCon;
List<Connection> connectionsList;
String driver = "com.mysql.jdbc.Driver";
String user = "root";
String password = "root";
String dbName = "myDB";
String connectionUrl1= "jdbc:mysql://11.232.33:3306/"+dbName+"?user="+user+"&password="+password+"&useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&failOverReadOnly=false&maxReconnects=10";
String connectionUrl2= "jdbc:mysql://localhost:3306/"+dbName+"?user="+user+"&password="+password+"&useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&failOverReadOnly=false&maxReconnects=10";
public List<Connection> createConnection() {
try {
Class.forName(driver);
localCon = DriverManager.getConnection(connectionUrl2);
if(localCon != null)
System.out.println("connected to remote database at : "+new Date());
remoteCon = DriverManager.getConnection(connectionUrl1);
if(remoteCon != null)
System.out.println("connected to local database at : "+new Date());
connectionsList = new ArrayList<Connection>( 2 );
connectionsList.add( 0 , localCon );
connectionsList.add( 1 , remoteCon );
} catch(ClassNotFoundException cnfe) {
cnfe.printStackTrace();
} catch(SQLException sqle) {
sqle.printStackTrace();
}
return connectionsList;
}
public void insert(){
Runnable runnable = new Runnable(){
public void run() {
PreparedStatement ps1 = null;
PreparedStatement ps2 = null;
String sql = "insert into user1(name, address, created_date)" +
" values('johnsan', 'usa', '2013-08-04')";
if(remoteConnection != null&&localConnection != null) {
System.out.println("Database Connection Is Established");
try {
ps1 = remoteConnection.prepareStatement(sql);
ps2 = localConnection.prepareStatement(sql);
int i = ps1.executeUpdate();
int k = ps2.executeUpdate();
if(i > 0) {
System.out.println("Data Inserted into remote database table Successfully");
}
if(k > 0) {
System.out.println("Data Inserted into local database table Successfully");
}
} catch (SQLException s) {
System.out.println("SQL code does not execute.");
s.printStackTrace();
}catch (Exception e) {
e.printStackTrace();
} finally {
if (ps1 != null) {
try {
ps1.close();
} catch (SQLException ex) {
System.out.println("Cannot close ps1 statement.");
}
}
if (ps2 != null) {
try {
ps2.close();
} catch (SQLException ex) {
System.out.println("Cannot close ps2 statement.");
}
}
}
}
System.out.println("Inserting values in db");
}
};
Thread thread = new Thread(runnable);
thread.start();
}
public void retrieve(){
Runnable runnable = new Runnable(){
public void run() {
Statement st1 = null;
Statement st2 = null;
ResultSet res1 = null;
ResultSet res2 = null;
try {
st1 = localConnection.createStatement();
st2 = remoteConnection.createStatement();
res1 = st1.executeQuery("SELECT * FROM user1");
res2 = st2.executeQuery("SELECT * FROM user1");
System.out.println("---------------------------Local Database------------------------");
while (res1.next()) {
Long i = res1.getLong("userId");
String s1 = res1.getString("name");
String s2 = res1.getString("address");
java.sql.Date d = res1.getDate("created_date");
System.out.println(i + "\t\t" + s1 + "\t\t" + s2 + "\t\t"+ d);
}
System.out.println("------------------------Remote Database---------------------");
while (res2.next()) {
Long i = res2.getLong("userId");
String s1 = res2.getString("name");
String s2 = res2.getString("address");
java.sql.Date d = res2.getDate("created_date");
System.out.println(i + "\t\t" + s1 + "\t\t" + s2 + "\t\t"+ d);
}
} catch (SQLException s) {
System.out.println("SQL code does not execute.");
s.printStackTrace();
}catch (Exception e) {
e.printStackTrace();
} finally {
if (res1 != null) {
try {
res1.close();
} catch (SQLException ex) {
System.out.println("Cannot close res1 result set.");
}
}
if (st1 != null) {
try {
st1.close();
} catch (SQLException ex) {
System.out.println("Cannot close st1 statement.");
ex.printStackTrace();
}
}
if (res2 != null) {
try {
res2.close();
} catch (SQLException ex) {
System.out.println("Cannot close res2 result set.");
}
}
if (st2 != null) {
try {
st2.close();
} catch (SQLException ex) {
System.out.println("Cannot close st2 statement.");
ex.printStackTrace();
}
}
}
}
};
Thread thread = new Thread(runnable);
thread.start();
}
public static void main(String[] args) {
DBTestCases dbTestCases = new DBTestCases();
List l = dbTestCases.createConnection();
dbTestCases.localConnection = (Connection)l.get(0);
dbTestCases.remoteConnection = (Connection)l.get(1);
for(;;) {
dbTestCases.insert();
dbTestCases.countRows();
dbTestCases.retrieve();
}
}
}
There is also another problem with these statements:
ResultSet res1 = st1.executeQuery("SELECT * FROM user1");
ResultSet res2 = st2.executeQuery("SELECT * FROM user1");
I should never (with very rare exceptions) read entire table without using conditions in WHERE. Also if you need only one column created_date, then you must rewrite it like this:
ResultSet res1 = st1.executeQuery("SELECT created_date FROM user1");
ResultSet res2 = st2.executeQuery("SELECT created_date FROM user1");
But I didn't actually replaced it in a full listing, because it can be just 'quick and dirty' code. :)

Exporting SQL query result to csv or Excel

I want to write the result of a SQL query to a csv or Excel file and save it in a particular folder. I have following requests:
I would like to know, if this can be achieved using a Java program which can be reused for any SQL query result.
I would also like to know, if this can be used for different databases (Oracle, MySQL, SQL Server, etc.).
I plan to attach the saved file to an email. Is it possible to export SQL query results to an email directly?.
With use of openCSV API, you can export your data in csv file.
CSVWriter writer = new CSVWriter(new FileWriter("yourfile.csv"), '\t');
Boolean includeHeaders = true;
java.sql.ResultSet myResultSet = .... //your resultset logic here
writer.writeAll(myResultSet, includeHeaders);
writer.close();
Simplest solution.
Main Method
private List<String> resultSetArray=new ArrayList<>();
private String username =""; // Enter DB Username
private String password = ""; // Enter DB password
private String url = ""; // Enter DB URL
Connection connection=DriverManager.getConnection(url,user,pwd);
public static void main(String args[]) throws Exception{
fetchDataFromDatabase("SQL queries", connection);
printToCsv(resultArray);
}
fetchDataFromDatabase
The code below count the number of columns in a table, and store in a result array.
private void fetchDataFromDatabase(String selectQuery,Connection connection) throws Exception{
try {
Statement stmt = connection.createStatement();
ResultSet rs = stmt.executeQuery(selectQuery);
int numCols = rs.getMetaData().getColumnCount();
while(rs.next()) {
StringBuilder sb = new StringBuilder();
for (int i = 1; i <= numCols; i++) {
sb.append(String.format(String.valueOf(rs.getString(i))) + " ");
}
resultSetArray.add(sb.toString());
}
} catch (SQLException e) {
LOGGER.error("Sql exception " + e.getMessage());
}
}
printToCsv
public static void printToCsv(List<String> resultArray) throws Exception{
File csvOutputFile = new File(file_name);
FileWriter fileWriter = new FileWriter(csvOutputFile, false);
for(String mapping : resultArray) {
fileWriter.write(mapping + "\n");
}
fileWriter.close();
}
It's difficult to export result set data from any tool.
ex: while exporting result set data to .csv file it does not export properly when data contains (,)
please refer below java code :
it works perfectly with any any query input and all type of data in result set
package com.demo.export;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
public class dataExportService {
public void getDefendants(Connection con , String db) throws Exception {
#SuppressWarnings("unused")
Workbook readWorkbook = WorkbookFactory.create(new FileInputStream(".xls file path(C:\\Users\\CEPL\\Desktop\\test.xls)") );
#SuppressWarnings("resource")
Workbook writeWorkbook = new HSSFWorkbook();
Sheet desSheet = writeWorkbook.createSheet("new sheet");
Statement stmt = null;
ResultSet rs = null;
try{
String query ="QUERY";
stmt = con.createStatement();
rs = stmt.executeQuery(query);
ResultSetMetaData rsmd = rs.getMetaData();
int columnsNumber = rsmd.getColumnCount();
Row desRow1 = desSheet.createRow(0);
for(int col=0 ;col < columnsNumber;col++) {
Cell newpath = desRow1.createCell(col);
newpath.setCellValue(rsmd.getColumnLabel(col+1));
}
while(rs.next()) {
System.out.println("Row number" + rs.getRow() );
Row desRow = desSheet.createRow(rs.getRow());
for(int col=0 ;col < columnsNumber;col++) {
Cell newpath = desRow.createCell(col);
newpath.setCellValue(rs.getString(col+1));
}
FileOutputStream fileOut = new FileOutputStream(".xls file path(C:\\Users\\CEPL\\Desktop\\test.xls)");
writeWorkbook.write(fileOut);
fileOut.close();
}
}
catch (SQLException e) {
System.out.println("Failed to get data from database");
}
}
}
Here is an example:
import java.io.*;
import java.sql.*;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFCell;
public class ExcelFile {
public static void main(String[] args) {
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection connection = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/test", "root", "root");
PreparedStatement psmnt = null;
Statement st = connection.createStatement();
ResultSet rs = st.executeQuery("Select * from student");
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("Excel Sheet");
HSSFRow rowhead = sheet.createRow((short) 0);
rowhead.createCell((short) 0).setCellValue("Roll No");
rowhead.createCell((short) 1).setCellValue("Name");
rowhead.createCell((short) 2).setCellValue("Class");
rowhead.createCell((short) 3).setCellValue("Marks");
rowhead.createCell((short) 4).setCellValue("Grade");
int index = 1;
while (rs.next()) {
HSSFRow row = sheet.createRow((short) index);
row.createCell((short) 0).setCellValue(rs.getInt(1));
row.createCell((short) 1).setCellValue(rs.getString(2));
row.createCell((short) 2).setCellValue(rs.getString(3));
row.createCell((short) 3).setCellValue(rs.getInt(4));
row.createCell((short) 4).setCellValue(rs.getString(5));
index++;
}
FileOutputStream fileOut = new FileOutputStream("c:\\excelFile.xls");
wb.write(fileOut);
fileOut.close();
System.out.println("Data is saved in excel file.");
rs.close();
connection.close();
} catch (Exception e) {
}
}
}
Reference
This is my solution. Code to insert in the main class:
import java.io.*;
import java.sql.*;
import com.company.*;
/**
* Created by MAXNIGELNEGRO
*/
String[] filePath = new String[] {"C:\\Users\\Documents\\MyFile.csv"};
String[] driverDB = new String[] {"oracle.jdbc.driver.OracleDriver"};
String[] stringConnDB = new String[] {"jdbc:oracle:thin:#//127.0.0.1:1881/mydb"};
String[] userDB = new String[] {"pippo"};
String[] passDB = new String[] {"pluto"};
String[] charSep = new String[] {";"};
Boolean column= new Boolean (true);
String[] queryDB = new String[] {"select * FROM MYQUERY"};
try{
System.out.println("---------------File exist?------------" + filePath[0]);
File fileTemp = new File(filePath[0].toString());
if (fileTemp.exists()){
fileTemp.delete();
System.out.println("---------------DELETE FILE------------" + filePath[0] );
}
System.out.println("QUERY: ---->"+ queryDB[0].toString());
exportQueryToCsv exp = new exportQueryToCsv();
exp.exportQueryToCsv(filePath, driverDB, stringConnDB, userDB, passDB, queryDB, column, charSep);
if (fileTemp.exists()){
System.out.println("---File created---" + filePath[0]);
}
}
catch(Exception e){
e.printStackTrace();
}
The core class:
import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
/**
* Created by MAXNIGELNEGRO
*/
public class exportQueryToCsv {
public exportQueryToCsv(){}
public static void exportQueryToCsv (String[] filename, String[] driverDB, String[] connDB
, String[] userDB, String[] passDB, String[] queryDB, Boolean intestaFile
, String[] charSep) throws SQLException, IOException {
Statement stmt=null;
ResultSet rset=null;
Connection conn=null;
try { DBConn connessione = new DBConn();
conn=connessione.connect(driverDB[0],connDB[0],userDB[0],passDB[0]);
conn.setAutoCommit(false);
stmt = conn.createStatement();
rset = stmt.executeQuery(queryDB[0]);
ExportData2CSV csv = new ExportData2CSV();
csv.ExportData2CSV(rset,filename[0],intestaFile,charSep[0]);
csv.createFileCsv();
} catch (SQLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
finally {
if (stmt != null) {stmt.close();}
if (conn != null) {conn.close();}
if (rset != null) {rset.close();}
}
}
}
This is the class DBConn for the connection to the database:
import java.sql.*;
/**
* Created by MAXNIGELNEGRO
*/
public class DBConn {
public DBConn() {
}
public Connection connect(String driverDB, String db_connect_str, String db_userid, String db_password) {
Connection conn;
try {
Class.forName(driverDB).newInstance();
conn = DriverManager.getConnection(db_connect_str, db_userid, db_password);
} catch (Exception e) {
e.printStackTrace();
conn = null;
}
return conn;
}
}
This is the class for retrieves data from table to resultset and writes to csv file:
package com.company;
import java.io.FileWriter;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
/**
* Created by MAXNIGELNEGRO
*/
public class ExportData2CSV {
public ResultSet rset;
public String filename;
public Boolean columnName;
public String charSep;
public void ExportData2CSV(ResultSet rset, String filename, Boolean columnName, String charSep) {
this.rset = rset;
this.filename = filename;
this.columnName = columnName;
this.charSep = charSep;
}
public void createFileCsv() throws SQLException, IOException {
FileWriter cname = null;
try {
// WRITE COLUMN NAME
ResultSetMetaData rsmd = rset.getMetaData();
cname = new FileWriter(filename);
if (columnName) {
for (int i = 1; i <= rsmd.getColumnCount(); i++) {
cname.append(rsmd.getColumnName(i));
cname.append(charSep);
cname.flush();
}
cname.append(System.getProperty("line.separator"));
}
// WRITE DATA
while (rset.next()) {
for (int i = 1; i <= rsmd.getColumnCount(); i++) {
if (rset.getObject(i) != null) {
String data = rset.getObject(i).toString().replaceAll(charSep, "");
cname.append(data);
cname.append(charSep);
} else {
String data = "null";
cname.append(data);
cname.append(charSep);
}
}
//new line entered after each row
cname.append(System.getProperty("line.separator"));
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (cname != null) {
cname.flush();
cname.close();
}
if (rset != null) {
rset.close();
}
}
}
}
This is the excel data sheet
input excel file containing records
Hi this is the solution you need 3 files 1.input thread 2.output thread 3.data structure 4.main
1.input thread to read the excel and output thread to write the sql out put
2.data structure is to hold and transfer the data
(InputThread.java)
import java.io.*;
public class InputThread extends Thread{
String fp;
InputString is;
String tableName="emp";
String outFile;
InputThread(String FilePath,String nameOfTheTable,String outFileName){
fp=FilePath;
outFile=outFileName;
tableName=nameOfTheTable;
}
public void run(){
File file = new File(fp);
String line;
try{
BufferedReader br = new BufferedReader(new FileReader(file));
if( (line=br.readLine()) != null)
is = new InputString(line);
//transform(is);
InputString tmp = new InputString(createTable(line));
//tmp.next = is;
is = tmp;
//tmp = tmp.next;
for(; (line = br.readLine()) != null; ) {
tmp.next = new InputString(line);
tmp = tmp.next;
transform(tmp);
}
}catch(Exception e){ System.out.println("Error is :"+e); }
//traverse();
new OutputThread(is,outFile).start();
}
void transform(InputString x){
String[] arr = x.getLine().split(",");
String sql = "insert into "+tableName+" values(";
for(int i=0;i<arr.length;i++){
sql+="'"+arr[i]+"'";
if( (i+1) < arr.length) sql+=",";
}
sql+=");";
x.setLine(sql);
}
String createTable(String x){
String[] arr = x.split(",");
String sql = "create database vamsidb "+ "use vamsidb "+"create table "+tableName+"(";
for(int i=0;i<arr.length;i++){
sql+=arr[i]+" varchar(50)";
if( (i+1) < arr.length) sql+=",";
}
sql+=");";
return sql;
}
/*public void traverse(){
InputString tmp = is;
while(is != null){
System.out.println(is.getLine());
is=is.next;
}
}*/
}
(OutputThread.java)
import java.io.*;
public class OutputThread extends Thread{
InputString is;
String outFile;
OutputThread(InputString linkedList,String outFileName){
is=linkedList;
outFile = outFileName;
}
public void run(){
try{
FileOutputStream fos = new FileOutputStream(outFile);
while(is != null){
fos.write(is.getLine().getBytes());
is=is.next;
}
fos.close();
}catch(Exception e){
System.out.println("Error is :"+e);
}
}
}
(Main.java)
public class Main{
public static void main(String[] args){
InputThread it = new InputThread("sasken.csv","emp","output.sql");
it.start();
}
}
(DataStructure.java)
//This class represents the data structure to hold and transform input
//data as a linked list of sql statements
class InputString{
String line;
InputString next;
InputString(String x){
line = x;
}
String getLine(){
return line;
}
void setLine(String x){
line = x;
}
}
output result
You can use JDBC to fetch the records from DB in java and then use Apache POI for exporting the data to CSV/Excel.
Additionally, you can use the desktop API of java to send email using your default email client.
For this to work you need to work write a small code that can take up any query and any driver . The first input should be the driver name as the input to the software that you are writing. Then the software you are writing should be in a position to execute any SQL given to it and give out only rows and columns.
The next task comes to parse the ResultSet that comes from the JDBC of java application. Either you want to write the results into CSV file or EXCEL is based on how good you have the java api to do that.
Writing the output into the CVS is easy and not trival. I have not worked on exporting the data into Excel. I am sure you find jars for that.
The solution is based on a properties file.
Where this options are configured:
The parameters of the database.
Optional extra SQL Where clause.
The parameters of the output files.
The process can start bis 4 threads to download 4 tables at the same time.
If you want to run again, the generated files must be deleted.
A text file (logs.txt) with the process data is also created.
---- PROP FILE : ExportProperties.prop ---------
The solution will write a draft cinfiguration version in:
C:\tmp\test\ExportProperties.prop
## configuration properties
driver=oracle.jdbc.driver.OracleDriver
url=jdbc:oracle:thin:#ldap://localhost
username=user
password=pass
fileExtension=_20200623.csv
columSeparator=;
charsetName=CP1252
## export only 10 rows change to false
only10Rows=true
##tables
tableName.1=USER
tableName.1.sqlWhere= user.name IS NOT NULL
tableName.2=ROLL
tableName.3=FIRMA
--------- The main file --------
public class ExportTable2CSVMain implements Runnable {
static SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss SSS|");
static final String folder = "C:\\tmp\\test\\";
static final Properties prop = getProperties();
public static void main(String[] args) {
for (int x = 1; x < 6; x++) {
try {
writieLog(0, "Start thread " + x);
new ExportTable2CSVMain();
Thread.sleep(1000);
} catch (final Exception e) {
e.printStackTrace();
}
}
}
public ExportTable2CSVMain() {
final Thread t = new Thread(this);
t.start(); // start the thread -> run
}
#Override
public void run() {
int pos = 1;
String tableName = prop.getProperty("tableName." + pos);
while (tableName != null) {
try {
export(tableName, pos++);
} catch (final Exception e) {
e.printStackTrace();
}
tableName = prop.getProperty("tableName." + pos);
}
}
private void export(String tableName, int filePos) throws Exception {
final boolean only10Rows = prop.getProperty("only10Rows", "false").equals("true");
String extraWhere = prop.getProperty("tableName."+filePos+".sqlWhere");
if(extraWhere ==null)
extraWhere = prop.getProperty("sqlWhere");
if(extraWhere ==null)
extraWhere = "";
final String sql = "select * from " + tableName + extraWhere
+ (only10Rows ? " FETCH NEXT 10 ROWS ONLY" : "");
final String fileName = folder + tableName + prop.getProperty("fileExtension");
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
ExportTable2CSV data2csv = null;
try {
data2csv = new ExportTable2CSV(fileName, tableName, filePos);
if (data2csv.toDo()) {
conn = getConnection();
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
data2csv.createFileCsv(rs);
}
} catch (final Exception e) {
final int row = data2csv == null ? -1 : data2csv.row;
writieLog(filePos, "SQL", "Error", "rows:" + row, tableName, e.getMessage());
e.printStackTrace();
} finally {
try {
rs.close();
} catch (final Exception e) {
}
try {
stmt.close();
} catch (final Exception e) {
}
try {
conn.close();
} catch (final Exception e) {
}
}
}
public Connection getConnection() throws Exception {
Class.forName(prop.getProperty("driver"));
return DriverManager.getConnection(//
prop.getProperty("url"),
prop.getProperty("username"),
prop.getProperty("password"));
}
static private Properties getProperties() {
File file = new File(folder);
if (!file.exists()) { // if the folder do not exist create it
file.mkdirs();
}
file = new File(folder + "ExportProperties.prop");
if (!file.exists()) {
try {
final PrintWriter out = new PrintWriter(
new BufferedWriter(new FileWriter(folder + "ExportProperties.prop", true)));
out.println(//
"## configuration properties\n" +
"driver=oracle.jdbc.driver.OracleDriver\n" +
"url=jdbc:oracle:thin:#ldap://localhost\n"+
"username=USER\n" +
"password=PASSWORD\n" +
"sqlWhere=\n" +
"fileExtension=_20200619.csv\n" +
"columSeparator=;\n" +
"charsetName=CP1252\n" +
"##tables\n" +
"tableName.1=USER\n" + //
"tableName.2=ROLL\n"
);
out.close();
} catch (final Exception e) {
e.printStackTrace();
}
}
final Properties prop = new Properties();
try {
prop.load(new FileInputStream(folder + "ExportProperties.prop"));
} catch (final IOException e) {
e.printStackTrace();
}
return prop;
}
public static void writieLog(int filePos, String... txt) throws Exception {
final PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(folder + "logs.txt", true)));
String sb = "";
sb += formatter.format(new Date()) + "\t";
sb += filePos == 0 ? "" : "F-" + filePos + "\t";
for (final String s : txt) {
sb += s + "\t";
}
System.out.println(sb);
out.println(sb);
out.close();
}
}
---------------- the ExportTable2CSV file -------
/**
* Created by Jose Manuel Davila (Mel) kailas.mel#gmail.com
*/
public class ExportTable2CSV {
final String fileName;
final String table;
final String columSeparator;
final Boolean columnName = true;
final int filePos;
int row = 0;
int column = 0;
public ExportTable2CSV(String fileName, String table, int filePos) {
this.filePos = filePos;
this.fileName = fileName;
this.table = table;
columSeparator = ExportTable2CSVMain.prop.getProperty("columSeparator", ";");
}
public boolean toDo() throws Exception {
if (new File(fileName).exists()) {// the file exist jet return
return false;
}
writeLine("");
return true;
}
public void createFileCsv(ResultSet rs) throws Exception {
String sb = "";
try {
ExportTable2CSVMain.writieLog(filePos, "FILE", "INI ", table, fileName);
// WRITE COLUMN NAME
final ResultSetMetaData rsmd = rs.getMetaData();
sb = "";
final List<String> list = new ArrayList<String>();
if (columnName) {
for (int i = 1; i <= rsmd.getColumnCount(); i++) {
sb += rsmd.getColumnName(i) + columSeparator;
list.add(rsmd.getColumnName(i));
}
writeLine(sb.toString());
}
// WRITE DATA
while (rs.next()) {
sb = "";
column = 0;
for (int i = 1; i <= rsmd.getColumnCount(); i++) {
final Object obj = rs.getObject(i);
String data = "";
if (obj != null) {
if (obj instanceof String) {
data = obj.toString();
if (data.indexOf(columSeparator) != -1) {
if (data.indexOf("\"") != -1) {
data = data.replaceAll("\"", "'");
ExportTable2CSVMain.writieLog(filePos, "FILE", "WITH comm and ; ", "row:" + row,
"Column:" + list.get(column), table, fileName);
}
data = "\"" + data + "\"";
}
} else {
data = obj.toString();
}
}
sb += data + columSeparator;
column++;
}
writeLine(sb.toString());
row++;
}
ExportTable2CSVMain.writieLog(filePos, "FILE", "END ", "rows:" + row, table, fileName);
} catch (final Exception e) {
ExportTable2CSVMain.writieLog(filePos, "FILE", "Error ", "rows:" + row, table, fileName, e.getMessage());
e.printStackTrace();
} finally {
if (rs != null) {
rs.close();
}
}
}
void writeLine(String line) throws Exception {
if (row > 0 && row % 1000 == 0) {
System.out.println(filePos + " " + row + " working ...");
}
final PrintWriter cname = new PrintWriter(new BufferedWriter((new OutputStreamWriter(
new FileOutputStream(fileName, true), ExportTable2CSVMain.prop.getProperty("charsetName", "CP1252")))));
if (line.equals("")) {
cname.print(line);
} else {
cname.println(line);
}
cname.close();
}
}
--------- POM file pom.xml ----------------
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>ExportTable2CSV</groupId>
<artifactId>ExportTable2CSV</artifactId>
<version>1.1.0</version>
<name>ExportTable2CSV</name>
<properties>
<ojdbc8.version>18.3.0.0.0</ojdbc8.version>
</properties>
<dependencies>
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc8</artifactId>
<version>${ojdbc8.version}</version>
<scope>runtime</scope>
</dependency>
</dependencies>
</project>
To export to a column separated with tab.
change this prop in properties file :
fileExtension=_mel.txt
columSeparator=\t
This works perfectly fine:
PreparedStatement statement = famuat.prepareStatement(sql query);
ResultSet result = statement.executeQuery();
ResultSetMetaData md = result.getMetaData();
FileWriter fw = new FileWriter(filepath);
int columnCount = md.getColumnCount();
for (int i = 1; i <= columnCount; i++) {
columnheader = md.getColumnName(i);
// System.out.println(columnheader);
if (i > 1) System.out.print(" | ");
if (i > 1) System.out.print(", ");
if (i > 1) System.out.print("\t");
fw.append(columnheader.toUpperCase());
fw.append(",");
System.out.println(columnheader);
}
while (result.next()) {
fw.append("\r\n");
for (int i = 1; i <= columnCount; i++) {
if (i > 1) System.out.print(" | ");
if (i > 1) System.out.print(", ");
if (i > 1) System.out.print("\t");
fw.append(result.getString(i));
fw.append(",");
row = result.getString(i);
System.out.println(row);
}
}
this work for exort to csv and txt
public void ExportTxt(){
FileWriter texto = null;
ResultSet rs2 = null;
ResultSet rs = null;
try {
texto = new FileWriter("texto.txt");
PrintWriter pw = new PrintWriter(texto);
Statement consulta = conn.createStatement();
Statement consulta2 = conn.createStatement();
String query = "SELECT * FROM coches";
rs = consulta.executeQuery(query);
int i=1;
pw.println("----------------");
while (rs.next()) {
pw.println(i + ",Coche: Matricula:" + rs.getString("matricula") + ", Marca:" + rs.getString("marca") + ", Modelo:" + rs.getString("modelo"));
String query2 = "SELECT * FROM reparaciones WHERE coche = '" + rs.getString("matricula") + "'";
rs2 = consulta2.executeQuery(query2);
pw.println("----------------");
while(rs2.next()){
pw.println(rs2.getRow() + ",Reparacion: Fecha entrada:" + rs2.getString("fecha_entrada") + ", Fecha salida:" + rs2.getString("fecha_salida"));
}
i++;
}
rs.close();
rs2.close();
consulta.close();
} catch (IOException ex) {
Logger.getLogger(Conexion.class.getName()).log(Level.SEVERE, null, ex);
} catch (SQLException ex) {
Logger.getLogger(Conexion.class.getName()).log(Level.SEVERE, null, ex);
} finally {
try {
texto.close();
} catch (IOException ex) {
Logger.getLogger(Conexion.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
public void ExportCSV(){
PrintWriter pw = null;
try {
File archivo = new File("excel.csv");
pw = new PrintWriter((new FileWriter(archivo)));
Statement consulta = conn.createStatement();
String query = "SELECT * FROM clientes";
ResultSet rs = consulta.executeQuery(query);
pw.println("id;nombre;apellidos;direccion;nif;telefono");
while(rs.next()){
pw.println(rs.getInt("id") + ";" + rs.getString("nombre") + ";" + rs.getString("Apellidos") + ";" + rs.getString("direccion") + ";" + rs.getString("nif") + ";" + rs.getString("telefono"));
}
} catch (IOException ex) {
Logger.getLogger(Conexion.class.getName()).log(Level.SEVERE, null, ex);
} catch (SQLException ex) {
Logger.getLogger(Conexion.class.getName()).log(Level.SEVERE, null, ex);
} finally {
pw.close();
}
}
Tried some of the other answers and they didn't really work and/or ask to add libraries, which I like to avoid, especially for a format as dead simple as CSV.
So here's my own take. No special library requirements (other than your SQL driver) so (mostly) vanilla Java 8 or later should work. Also works with beanshell (hence try/finally rather than try-with-resources.) All fields should be escaped correctly, assuming the junit test I created (posted) is correct, so any RFC 4180 compliant CSV reader should be able to parse it without errors and/or getting it wrong, and for the purpose of this code, that includes Microsoft Excel.
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Connection;
import java.sql.Statement;
import java.util.List;
import java.util.ArrayList;
import java.io.FileWriter;
// Replace this with your own SQLServerDriver class
import com.microsoft.sqlserver.jdbc.SQLServerDriver;
public class Sandbox {
/**
* Replaces null with an empty string, surrounds values that have commas or
* newlines with quotes, and if a value begins with a quote it escapes any
* quotes with another quote
*
* #param value
* #return
*/
public String csvEscape(String value) {
if (value == null) {
value = "";
} else if (value.contains(",") || value.contains("\n") || value.startsWith("\"")) {
value = "\"" + value.replace("\"", "\"\"") + "\"";
}
return value;
}
/**
* Run an SQL query and dump the output into a CSV file as we receive it
*
* #param url URL to your server in a format that the driver expects, i.e.
* "jdbc:sqlserver://server.com:1433;IntegratedSecurity=true;databaseName=foo"
* #param statement SQL statement, i.e. "SELECT foo FROM bar". Do NOT pass user
* generated strings!
* #param csvPath File path to the csv file, i.e. /foo/bar.csv
* #throws SQLException
* #throws IOException
*/
public void sqlToCsv(String url, String statement, String csvPath) throws SQLException, IOException {
Connection connection = DriverManager.getConnection(url);
Driver driverSelect = new SQLServerDriver();
DriverManager.registerDriver(driverSelect);
Statement stmt = connection.createStatement();
ResultSet rs = stmt.executeQuery(statement);
ResultSetMetaData md = rs.getMetaData();
int colCount = md.getColumnCount();
List<String> cols = new ArrayList<String>();
for (int i = 1; i <= colCount; i++) {
String value = csvEscape(md.getColumnName(i));
cols.add(value);
}
List<String> row = new ArrayList<String>();
FileWriter fileWriter = null;
try {
fileWriter = new FileWriter(csvPath, false);
fileWriter.write(String.join(",", cols) + "\n");
while (rs.next()) {
row.clear();
for (int i = 1; i <= colCount; i++) {
String value = csvEscape(rs.getString(i));
row.add(value);
}
fileWriter.write(String.join(",", row) + "\n");
}
} finally {
fileWriter.close();
}
}
#Test
public void testCsvEscape() {
assertEquals("say \"cheese!\"", csvEscape("say \"cheese!\""));
assertEquals("\"\"\"say cheese\"\"!\"", csvEscape("\"say cheese\"!"));
assertEquals("\"cheese\nplease\"", csvEscape("cheese\nplease"));
assertEquals("\"cheese, please\"", csvEscape("cheese, please"));
assertEquals("\"say \"\"cheese,\n please!\"\"\"", csvEscape("say \"cheese,\n please!\""));
assertEquals("\"\"\"say \"\"cheese,\n please!\"\"\"\"\"", csvEscape("\"say \"cheese,\n please!\"\""));
}
}
Yes!
You can connect to the different database types using jdbc and then create an Excel with the results (Seen here).
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class TestAccessExcel {
public static Connection getConnection() throws Exception {
String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
String url = "jdbc:odbc:excelDB";
String username = "username";
String password = "pass";
Class.forName(driver);
return DriverManager.getConnection(url, username, password);
}
public static void main(String args[]) {
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
conn = getConnection();
stmt = conn.createStatement();
String excelQuery = "select * from [Sheet1$]";
rs = stmt.executeQuery(excelQuery);
while (rs.next()) {
System.out.println(rs.getString("BadgeNumber") + " " + rs.getString("FirstName") + " "
+ rs.getString("LastName"));
}
} catch (Exception e) {
System.err.println(e.getMessage());
} finally {
try {
rs.close();
stmt.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}

Categories