Com certeza
Mas nesse caso tem que ser "select campo1, campo2, campoN from etc", os campos tem que ser definidos.
Um exemplo guardando os dados em uma lista de Map:
package testes;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Classe com altas mensagens subliminares
*/
public class BigMacDAO
{
private Connection conn;
public BigMacDAO(Connection conn)
{
this.conn = conn;
}
private void fecha(PreparedStatement ps, ResultSet rs)
throws SQLException
{
if (ps != null)
ps.close();
if (rs != null)
rs.close();
}
private Map<String, Object> montaBigMac(ResultSet rs)
throws SQLException
{
Map <String, Object> bigMac = new HashMap<String, Object>();
// Tudo é Long pq tudo é quantidade ;)
bigMac.put("hamburguer", rs.getLong(1)); // os numeros são
bigMac.put("tomate", rs.getLong(2)); // a posição da
bigMac.put("queijo", rs.getLong(3)); // coluna no sql
bigMac.put("molhoespecial", rs.getLong(4));
bigMac.put("cebola", rs.getLong(4));
bigMac.put("pickles", rs.getLong(5));
bigMac.put("pãogergelim", rs.getLong(6));
return bigMac;
}
public List<Map<String, Object>> buscaPorQtdHamburguer(int qtd)
throws SQLException
{
StringBuffer sql = new StringBuffer()
.append("select ")
.append(" hamburguer, ")
.append(" tomate, ")
.append(" queijo, ")
.append(" molho_especial, ")
.append(" cebola, ")
.append(" pickles, ")
.append(" pao_gergelim ")
.append("from mcd_bigmac ")
.append("where hamburguer = ? ");
PreparedStatement ps = null;
ResultSet rs = null;
try
{
ps = conn.prepareStatement(sql.toString());
ps.setLong(1, qtd); // coloca a qtd no lugar de ? na SQL
rs = ps.executeQuery();
// declaramos nossa lista
List<Map<String, Object>> lista = new ArrayList<Map<String, Object>>();
while(rs.next())
// monta os BigMacs retornados pelo select e coloca cada
// resultado em uma lista.
lista.add(montaBigMac(rs));
// Use lista.size() para saber qtos elementos tem dentro dela
// Pode ter de 0 à N. Obs: a lista nunca vai ser nula.
return lista;
}
finally
{
// Lembre-se de sempre fechar estes objetos.
// A conexão será fechada a em classes mais abaixo no stack.
fecha(ps, rs);
}
}
}
Agora, se o teu sistema começar a crescer(mais que 5 tabelas, mais de 2 consultas por DAO), use um framework de persistência que faça o trabalho p/ você. Hibernate é o melhor deles.