• 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

Help please, getting info from Database

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok i have this method to get names from a database so i can put them in a combo box later. Here is my code for the method:

public void getContacts(){
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String url = "jdbc dbc:email";

String userName="";
String PWD="";

con=DriverManager.getConnection(url, userName,PWD );
stat=con.createStatement();
rs=stat.executeQuery("SELECT * FROM contacts");
}
catch(Exception ev){
JOptionPane.showMessageDialog(null,""+ev.getMessage());
}//end catch
showRecord(rs);

}//end getContacts

public void showRecord(ResultSet result){
try{
do{

String iD,id2="";
iD=rs.getString(1);
if(iD==id2){
break;
}
names[i]=rs.getString(2);
id2=iD;
System.out.println(names[i]);
rs.next();
i=i++;
}while(i<=0);
}
catch(Exception eve){
JOptionPane.showMessageDialog(null,""+eve.getMessage());
}//end catch
}//end resultset

------------
Ok thats it.. now i know the database is fine and everything since i have connected to it before. The error i get when i try runing this is "Invalid cursor state" which i have no idea what that is.
So can i please get some urgent help on this?
I know the loop is pretty wierd lol , is there a better way? What i have is a database table with and ID for first coloumn and Firstname for second colom. I want it to add all the names in that table to a string variable names[] and then stop when its all done.. this is the only way i see it working but im sure there is a better way. Thanks alot.. and the sooner the help the better.!!!
 
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Firstly, passing ResultSets around is pretty bad form. You're better off converting from your database representation to an Object representation as quickly as possible, and then doing Java-only stuff from that point on.
The problem you are having is that the ResultSet cursor starts at a 'before first' position. That is, until you call 'rs.next()', you aren't looking at the first record. That's why almost all ResultSet loops you see will start with 'while( rs.next() )'.
Dave
ps: use the code tags to format your code. It helps us help you!
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Kevin,
I'm not sure what's causing the problem you are getting, but here's the standard form of looping through a result set:

Also, it is good practice to close the resultset, statement and connection objects.
 
Kevin McNally
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok thx, i found were my problem really lies (other then the part you guys helped me fix). my aray is messed up..hmm Can u please tell me how to make an array and declare it so it can carry an infinite amount.. all i do is go
String names[]; but theres more right? plz show me =] thx
 
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Kevin,
Glad you got it fixed...
If you want a dynamic array, you can use ArrayList. It will allow your data to grow. Also, you can easily convert it to a standard array if needed.
 
reply
    Bookmark Topic Watch Topic
  • New Topic