1
I need to do two iterations on the same ResultSet
. For that I did the following:
I set parameters in a Statement
to make the cursor editable, according to my connection class constructor:
public ConexaoComDb(){
try {
Class.forName("org.h2.Driver");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
cn = DriverManager.getConnection("jdbc:h2:./lib/BDContaNoBB", "sa", "");
stmt = cn.createStatement(
ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_UPDATABLE);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Then I make one while(resultSet.next())
. After that, I get the result
with the first()
. In a following method I try to iterate again with the resultSet.next()
, but instead of showing the complete table with the print, what appears is twice the first line, as if there had been no iteration in either of the two while
s.
Below follows the code of the method that performs the query, generating content for the resultSet
, and the method that has the first while (rs.next())
:
public void executaBusca(String nomeTable, String colunas, String condicao){
try {
rs = stmt.executeQuery("SELECT " +colunas+ " FROM " +nomeTable+
" "+condicao);
imprimeConsulta(rs);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
First while (rs.next())
:
public void imprimeConsulta(ResultSet result){
try {
metaDadosDoRs = result.getMetaData();
numeroColunas = metaDadosDoRs.getColumnCount();
int greaterCel;
for (int coluna=1; coluna<=numeroColunas; coluna++){
greaterCel= tamanhoDaMaiorCelula(coluna);
formatarCelulas(greaterCel, coluna);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
According to while(rs.next())
:
public int tamanhoDaMaiorCelula(int coluna){
int TamConteudoDaAtual=0;
int TamConteudoDaAnterior=0;
int indiceMaiorCel=0;
int tamMaiorCel=0;
String conteudo;
try {
while(rs.next()){
conteudo = rs.getString(coluna);
TamConteudoDaAtual = conteudo.length();
if (TamConteudoDaAtual > TamConteudoDaAnterior){
indiceMaiorCel = TamConteudoDaAtual;
tamMaiorCel = TamConteudoDaAtual;
}
TamConteudoDaAnterior = TamConteudoDaAtual;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
formatarCelulas(tamMaiorCel, coluna);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return tamMaiorCel;
}
And finally, the method that prints the formatted table. That was to print all of it, which has 5 lines, but only prints twice the contents of the first line...
public void formatarCelulas(int tamMaiorCel, int coluna) throws SQLException{
rs.first();
ArrayList<String> stringFormatada = new ArrayList<String>();
do {
int tamQueFalta = rs.getString(coluna).length() - tamMaiorCel;
char[] arrayDoTamQueFalta = new char[tamQueFalta];
String tamLast = new String (arrayDoTamQueFalta);
stringFormatada.add(rs.getString(coluna));
stringFormatada.add(tamLast);
System.out.println(stringFormatada.toString());
stringFormatada.clear();
}while(rs.next());
}
You could post the class
ConexaoComDb
whole, to make it easier to analyze and change the code? Also, in what context do you use the methodexecutaBusca
and with what parameters? I am writing an answer and your code has several problems, but if you give me this information, I can elaborate a more precise and detailed answer.– Victor Stafusa