File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JDBC and the fly likes Trouble Displaying all the data Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Trouble Displaying all the data" Watch "Trouble Displaying all the data" New topic
Author

Trouble Displaying all the data

Kassi Hill
Greenhorn

Joined: Sep 29, 2003
Posts: 24
I finally was able to get a connection to my access database. It will print one row of data and stop, I know there is at least 3 lines of data to print.
If I use while(rs.next()); it does not print any of the data. If I remove the while, it will print one record. Can anyone tell me what I am doing wrong??

/**
* Testing Classes
* @version 1.0
*/
import java.sql.*;
import java.io.*;
public class DataAccess {
private final String url = "jdbc dbc:TestBank";
// private final String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
// private Connection conn;
private ResultSet rs; //This is where I am setting the variable
private Statement stmt;
public DataAccess() {
}
private void openConnection ()throws ClassNotFoundException{
//this is the method used to connect to the database using hte JDBC dbc bridge
String sql = "SELECT * FROM Question";
try{
//load the driver
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
//Connect
Connection conn = DriverManager.getConnection(url,"","");
System.out.println("Class Found");
stmt = conn.createStatement();//Create Statement Object
ResultSet rs = stmt.executeQuery(sql);
(rs.next());//only one row of data prints
// while (rs != null);{ when I do this none of the data prints
//get informatoin and load into variables
int qID = (rs.getInt("qID"));
String qText = (rs.getString("qText"));
String qType =(rs.getString("qType"));
int qCorrect = (rs.getInt("qCorrect"));;//chek for results and add them to result set.
System.out.println(qID);
System.out.println(qText);
System.out.println(qType);
System.out.println(qCorrect);
rs.close();
}catch(SQLException sqle) {
System.err.println("SQL NOT Executed: " + sqle);
}catch(java.lang.Exception ex) {
ex.printStackTrace();
}
}
public static void main(String[] args) {
DataAccess dataAccess1 = new DataAccess();
try{
dataAccess1.openConnection();
}catch(Exception sqle) {
System.err.println("SQL NOT Executed: " + sqle);
}
}
}
Sainudheen Mydeen
Ranch Hand

Joined: Aug 18, 2003
Posts: 218
Try This

-Sainudheen
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8867
    
    8

Originally posted by Kassi Hill:

(rs.next());//only one row of data prints
while (rs != null);{ when I do this none of the data prints

By putting a semicolon after a statement, you terminate it. By putting brackets around a bunch of statements, you are saying you want that group of statements to be executed as if they were one statement. A loop like while(rs != null) will execute one statement until rs is null (btw, that is a bad idea. rs.next() is the correct way to do it). By writing while(rs != null); you are saying execute a null statement until rs is null. What you want to do is execute a group of statements. The correct syntax for that is

And remember, I said (rs != null) is a bad idea.


"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8867
    
    8

Originally posted by Sainudheen Mydeen:
Try This

Don't forget to explain why. We don't want to hold people's hands for the rest of their programming careers.
Kassi Hill
Greenhorn

Joined: Sep 29, 2003
Posts: 24
Thank you very much. I works now.
Obviously, I am a newbie with JAVA and I can not tell you how greatful I am to have a place to go with questions!!!
Sainudheen Mydeen
Ranch Hand

Joined: Aug 18, 2003
Posts: 218
Thanks Joe. I will do that next time. I was in a hurry.
-Sainudheen
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8867
    
    8

Originally posted by Sainudheen Mydeen:
Thanks Joe. I will do that next time. I was in a hurry.
-Sainudheen

No worries. Just trying to reinforce the learning process.
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8867
    
    8

Originally posted by Kassi Hill:

Obviously, I am a newbie with JAVA and I can not tell you how greatful I am to have a place to go with questions!!!

That's what we're here for. Though we do expect you to do your part.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Trouble Displaying all the data