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 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
Bookmark "Help please, getting info from Database" Watch "Help please, getting info from Database" New topic
Author

Help please, getting info from Database

Kevin McNally
Greenhorn

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(){
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.!!!
David O'Meara
Rancher

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 '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!
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30116
    
150

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.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Kevin McNally
Greenhorn

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
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.


An egg is a chicken's house!
 
jQuery in Action, 2nd edition
 
subject: Help please, getting info from Database
 
Similar Threads
Please help debug this program
JApplet not connecting to SQL database
Need help with insert values into access
else loop not working
Connection with database Help