views:

41

answers:

1

Hi,

I am trying to do 2 output data from mysql to a csv file. My code is as follows: public void exportData(Connection conn,String filename) { Statement stmt; String query; try { stmt = conn.createStatement();

        //For comma separated file
        query = "SELECT * into OUTFILE  '/tmp/input.csv' FIELDS TERMINATED BY ',' FROM router ";
        stmt.executeQuery(query);

    } catch(Exception e) {
        e.printStackTrace();
        stmt = null;
    }
}

};

I get the following error at the line where stmt.executequery is called.

java.sql.SQLException: Access denied for user 'nxy'@'%' (using password: YES) at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1075) at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3566) at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3498) at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1959) at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2113) at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2562) at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2512) at com.mysql.jdbc.StatementImpl.executeQuery(StatementImpl.java:1476) at DBase.exportData(automateExport.java:50) at automateExport.main(automateExport.java:18)

I am able to connect to the database properly and even execute basic queries but unable to export data to a .csv file. Please help.

Thank you

+1  A: 

You need to have the FILE privilege for your user account to allow for the file to be created at the server host, via a SELECT .. INTO OUTFILE command.

Vineet Reynolds
Thanks for the quick response. Is there anyway I can enable it ?
jillika iyer
You need to run GRANT FILE on "*.*" to <user>@<host> (remove quotes around star-dot-star, as Stackoverflow is eating them up) to grant it to a user (replace the user and host tags with appropriate values for your case). Since GRANT FILE is an administrative privilege, you need to connect as root to execute it.
Vineet Reynolds
Thanks. But I do not understand why I should create file at server host??? I just want to create a file on my machine and export the data here. Could you please explain??
jillika iyer
Is there no way to do it without using Grant File ?? As I do not have any kind of administrative access and unable to connect as root. :(
jillika iyer
Well, in that case you'll need to write code (perhaps in Java itself) that will obtain all the rows of the table, which you can save as a CSV file (if you format the results correctly). This link might help you in assessing other options: http://support.modwest.com/content/6/135/en/how-can-i-export-data-in-csv-or-tab-delimited-format.html
Vineet Reynolds
Hi thanks again. I was able to get an account created for on the particular host. But my user account remains the same for mysql with same read access. In what way could I create the csv file now. I am not able to connect to that machine remotely either.
jillika iyer
Well, if you are able to connect to the MySQL database via JDBC from a Java application, you should be able to perform a select on the table to extract the data and then write it to a CSV file in a location of your choice. Given your circumstances, I do not think that you will be able to write onto the database server host at all, so JDBC access appears to be the best bet.
Vineet Reynolds
Hi is there any way I could use mysqldump and dump all contents of the database to my machine and then export required info to a csv file?
jillika iyer
It's possible to have mysqldump produce a csv file containing the table dump. You need to specify the database name and the table name to the mysqldump client.
Vineet Reynolds
Addendum: Use the fields-terminated-by switch of mysqldump with comma as a parameter.
Vineet Reynolds
thank you so much :)
jillika iyer