Results 1 to 7 of 7
  1. #1
    javaguy345 is offline Member
    Join Date
    May 2011
    Posts
    15
    Rep Power
    0

    Default While Loop Problem

    try
    {
    File output = new File("c:\\Temp\\", "QuestionList.txt");

    BufferedWriter outFile = new BufferedWriter (
    new FileWriter(output.getPath(), true));

    Connection con = DriverManager.getConnection(databaseURL, "user", "password");
    Statement stmt = con.createStatement();
    ResultSet rs = stmt.executeQuery("SELECT * FROM APP.QUESTIONS");

    while (rs.next()) //I THINK THE PROBLEM IS HERE
    {
    String QUESTIONNO = rs.getString("QUESTIONNO");
    String QUESTION = rs.getString("QUESTION");
    String ANSWER = rs.getString("ANSWER");
    String ANSWERA = rs.getString("ANSWERA");
    String ANSWERB = rs.getString("ANSWERB");
    String ANSWERC = rs.getString("ANSWERC");
    String ANSWERD = rs.getString("ANSWERD");

    outFile.write(QUESTIONNO + " " + QUESTION);
    outFile.newLine();
    outFile.newLine();
    outFile.write("(a)" + ANSWERA);
    outFile.newLine();
    outFile.write("(b)" + ANSWERB);
    outFile.newLine();
    outFile.write("(c)" + ANSWERC);
    outFile.newLine();
    outFile.write("(d)" + ANSWERD);
    outFile.newLine();
    outFile.write("----------------------------------------------");
    outFile.newLine();
    outFile.close();
    }
    JOptionPane.showMessageDialog(null, "Questions added to text file");

    }catch(Exception e)
    {
    JOptionPane.showMessageDialog(null, "Unspecified File IO error");
    }

    Hey guys, I got another problem. So right now I got my data in the database and I want to be able to save that to a text file. The problem is that whenever I try to save I'm able to get the data for one row and that's it. I need to be able to get the data from the whole table. Thanks in advance.

  2. #2
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    Going out on a limb here, as I haven't run the code. But I'm guessing it's because you close your output file after the end of the first loop.

  3. #3
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    If your loop is only executing once then there is only one row in the ResultSet. Try executing your SQL outside Java to make sure the results you get are what you are expecting.

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    Quote Originally Posted by Zack View Post
    Going out on a limb here, as I haven't run the code. But I'm guessing it's because you close your output file after the end of the first loop.
    Good point!

    @OP what exactly is happening. Is your code throwing an exception? Which I imagine it would be if the stream has been closed.

  5. #5
    javaguy345 is offline Member
    Join Date
    May 2011
    Posts
    15
    Rep Power
    0

    Default

    Quote Originally Posted by Zack View Post
    Going out on a limb here, as I haven't run the code. But I'm guessing it's because you close your output file after the end of the first loop.
    Haha! You're right I figured it out like 5 minutes after posting this. Cause I closed my output file outside of the loop it wasn't saving to the text file. Thanks

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    That makes no sense. If you close the stream after the loop then it most certainly will save to the file.

  7. #7
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    I think he meant that he closed it inside, but MEANT to close it outside. Freudian slip. Or Freudian typo. I don't know.

Similar Threads

  1. Problem with while loop
    By sassy1 in forum New To Java
    Replies: 12
    Last Post: 03-24-2011, 11:01 AM
  2. While loop problem
    By mochibon in forum New To Java
    Replies: 3
    Last Post: 04-18-2010, 08:21 PM
  3. Problem with the while loop
    By molokomesto in forum New To Java
    Replies: 4
    Last Post: 02-14-2010, 05:01 PM
  4. simple line problem / for loop problem
    By helpisontheway in forum New To Java
    Replies: 1
    Last Post: 11-17-2009, 06:12 AM
  5. Help with a loop-like problem
    By Jnoobs in forum New To Java
    Replies: 1
    Last Post: 10-14-2009, 01:15 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
  •