wood burning stoves 2.0*
The moose likes JDBC and the fly likes sql error when connecting to mysql error Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "sql error when connecting to mysql error" Watch "sql error when connecting to mysql error" New topic
Author

sql error when connecting to mysql error

Joseph Rudman
Greenhorn

Joined: Mar 26, 2006
Posts: 13
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

Joined: Mar 06, 2001
Posts: 13459

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

Joined: Mar 06, 2001
Posts: 13459

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

Joined: Jan 25, 2005
Posts: 116
Hi,

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

}
[ March 27, 2006: Message edited by: vishwanath nadimpally ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: sql error when connecting to mysql error