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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • asenthil
    New Member
    • Dec 2006
    • 61

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

    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...

    Code:
    import java.io.*;
    import java.sql.*;
    
    public class DataBase {
    
      public static void main(String args[]) {
    String Driver;
    Statement stmt; 
    ResultSet rs; 
    
    Driver = "com.mysql.jdbc.Driver";
    Connection con = null;
    try {
    File file = new File("C:/test.txt");
    BufferedWriter out = new BufferedWriter(new FileWriter(file));
    Class.forName(Driver);
    con = DriverManager.getConnection("jdbc:mysql://192.168.0.01/senthil","root","");
          if(!con.isCloses)
    {
    System.out.println("Successfully connected to MySQL DataBase \n");
                   stmt = con.createStatement(); 
    rs = stmt.executeQuery("SELECT * FROM employees");
    InputStream data= null;
    System.out.println("Display all results: "); 
                   while (rs.next()) { 
                      int id = rs.getInt("Employee_Number");
    	  String name = rs.getString("First_Name"); 
                      String addr = rs.getString("Second_Name");
    	 data = rs.getBinaryStream(1);
                     int temp=0;
                     while( (temp = data.read()) != -1) {         
    	 out.write(temp);        }                  
                    System.out.println( id + name + addr);   
                       } 
    
                     out.flush();   
    	 out.close(); 
            
    	  }
    
        } catch(Exception e) {
          System.err.println("Exception: " + e.getMessage());
        } finally {
          try {
            if(con != null)
              con.close();
          } catch(SQLException e) {}
        }
      }
    }
    Can any one plzz help me soon...

    thanks and regards..
    senthil
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by asenthil
    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...

    Code:
    import java.io.*;
    import java.sql.*;
     
    public class DataBase {
     
    public static void main(String args[]) {
    String Driver;
    Statement stmt; 
    ResultSet rs; 
     
    Driver = "com.mysql.jdbc.Driver";
    Connection con = null;
    try {
    File file = new File("C:/test.txt");
    BufferedWriter out = new BufferedWriter(new FileWriter(file));
    Class.forName(Driver);
    con = DriverManager.getConnection("jdbc:mysql://192.168.0.01/senthil","root","");
    if(!con.isCloses)
    {
    System.out.println("Successfully connected to MySQL DataBase \n");
    stmt = con.createStatement(); 
    rs = stmt.executeQuery("SELECT * FROM employees");
    InputStream data= null;
    System.out.println("Display all results: "); 
    while (rs.next()) { 
    int id = rs.getInt("Employee_Number");
    	 String name = rs.getString("First_Name"); 
    String addr = rs.getString("Second_Name");
    	 data = rs.getBinaryStream(1);
    int temp=0;
    while( (temp = data.read()) != -1) { 
    	 out.write(temp); } 
    System.out.println( id + name + addr); 
    } 
     
    out.flush(); 
    	 out.close(); 
     
    	 }
     
    } catch(Exception e) {
    System.err.println("Exception: " + e.getMessage());
    } finally {
    try {
    if(con != null)
    con.close();
    } catch(SQLException e) {}
    }
    }
    }
    Can any one plzz help me soon...

    thanks and regards..
    senthil
    Just try this while loop
    Code:
     while (rs.next()) { 
    	  int id = rs.getInt("Employee_Number");
    	  out.write(id);
    	  String name = rs.getString("First_Name"); 
    	  out.write(name);		 
    	  String addr = rs.getString("Second_Name");
    	  out.write(addr);
     }

    Comment

    • asenthil
      New Member
      • Dec 2006
      • 61

      #3
      Originally posted by r035198x
      Just try this while loop
      Code:
       while (rs.next()) { 
      	  int id = rs.getInt("Employee_Number");
      	  out.write(id);
      	  String name = rs.getString("First_Name"); 
      	  out.write(name);		 
      	  String addr = rs.getString("Second_Name");
      	  out.write(addr);
       }
      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.

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by asenthil
        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.

        Comment

        • asenthil
          New Member
          • Dec 2006
          • 61

          #5
          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.

          Comment

          • hirak1984
            Contributor
            • Jan 2007
            • 316

            #6
            Well why do we need to write it this way?please explain.
            try out.write(""+id );
            .

            Comment

            • asenthil
              New Member
              • Dec 2006
              • 61

              #7
              Ya i'm also want the reason for hirak's question...

              plzz explain us.. r035198x....

              thanks.
              senthil.

              Comment

              • r035198x
                MVP
                • Sep 2006
                • 13225

                #8
                Originally posted by asenthil
                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.

                Comment

                • r035198x
                  MVP
                  • Sep 2006
                  • 13225

                  #9
                  Originally posted by asenthil
                  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.

                  Comment

                  • asenthil
                    New Member
                    • Dec 2006
                    • 61

                    #10
                    ya okay,

                    now itz cleared...

                    thanks for ur support and responses...

                    senthil.

                    Comment

                    Working...