I´m new to Java programming, and I am migrating an application from C # to Java EE. The problem is this, I run a query through a stored procedure that returns the paginated result, for example, imagine that I search for the name John in the database. The result of the query might return 10,000 John's, but I want to show only 25 John's. So my stored procedure returns two results: first the amount of John's found, and the second 25 John's selected (pagging).
In my C # code is as follows:
// Execute a stored procedure.
DataSet dataSet = banco.ExecuteDataSet(command);
if (dataSet != null)
{
// Check if something was retorned.
if (dataSet.Tables.Count == 2)
{
// Get the total of records found by query.
_totRegistros = (int)dataSet.Tables[0].Rows[0][0];
// Lê os dados da segunda tabela, que é a que contém os
// registros e preenche a página com os registros de detalhe.
bool impar = true;
foreach (DataRow row in dataSet.Tables[1].Rows)
{
// Salva os dados no registro da classe.
ReadDataRow(row);
// Inclui os registros de detalhe no formulário.
listagem += PrintLinGrid(impar);
impar = !impar;
}
}
else
{
_temErro = true;
_msgUsuario = "Nenhum registro encontrado.";
}
// Libera área de memória alocada pelo DataSet.
if (dataSet != null)
{
dataSet.Dispose();
dataSet = null;
}
}
And here, my code in Java
Connection con = Conexao.GetConnection();
if (con != null) {
// Create statement do execute stored procedure.
CallableStatement stmt = null;
try {
// Insert de statament with stored procedure.
stmt = con.prepareCall("{call BackOffice_Usuario_QueryPaginada(?,?,?,?,?,?,?)}",
ResultSet.TYPE_FORWARD_ONLY,
ResultSet.CONCUR_READ_ONLY);
// Add parameters to stored procedure.
stmt.setString(1, "UsuarioId, UsuarioLojistaId, UsuarioStatusId, UsuarioTipoId, UsuarioNome, UsuarioCargo, UsuarioDepto, UsuarioCentroCusto, UsuarioLogin, UsuarioDataCadastro, UsuarioCriadoPorUsuarioId, UsuarioDataUltimoAcesso, UsuarioUltimaSessao, UsuarioAcessosAcumulados");
stmt.setString(2, "Usuarios");
stmt.setString(3, "1 = 1"); // Condition to teste
stmt.setString(4, "UsuarioNome");
stmt.setString(5, "ASC");
stmt.setInt(6, 1);
stmt.setInt(7, 25);
// Execute stored procedure.
ResultSet resultado = null;
try {
resultado = stmt.executeQuery();
// This point is my problem, how can I do to translate
// _totRegistros = (int)dataSet.Tables[0].Rows[0][0]; and
// foreach (DataRow row in dataSet.Tables[1].Rows) to java sintax?
int x = 0;
while(resultado.next()) {
x++;
System.out.println(x);
resultado.getRow();
}
} catch (SQLException e) {
_temErro = true;
_msgUsuario = e.getMessage();
}