Meaningless Drivel is fun!
The moose likes JDBC and Relational Databases and the fly likes Help please, getting info from Database Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "Help please, getting info from Database" Watch "Help please, getting info from Database" New topic

Help please, getting info from Database

Kevin McNally

Joined: Sep 24, 2003
Posts: 6
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(){
String url = "jdbc dbc:email";

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

con=DriverManager.getConnection(url, userName,PWD );
rs=stat.executeQuery("SELECT * FROM contacts");
catch(Exception ev){
}//end catch

}//end getContacts

public void showRecord(ResultSet result){

String iD,id2="";
catch(Exception eve){
}//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.!!!
David O'Meara

Joined: Mar 06, 2001
Posts: 13459

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 '', you aren't looking at the first record. That's why almost all ResultSet loops you see will start with 'while( )'.
ps: use the code tags to format your code. It helps us help you!
Jeanne Boyarsky
author & internet detective

Joined: May 26, 2003
Posts: 32823

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.

[OCA 8 book] [Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
Kevin McNally

Joined: Sep 24, 2003
Posts: 6
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
Jason Steele
Ranch Hand

Joined: Apr 25, 2003
Posts: 100
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.

An egg is a chicken's house!
I agree. Here's the link:
subject: Help please, getting info from Database
It's not a secret anymore!