By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,395 Members | 2,541 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,395 IT Pros & Developers. It's quick & easy.

How to export table data from a mysql database into text file using java?

P: 61
Hai,

i'm a beginner to java...

just now i had tried to read and write files using java...

and then i had tried to connect a database using jdbc...

now i want to export the data's from a database into a text file

using java....

Can any one plzz give me an idea for doing this process...

thanks and regards..
senthil.
Feb 9 '07 #1
Share this Question
Share on Google+
2 Replies


10K+
P: 13,264
Hai,

i'm a beginner to java...

just now i had tried to read and write files using java...

and then i had tried to connect a database using jdbc...

now i want to export the data's from a database into a text file

using java....

Can any one plzz give me an idea for doing this process...

thanks and regards..
senthil.
Write files using BufferedWriter and FileWriter
Feb 9 '07 #2

P: 1
Output it to a file:

Expand|Select|Wrap|Line Numbers
  1.  SELECT agent_index, program_name, description 
  2. INTO OUTFILE "/tmp/my_table_widgets.out" 
  3. FROM my_table_widgets

Then to get it back:

Expand|Select|Wrap|Line Numbers
  1.  LOAD DATA LOCAL INFILE '/tmp/my_table_widgets.out'
  2. INTO TABLE my_table_widgets
  3. FIELDS TERMINATED BY '\t'
  4. LINES TERMINATED BY '\n'
  5. (agent_index, program_name, description);

Here is the java to do it:

Expand|Select|Wrap|Line Numbers
  1.  try {
  2.   Class.forName(Driver);
  3.   con = DriverManager.getConnection(connect_url);
  4.  if(!con.isClosed()){
  5.       stmt = con.createStatement(); 
  6.  
  7.     stmt.executeQuery("SELECT agent_index, program_name, description " +
  8. "INTO OUTFILE '/tmp/my_table_widgets.out' " +
  9. "FROM my_table_widgets ");
  10. }
  11. }
  12. catch(Exception e) {
  13.    System.err.println("Exception: " + e.getMessage());
  14.     } 
  15. finally {
  16.       try {
  17.         if(con != null)
  18.           con.close();
  19.       } catch(SQLException e) {}
  20.  }
  21. }
  22. }
Jul 12 '08 #3

Post your reply

Sign in to post your reply or Sign up for a free account.