Languages

Spring Boot 2 - JdbcTemplate throws: java.sql.SQLException: Before start of result set

4 points
Asked by:
Abel-Burks
758

I have trouble uring JdbcTemplate query() function getting single result from ResultSet.

This is exception I got:

java.sql.SQLException: Before start of result set

This is stack trace:

java.sql.SQLException: Before start of result set
	at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:129) ~[mysql-connector-java-8.0.22.jar:8.0.22]
	at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97) ~[mysql-connector-java-8.0.22.jar:8.0.22]
	at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:89) ~[mysql-connector-java-8.0.22.jar:8.0.22]
	at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:63) ~[mysql-connector-java-8.0.22.jar:8.0.22]
	at com.mysql.cj.jdbc.result.ResultSetImpl.checkRowPos(ResultSetImpl.java:492) ~[mysql-connector-java-8.0.22.jar:8.0.22]
	at com.mysql.cj.jdbc.result.ResultSetImpl.getObject(ResultSetImpl.java:1299) ~[mysql-connector-java-8.0.22.jar:8.0.22]
	at com.mysql.cj.jdbc.result.ResultSetImpl.getLong(ResultSetImpl.java:823) ~[mysql-connector-java-8.0.22.jar:8.0.22]
	at com.zaxxer.hikari.pool.HikariProxyResultSet.getLong(HikariProxyResultSet.java) ~[HikariCP-4.0.3.jar:?]
	at chat.MessagesService.lambda$fetchMessages$1(MessagesService.java:215) ~[classes/:?]
	at org.springframework.jdbc.core.JdbcTemplate$1QueryStatementCallback.doInStatement(JdbcTemplate.java:453) ~[spring-jdbc-5.3.13.jar:5.3.13]
	at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:381) ~[spring-jdbc-5.3.13.jar:5.3.13]
	at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:465) ~[spring-jdbc-5.3.13.jar:5.3.13]
	...

SQL query I used:

SELECT COUNT(*)
FROM `messages` m
WHERE (m.`removal_time` IS NULL)

My java source code:

// import org.springframework.jdbc.core.JdbcTemplate;

// @Autowire
// private JdbcTemplate jdbcTemplate;

public Long getCount() {
    String sqlQuery = "SELECT COUNT(*)                 \n" +
                      "FROM `messages` m               \n" +
                      "WHERE (m.`removal_time` IS NULL)\n";

    return this.jdbcTemplate.query(sqlQuery, (resultSet) -> (Long) resultSet.getLong(1));
}
1 answer
5 points
Answered by:
Abel-Burks
758

In the above case you need to call resultSet.next() function before calling resultSet.getLong(1) function.

Fixed source code:

public Long getCount() {
    String sqlQuery = "SELECT COUNT(*)                 \n" +
                      "FROM `messages` m               \n" +
                      "WHERE (m.`removal_time` IS NULL)\n";

    long rowsCount = this.jdbcTemplate.query(sqlQuery, (resultSet) -> {
        if (resultSet.next()) {
            return resultSet.getLong(1);
        }
        return 0L;
    });

    return rowsCount;
}
0 comments Add comment
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join