1

I'm having a problem with a query in my java application. It's a simple query to get a users score based on their name, however, I am constantly getting an error and can't spot where the problem is coming from.

 String driver = "com.mysql.jdbc.Driver"; Class.forName(driver).newInstance(); con = DriverManager.getConnection(url, USERNAME, PASSWORD); st = con.createStatement(); PreparedStatement preStatement = con.prepareStatement("select score from playerscore where name=?"); preStatement.setString(1, "tomcat"); ResultSet resultSet = preStatement.executeQuery(); score = resultSet.getInt(1); 

Could someone cast an eye and point me in the right direction?

EDIT: Image of stack trace

http://imageshack.us/photo/my-images/854/stackr.jpg/

4
  • Could you post a stacktrace? It would be helpful. Commented Apr 14, 2012 at 14:32
  • added link to image of stacktrace Commented Apr 14, 2012 at 14:40
  • 1
    A side note: do not write .newInstance() where you have written it. Class.forName(...) is enough and this will just create an unnecessary instance of the driver. Commented Apr 14, 2012 at 14:41
  • considering the stack trace: 1) external links are not the best: maybe moved, changed, deleted, blocked, show nothing; 2) Please check the answer to the question Why should I not upload images of code/data/errors? Commented Jun 8 at 15:16

1 Answer 1

8

Initially, the ResultSet is positioned before the first row. Therefore, you must first call resultSet.next() to move to the first row, before you can retrieve any of its values.

You can verify if the first row is indeed present by checking the returned boolean of resultSet.next().

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.