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
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()
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.