Olá!!
Tenho um problema. Estou a “tentar” ler um result set para um array de strings mas dá-me erro.
O meu código é o seguinte:
ps_aux=c.prepareStatement("SELECT Nome FROM "+tab_aux+" WHERE (Nome IS NOT NULL)");System.out.println("SELECT Nome FROM "+tab_aux+" WHERE (Nome IS NOT NULL)");rs_aux=ps_aux.executeQuery();//rs_aux.next();//BEACAUSETheresultsetwillalwayscometoyoupositionedbeforethefirstrow...while(rs_aux.next()){for(inti=0;rs_aux.last();i++){//NewobjectstoresList[i]=rs_aux.getString("Nome");System.out.println("Nome");}}rs_aux.close();ps_aux.close();c.close();
Que erro acontece? Uma pergunta, pq vc ta usando aquele for dentro do while?
thiago.correa
Cláudia,
sempre que for postar algum problema, tente postar o erro que está dando.
Bom, vamos lá, o erro pode estar sendo ocasiondado pelo seguinte, você está atribuindo mais elementos do que o teu array pode suportar, uma dica, você pode usar ArrayList, este manipula de forma dinâmica o seu tamnhao enquanto que o array é estático.
...
List<String> lista = new ArrayList<String>();
while(rs_aux.next()){
// New object
lista.add(rs_aux.getString("Nome"));
}
...
Claudia.pt
Bem o erro é o seguinte:
java.sql.SQLException:[Microsoft][SQLServer 2000 Driver for JDBC]Unsupportedmethod:ResultSet.last
Ok. Sei que posso usar um ArrayList mas queria percebre cmo se faz com um STring[]…
Claudia
thiago.correa
Bom, primeiramente você teria que saber o tamanho do array.
Supondo que o limite máximo de registros que irá voltar do banco é 100 você poderia fazer assim
ps_aux = c.prepareStatement("SELECT Nome FROM " + tab_aux + " WHERE (Nome IS NOT NULL)");
System.out.println("SELECT Nome FROM " + tab_aux + " WHERE (Nome IS NOT NULL)");
rs_aux = ps_aux.executeQuery();
String[] storesList = new String[100];
int i = 0;
while(rs_aux.next()) {
storesList [i++] = rs_aux.getString("Nome");
System.out.println("Nome");
}
Claudia.pt
Só pra confirmar…
Não dá mesmo se eu à partida não souber o tamanho do result set? E tb não existe nada do género:
rs.size();
Cláudia
L
Lucashgt
Pode fazer da seguinte maneira também, usando o mesmo exemplo do Thiago.
ps_aux=c.prepareStatement("SELECT Nome FROM "+tab_aux+" WHERE (Nome IS NOT NULL)");System.out.println("SELECT Nome FROM "+tab_aux+" WHERE (Nome IS NOT NULL)");rs_aux=ps_aux.executeQuery();String[]storesList=newString[rs_aux.size];// Note que tu podes utilizar o size do rs_aux para definir o tamanho do array. ;-)inti=0;while(rs_aux.next()){storesList[i++]=rs_aux.getString("Nome");System.out.println("Nome");}
thiago.correa
Eu tinha procurado o método ‘size’ no javadoc mas não havia encontrado
Bom Cláudia, acho que isso resolve o teu problema e é bem genérico.