• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

sql error when connecting to mysql error

 
Joseph Rudman
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm pretty new to this so maybe be stupid question but here goes:

I have created a servlet that via an sql statement get login and password value from mysql database. I then compare this to user and param parameters entered in html login page.

I then compare the userparam and userpassord against the database information and I get the following error:
Error! Opening connection has failed: java.sql.SQLException: Before start of result set
java.sql.SQLException: Before start of result set

I cannot see what is causing this error.
I'm really struggling here, here the code causing error:
String queryEmpUser = "SELECT * FROM employees WHERE ((login = '" + userParam +"')"
+" AND (password = '"+ passwordParam +"'))";
String queryClients = "SELECT * FROM clients";

//Store username information
stmtEmpUser = connection.createStatement();
stmtClients = connection.createStatement();
rsEmpUser = stmtEmpUser.executeQuery(queryEmpUser);
ResultSetMetaData rsmdEmp = rsEmpUser.getMetaData();

Vector Clients = new Vector();
System.out.println("Query = " +queryEmpUser + "<P>");
System.out.println("userParam = " +userParam + "<P>");

//This is where error is occuring - if ( rsEmpUser.getString("login").equals(userParam) &&
rsEmpUser.getString("password").equals(passwordParam) ) {

Can someone help please?

Cheers Joe
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ResultSets start in a 'before first' position, you always have to call rs.next() before trying to access the data in the ResultSet. You should always check the result of rs.next() to make sure there is actually some thing to read.

In your case, you can edo something like this:
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is almost completely a JDBC question, so I'll move it to that forum.

Cheers, Dave.

[ March 26, 2006: Message edited by: David O'Meara ]
 
vishwanath nadimpally
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Your code should be something like
while (rs.next()) {
///Your logic

}
[ March 27, 2006: Message edited by: vishwanath nadimpally ]
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic