Results 1 to 7 of 7
Thread: While Loop Problem
- 05-19-2011, 06:57 AM #1
Member
- Join Date
- May 2011
- Posts
- 15
- Rep Power
- 0
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.
- 05-19-2011, 07:40 AM #2
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.
- 05-19-2011, 07:41 AM #3
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.
- 05-19-2011, 07:43 AM #4
- 05-19-2011, 07:48 AM #5
Member
- Join Date
- May 2011
- Posts
- 15
- Rep Power
- 0
- 05-19-2011, 07:51 AM #6
That makes no sense. If you close the stream after the loop then it most certainly will save to the file.
- 05-19-2011, 08:43 AM #7
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
-
Problem with while loop
By sassy1 in forum New To JavaReplies: 12Last Post: 03-24-2011, 11:01 AM -
While loop problem
By mochibon in forum New To JavaReplies: 3Last Post: 04-18-2010, 08:21 PM -
Problem with the while loop
By molokomesto in forum New To JavaReplies: 4Last Post: 02-14-2010, 05:01 PM -
simple line problem / for loop problem
By helpisontheway in forum New To JavaReplies: 1Last Post: 11-17-2009, 06:12 AM -
Help with a loop-like problem
By Jnoobs in forum New To JavaReplies: 1Last Post: 10-14-2009, 01:15 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks