473,479 Members | 2,115 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

How to retrive all the rows in a mysql database and write it to a file using java?

61 New Member
Hai to all,,

i had to tried to retrive and write a single row to a file from the database..

But dont know to write all the retrived rows to a file from the database..

how to do that...

here are my codings for writing a single row to a file...

Expand|Select|Wrap|Line Numbers
  1. import java.io.*;
  2. import java.sql.*;
  3.  
  4. public class DataBase {
  5.  
  6.   public static void main(String args[]) {
  7. String Driver;
  8. Statement stmt; 
  9. ResultSet rs; 
  10.  
  11. Driver = "com.mysql.jdbc.Driver";
  12. Connection con = null;
  13. try {
  14. File file = new File("C:/test.txt");
  15. BufferedWriter out = new BufferedWriter(new FileWriter(file));
  16. Class.forName(Driver);
  17. con = DriverManager.getConnection("jdbc:mysql://192.168.0.01/senthil","root","");
  18.       if(!con.isCloses)
  19. {
  20. System.out.println("Successfully connected to MySQL DataBase \n");
  21.                stmt = con.createStatement(); 
  22. rs = stmt.executeQuery("SELECT * FROM employees");
  23. InputStream data= null;
  24. System.out.println("Display all results: "); 
  25.                while (rs.next()) { 
  26.                   int id = rs.getInt("Employee_Number");
  27.       String name = rs.getString("First_Name"); 
  28.                   String addr = rs.getString("Second_Name");
  29.      data = rs.getBinaryStream(1);
  30.                  int temp=0;
  31.                  while( (temp = data.read()) != -1) {         
  32.      out.write(temp);        }                  
  33.                 System.out.println( id + name + addr);   
  34.                    } 
  35.  
  36.                  out.flush();   
  37.      out.close(); 
  38.  
  39.       }
  40.  
  41.     } catch(Exception e) {
  42.       System.err.println("Exception: " + e.getMessage());
  43.     } finally {
  44.       try {
  45.         if(con != null)
  46.           con.close();
  47.       } catch(SQLException e) {}
  48.     }
  49.   }
  50. }
Can any one plzz help me soon...

thanks and regards..
senthil
Feb 12 '07 #1
9 2829
r035198x
13,262 MVP
Hai to all,,

i had to tried to retrive and write a single row to a file from the database..

But dont know to write all the retrived rows to a file from the database..

how to do that...

here are my codings for writing a single row to a file...

Expand|Select|Wrap|Line Numbers
  1. import java.io.*;
  2. import java.sql.*;
  3.  
  4. public class DataBase {
  5.  
  6. public static void main(String args[]) {
  7. String Driver;
  8. Statement stmt; 
  9. ResultSet rs; 
  10.  
  11. Driver = "com.mysql.jdbc.Driver";
  12. Connection con = null;
  13. try {
  14. File file = new File("C:/test.txt");
  15. BufferedWriter out = new BufferedWriter(new FileWriter(file));
  16. Class.forName(Driver);
  17. con = DriverManager.getConnection("jdbc:mysql://192.168.0.01/senthil","root","");
  18. if(!con.isCloses)
  19. {
  20. System.out.println("Successfully connected to MySQL DataBase \n");
  21. stmt = con.createStatement(); 
  22. rs = stmt.executeQuery("SELECT * FROM employees");
  23. InputStream data= null;
  24. System.out.println("Display all results: "); 
  25. while (rs.next()) { 
  26. int id = rs.getInt("Employee_Number");
  27.      String name = rs.getString("First_Name"); 
  28. String addr = rs.getString("Second_Name");
  29.      data = rs.getBinaryStream(1);
  30. int temp=0;
  31. while( (temp = data.read()) != -1) { 
  32.      out.write(temp); } 
  33. System.out.println( id + name + addr); 
  34.  
  35. out.flush(); 
  36.      out.close(); 
  37.  
  38.      }
  39.  
  40. } catch(Exception e) {
  41. System.err.println("Exception: " + e.getMessage());
  42. } finally {
  43. try {
  44. if(con != null)
  45. con.close();
  46. } catch(SQLException e) {}
  47. }
  48. }
  49. }
Can any one plzz help me soon...

thanks and regards..
senthil
Just try this while loop
Expand|Select|Wrap|Line Numbers
  1.  while (rs.next()) { 
  2.       int id = rs.getInt("Employee_Number");
  3.       out.write(id);
  4.       String name = rs.getString("First_Name"); 
  5.       out.write(name);         
  6.       String addr = rs.getString("Second_Name");
  7.       out.write(addr);
  8.  }
  9.  
Feb 12 '07 #2
asenthil
61 New Member
Just try this while loop
Expand|Select|Wrap|Line Numbers
  1.  while (rs.next()) { 
  2.       int id = rs.getInt("Employee_Number");
  3.       out.write(id);
  4.       String name = rs.getString("First_Name"); 
  5.       out.write(name);         
  6.       String addr = rs.getString("Second_Name");
  7.       out.write(addr);
  8.  }
  9.  
Hai ro35...

thanks for ur reply...

now its writing the data to the file...

But itz not writing the Employee_Number to the file...

the output is like the follwing in the text file..

? Axel Washington
? Arvid Sharma
? Jonas Ginsberg
? Florence Wojokowski
? Sean Washington
?Elizabeth Yamaguchi

its sucessfully writing the two strings but itz not writing the integer id...

what to do for this?

thanks.
senthil.
Feb 12 '07 #3
r035198x
13,262 MVP
Hai ro35...

thanks for ur reply...

now its writing the data to the file...

But itz not writing the Employee_Number to the file...

the output is like the follwing in the text file..

? Axel Washington
? Arvid Sharma
? Jonas Ginsberg
? Florence Wojokowski
? Sean Washington
?Elizabeth Yamaguchi

its sucessfully writing the two strings but itz not writing the integer id...

what to do for this?

thanks.
senthil.
try out.write(""+id);
also print the id to the console to make sure you're getting the id right from the database.
And you can call me r035198x rather than ro35, r0, or the rest of those variants.
Feb 12 '07 #4
asenthil
61 New Member
Hai r035198x...

Very very thanks for ur reply...

Now itz working perfectly...

i'm a beginner to java, jsp...

Now i'm learning hardly to work in java...

can u plzz tell me...

what are the main concepts i have to study in java

to do projects in jsp...

thanks
senthil.
Feb 12 '07 #5
hirak1984
316 Contributor
Well why do we need to write it this way?please explain.
try out.write(""+id);
.
Feb 12 '07 #6
asenthil
61 New Member
Ya i'm also want the reason for hirak's question...

plzz explain us.. r035198x....

thanks.
senthil.
Feb 12 '07 #7
r035198x
13,262 MVP
Hai r035198x...

Very very thanks for ur reply...

Now itz working perfectly...

i'm a beginner to java, jsp...

Now i'm learning hardly to work in java...

can u plzz tell me...

what are the main concepts i have to study in java

to do projects in jsp...

thanks
senthil.
Depends really on what you want to do with JSP. If you just want to master it, you can start by making small interesting programs with it. Just trying to come up with some nice system that involves one or two other technologies as well and you'll learn quite a lot about them that way.
Feb 12 '07 #8
r035198x
13,262 MVP
Ya i'm also want the reason for hirak's question...

plzz explain us.. r035198x....

thanks.
senthil.
id is an int. Calling write(id) calls a method
different from calling write(""+id); because the second one calls a method that takes a String as argument is more suited for presentation that the first method.
Feb 12 '07 #9
asenthil
61 New Member
ya okay,

now itz cleared...

thanks for ur support and responses...

senthil.
Feb 12 '07 #10

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

Similar topics

5
3361
by: apchar | last post by:
I am trying to use php as a kind of servlet to act as a middle man between a java applet and mysql. I know java has jdbc but it's flakey and painful. php access to mysql is much nicer. So I have:...
3
3130
by: NotGiven | last post by:
I am researching the best place to put pictures. I have heard form both sides and I'd like to know why one is better than the other. Many thanks!
0
6247
by: mdh | last post by:
I am trying to learn the basics of MVC applications using a Tomcat infrastructure. I'm starting by building a simple application with: * a login.jsp page for a basic login form with a action...
7
2475
by: Ike | last post by:
Let's say I have a MySQL 4.12 database, opened to the internet on 111.111.111.111 allowing all incoming and outgoing ports. I have a username and password setup, which CAN connect to this database,...
1
2591
by: asenthil | last post by:
Hai, Can any one tell me, How to retrive rows in alphabetcal order in the mysql database by using select query? thanks.. senthil
8
2511
by: asenthil | last post by:
Hai, i'm having a string in a specific field of a database... now i want to retrive that string from the database and i have to write that string into a xml file.... retriving is not a...
0
1694
by: nrip | last post by:
Dear All, I am facing a very peculiar problem. I am reading a CSV file from my JSP code and trying to insert them into MYSQL database. the program first reads a line and then splits it into words...
221
366924
Atli
by: Atli | last post by:
You may be wondering why you would want to put your files “into” the database, rather than just onto the file-system. Well, most of the time, you wouldn’t. In situations where your PHP application...
1
2967
by: amritranjan | last post by:
How to retrive image file from MS access database and display this in another JSPpage -------------------------------------------------------------------------------- This is my Jsp code for...
0
7027
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6899
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
6847
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5312
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4757
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4463
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
2970
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
555
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
166
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.