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(
        if (args.length != 1) {
                    "Usage: java automateExport [outputfile path] ");

class DBase {
    public DBase() {
    public Connection connect(String db_connect_str, 
            String db_userid, String db_password) {
        Connection conn;
        try {
            conn = DriverManager.getConnection(db_connect_str,
                    db_userid, db_password);
        } catch(Exception e) {
            conn = null;
        return conn;
    public void exportData(Connection conn,String filename) {
        Statement stmt;
        String query;
        try {
            stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
            //For comma separated file
            query = "SELECT id,text,price into OUTFILE  '"+filename+
                    "' FIELDS TERMINATED BY ',' FROM testtable t";
        } catch(Exception e) {
            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.


Related Posts Plugin for WordPress, Blogger...