Results 1 to 12 of 12
  1. #1
    priya2184 is offline Member
    Join Date
    Apr 2011
    Posts
    7
    Rep Power
    0

    Smile Output write into a file

    Hi Sir,

    I have used jdbc connection to get connecting to the database.
    Now i want to write the sql quires result to a file and also to the console..For that i have written a query below:

    --------******Code Start Her*****-------------


    import java.io.BufferedWriter;
    import java.io.FileOutputStream;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.sql.*;



    public class DatabaseCon {

    public static void main(String args[]) throws IOException {

    Connection connection = null;
    try {
    // Load the JDBC driver
    String driverName = "oracle.jdbc.driver.OracleDriver";
    Class.forName(driverName);

    // Create a connection to the database
    String serverName = "xx.yy.zz.ppp";
    String portNumber = "xxxx";
    String sid = "xxxx";
    String url = "jdbc:oracle:thin:@" + serverName + ":" + portNumber + ":" + sid;
    String username = "scott";
    String password = "tiger";
    connection = DriverManager.getConnection(url, username, password);
    System.out.println("New Connection Create ....");
    Statement stmt = connection.createStatement();
    ResultSet rset = stmt
    .executeQuery("SELECT ID, FIRST_NAME,LAST_NAME FROM EMPLOYEE");
    while (rset.next()){
    System.out.println(rset.getString(1));
    System.out.println(rset.getString(2));
    System.out.println(rset.getString(3));

    FileWriter fstream = new FileWriter("/Users/priyaranjan/Desktop/Helloexi12.txt");
    BufferedWriter out = new BufferedWriter(fstream);
    out.write(rset.getString(1));
    out.write(rset.getString(2));
    out.write(rset.getString(3));



    }
    catch (ClassNotFoundException e) {
    System.out.println("Could not find the database driver");
    } catch (SQLException e) {
    System.out.println("Could not connect to the database");
    }



    }

    }
    }


    --------------***********Code ends Here********----------------


    When i tried to run it it successfully writes the result to console but could not able to write it to output file.
    But this program is not showing any errors.
    So please any one help me out ....


    Thanks in advance..

    Regards ,
    Priyaranjan

  2. #2
    dswastik is offline Senior Member
    Join Date
    Dec 2008
    Location
    Kolkata
    Posts
    282
    Rep Power
    6

    Default

    Does this help?
    Java Code:
    import java.io.BufferedWriter;
    import java.io.FileOutputStream;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.sql.*;
    
    
    
    public class DatabaseCon {
    
    public static void main(String args[]) throws IOException {
    
    Connection connection = null;
    try {
    // Load the JDBC driver
    String driverName = "oracle.jdbc.driver.OracleDriver";
    Class.forName(driverName);
    
    // Create a connection to the database
    String serverName = "xx.yy.zz.ppp";
    String portNumber = "xxxx";
    String sid = "xxxx";
    String url = "jdbcracle:thin:@" + serverName + ":" + portNumber + ":" + sid;
    String username = "scott";
    String password = "tiger";
    connection = DriverManager.getConnection(url, username, password);
    System.out.println("New Connection Create ....");
    Statement stmt = connection.createStatement();
    ResultSet rset = stmt
    .executeQuery("SELECT ID, FIRST_NAME,LAST_NAME FROM EMPLOYEE");
    while (rset.next()){
    String a=rset.getString(1);
    String b=rset.getString(2);
    String c=rset.getString(3);
    System.out.println(a);
    System.out.println(b);
    System.out.println(c);
    
    FileWriter fstream = new FileWriter("/Users/priyaranjan/Desktop/Helloexi12.txt");
    BufferedWriter out = new BufferedWriter(fstream);
    out.write(a);
    out.write(b);
    out.write(c);
    
    
    
    }
    catch (ClassNotFoundException e) {
    System.out.println("Could not find the database driver");
    } catch (SQLException e) {
    System.out.println("Could not connect to the database");
    }
    
    
    
    }
    
    }
    }
    Swastik

  3. #3
    dlorde is offline Senior Member
    Join Date
    Jun 2008
    Posts
    339
    Rep Power
    7

    Default

    You're creating a new file writer for every row in the result set, so you're overwriting the file every time. Try creating the file writer outside the loop (or use the 'append' constructor).

  4. #4
    dswastik is offline Senior Member
    Join Date
    Dec 2008
    Location
    Kolkata
    Posts
    282
    Rep Power
    6

    Default

    That's right, but even if that is the case it should at least right the last record. So far as my knowledge goes in a forward only result set once you call a getter method in this case rset.getString(1), you can't call it again. If you call it again it should return u a null value, and when out.write(rset.getString(1)) is invoked its raising a nullpointerexception.

    In addition to this probably the output stream is needed to be flushed and closed.
    Swastik

  5. #5
    dlorde is offline Senior Member
    Join Date
    Jun 2008
    Posts
    339
    Rep Power
    7

    Default

    Quote Originally Posted by dswastik View Post
    So far as my knowledge goes in a forward only result set once you call a getter method in this case rset.getString(1), you can't call it again.
    You may well be right - but the API docs don't mention it. AIUI, the getters just get column values from the current RS row, which is buffered. You can't go back to the previous RS row, but I don't see why you can't get the current row values whenever you want. Of course, that doesn't mean it works, and I haven't tried it - backward compatibility in the Java library tends to mean that common-sense takes a back seat... :p

    In addition to this probably the output stream is needed to be flushed and closed.
    Yup - that may be the problem.

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,085
    Rep Power
    20

    Default

    Quote Originally Posted by dlorde View Post
    You may well be right - but the API docs don't mention it. AIUI, the getters just get column values from the current RS row, which is buffered. You can't go back to the previous RS row, but I don't see why you can't get the current row values whenever you want. Of course, that doesn't mean it works, and I haven't tried it - backward compatibility in the Java library tends to mean that common-sense takes a back seat... :p
    Quoting the API:
    For maximum portability, result set columns within each row should be read in left-to-right order, and each column should be read only once.

  7. #7
    priya2184 is offline Member
    Join Date
    Apr 2011
    Posts
    7
    Rep Power
    0

    Default

    Thanks DSWATIK

  8. #8
    dlorde is offline Senior Member
    Join Date
    Jun 2008
    Posts
    339
    Rep Power
    7

    Default

    Quote Originally Posted by Tolls View Post
    Quoting the API:
    Quote:
    For maximum portability, result set columns within each row should be read in left-to-right order, and each column should be read only once.
    D'oh! you're right - I totally missed that. Sorry... what can I say, it's Friday 13th!

    'For maximum portability' is a pretty useless qualification - if you can't rely on it, and there's no simple way to tell, they might as well say 'Don't read more than once!'

    I still don't see why, though. In the vast majority of situations, a single row should be bufferable...

  9. #9
    priya2184 is offline Member
    Join Date
    Apr 2011
    Posts
    7
    Rep Power
    0

    Default

    Hi I have found an another coding.Please review it and let me know if there is anything wrong in it.
    *********************


    import java.io.BufferedWriter;
    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.sql.*;



    public class DatabaseCon {



    public static void main(String args[]) throws IOException {



    Connection connection = null;
    try {
    // Load the JDBC driver
    String driverName = "oracle.jdbc.driver.OracleDriver";
    Class.forName(driverName);

    // Create a connection to the database
    String serverName = " ";
    String portNumber = " ";
    String sid = " ";
    String url = "jdbc:oracle:thin:@" + serverName + ":" + portNumber + ":" + sid;
    String username = "scott";
    String password = "tiger";
    connection = DriverManager.getConnection(url, username, password);
    System.out.println("New Connection Create ....");
    Statement stmt = connection.createStatement();
    ResultSet rset = stmt
    .executeQuery("SELECT ID, FIRST_NAME,LAST_NAME FROM EMPLOYEE");
    System.out.println("Employee Name: " );
    File f=new File("/Users/priyaranjan/Desktop/Hellodd1.txt");
    FileOutputStream fp=new FileOutputStream(f);
    fp.write("Here is the EMPLOYEES TABLE ".getBytes());
    String newline8="\n";
    fp.write(newline8.getBytes());
    while (rset.next()) {
    String employeeName = rset.getString(1);
    String employeeName1 = rset.getString(2);
    String employeeName2 = rset.getString(3);
    // fp.write("Here is the EMPLOYEES TABLE ".getBytes());
    fp.write(employeeName.getBytes());
    // String newline6="\r\n";
    fp.write(" ,".getBytes());
    fp.write(employeeName1.getBytes());
    fp.write(" ".getBytes());
    fp.write(employeeName2.getBytes());

    String newline="\r\n";

    fp.write(newline.getBytes());
    System.out.println(employeeName);
    System.out.println(employeeName1);
    System.out.println(employeeName2);
    }
    fp.flush();
    fp.close();
    //cn.close();

    FileOutputStream fileOut = new FileOutputStream("/Users/priyaranjan/Desktop/Helloexi12.txt");




    }
    catch (ClassNotFoundException e) {
    System.out.println("Could not find the database driver");
    } catch (SQLException e) {
    System.out.println("Could not connect to the database");
    }



    }

    }


    ***************************

  10. #10
    dswastik is offline Senior Member
    Join Date
    Dec 2008
    Location
    Kolkata
    Posts
    282
    Rep Power
    6

    Default

    Looks fine Priya, are you getting any error?
    Swastik

  11. #11
    priya2184 is offline Member
    Join Date
    Apr 2011
    Posts
    7
    Rep Power
    0

    Default

    No Swastik,

    I am getting right results.
    Thanks for your respond.I am new to java and this forum really help me a lot in solving my problem.

    But One thing using append operation hoe can I write to a file.
    Can you help me in the same...

    Regards ,
    Priyaranjan

  12. #12
    dswastik is offline Senior Member
    Join Date
    Dec 2008
    Location
    Kolkata
    Posts
    282
    Rep Power
    6

    Default

    You just need to create it in append mode

    Java Code:
    FileOutputStream fp=new FileOutputStream(f,true);
    Swastik

Similar Threads

  1. how to write output to a txt file? thanks!!!
    By sunsnow86 in forum New To Java
    Replies: 5
    Last Post: 01-24-2011, 01:05 PM
  2. Replies: 0
    Last Post: 09-30-2010, 08:16 PM
  3. How to write output data to a file
    By xana in forum NetBeans
    Replies: 39
    Last Post: 08-14-2010, 01:19 AM
  4. Replies: 8
    Last Post: 05-12-2010, 07:19 PM
  5. Replies: 1
    Last Post: 07-24-2007, 02:02 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •