• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Trouble Displaying all the data

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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);
}
}
}
 
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try This

-Sainudheen
 
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Joe Ess
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 218
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Joe. I will do that next time. I was in a hurry.
-Sainudheen
 
Joe Ess
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic