Java Program to export data from msql database to CSV file


Due to this program its possible to export data from mysql database to cvs file using java code...
// Comment


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class automateExport {
    public static void main(String[] args) {
        DBase db = new DBase();
        Connection conn = db.connect(
                "jdbc:mysql://localhost:3306/test","root","caspian");
        
        if (args.length != 1) {
            System.out.println(
                    "Usage: java automateExport [outputfile path] ");
            return;
        }
        db.exportData(conn,args[0]);
    }
    
}

class DBase {
    public DBase() {
    }
    
    public Connection connect(String db_connect_str, 
            String db_userid, String db_password) {
        Connection conn;
        try {
            Class.forName("com.mysql.jdbc.Driver").newInstance();
            conn = DriverManager.getConnection(db_connect_str,
                    db_userid, db_password);
            
        } catch(Exception e) {
            e.printStackTrace();
            conn = null;
        }
        return conn;
    }
    
    public void exportData(Connection conn,String filename) {
        Statement stmt;
        String query;
        try {
            stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
                    ResultSet.CONCUR_UPDATABLE);
            
            //For comma separated file
            query = "SELECT id,text,price into OUTFILE  '"+filename+
                    "' FIELDS TERMINATED BY ',' FROM testtable t";
            stmt.executeQuery(query);
            
        } catch(Exception e) {
            e.printStackTrace();
            stmt = null;
        }
    }
};


raja P Web Developer

1 comment:

  1. Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging. If anyone wants to become a Java developer learn from Java Training in Chennai. or learn thru Java Online Training in India . Nowadays Java has tons of job opportunities on various vertical industry.

    ReplyDelete

Related Posts Plugin for WordPress, Blogger...