Quando tento compilar aparece a seguinte erro de Exception: After end of result set. Alguém poderia me ajudar?
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class CategoriaDAO {
private Connection connection;
public CategoriaDAO(Connection connection) {
this.connection = connection;
}
public List<Categoria> listar() throws SQLException {
List<Categoria> categorias = new ArrayList<>();
System.out.println("Executando a query de listar categoria");
String sql = "SELECT ID, NOME FROM CATEGORIA";
try (PreparedStatement pstm = connection.prepareStatement(sql)) {
pstm.execute();
try (ResultSet rst = pstm.getResultSet()) {
while (rst.next()) {
Categoria categoria = new Categoria(rst.getInt(1), rst.getString(2));
categorias.add(categoria);
}
}
return categorias;
}
}
public List<Categoria> listarComProdutos() throws SQLException {
Categoria ultima = null;
List<Categoria> categorias = new ArrayList<>();
System.out.println("Executando a query de listar categoria");
String sql = "SELECT C.ID, C.NOME, P.ID, P.NOME, P.DESCRICACAO FROM CATEGORIA C INNER JOIN"
+ " PRODUTO P ON C.ID = P.CATEGORIA_ID"
+ " ORDER BY P.CATEGORIA_ID";
try (PreparedStatement pstm = connection.prepareStatement(sql)) {
pstm.execute();
try (ResultSet rst = pstm.getResultSet()) {
while (rst.next()) {
if (ultima == null || !ultima.getNome().equals(rst.getString(2))) {
}
Categoria categoria = new Categoria(rst.getInt(1), rst.getString(2));
ultima = categoria;
categorias.add(categoria);
}
Produto produto = new Produto(rst.getInt(3), rst.getString(4), rst.getString(5));
ultima.adicionar(produto);
}
}
return categorias;
}
}
Stack Trace:
jun. 14, 2021 10:22:32 PM com.mchange.v2.log.MLog
INFO: MLog clients using java 1.4+ standard logging.
jun. 14, 2021 10:22:32 PM com.mchange.v2.c3p0.C3P0Registry
INFO: Initializing c3p0-0.9.5.4 [built 23-March-2019 23:00:48 -0700; debug? true; trace: 10]
jun. 14, 2021 10:22:32 PM com.mchange.v2.c3p0.impl.AbstractPoolBackedDataSource
INFO: Initializing c3p0 pool... com.mchange.v2.c3p0.ComboPooledDataSource [ acquireIncrement -> 3, acquireRetryAttempts -> 30, acquireRetryDelay -> 1000, autoCommitOnClose -> false, automaticTestTable -> null, breakAfterAcquireFailure -> false, checkoutTimeout -> 0, connectionCustomizerClassName -> null, connectionTesterClassName -> com.mchange.v2.c3p0.impl.DefaultConnectionTester, contextClassLoaderSource -> caller, dataSourceName -> 1br9pflai3oh99cjalmjb|16022d9d, debugUnreturnedConnectionStackTraces -> false, description -> null, driverClass -> null, extensions -> {}, factoryClassLocation -> null, forceIgnoreUnresolvedTransactions -> false, forceSynchronousCheckins -> false, forceUseNamedDriverClass -> false, identityToken -> 1br9pflai3oh99cjalmjb|16022d9d, idleConnectionTestPeriod -> 0, initialPoolSize -> 3, jdbcUrl -> jdbc:mysql://localhost/loja_virtual1?useTimezone=true&serverTimezone=UTC, maxAdministrativeTaskTime -> 0, maxConnectionAge -> 0, maxIdleTime -> 0, maxIdleTimeExcessConnections -> 0, maxPoolSize -> 15, maxStatements -> 0, maxStatementsPerConnection -> 0, minPoolSize -> 3, numHelperThreads -> 3, preferredTestQuery -> null, privilegeSpawnedThreads -> false, properties -> {password=******, user=******}, propertyCycle -> 0, statementCacheNumDeferredCloseThreads -> 0, testConnectionOnCheckin -> false, testConnectionOnCheckout -> false, unreturnedConnectionTimeout -> 0, userOverrides -> {}, usesTraditionalReflectiveProxies -> false ]
Executando a query de listar categoria
Exception in thread "main" java.sql.SQLException: After end of result set
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:129)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:89)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:63)
at com.mysql.cj.jdbc.result.ResultSetImpl.checkRowPos(ResultSetImpl.java:484)
at com.mysql.cj.jdbc.result.ResultSetImpl.getObject(ResultSetImpl.java:1283)
at com.mysql.cj.jdbc.result.ResultSetImpl.getInt(ResultSetImpl.java:786)
at com.mchange.v2.c3p0.impl.NewProxyResultSet.getInt(NewProxyResultSet.java:425)
at CategoriaDAO.listarComProdutos(CategoriaDAO.java:60)
at TestaListagemDeCategorias.main(TestaListagemDeCategorias.java:11)