Is it possible to make two Selects in a Try?

1
try {
    Connection lig = DriverManager.getConnection("jdbc:mysql://localhost/gym", "root", "0000");
    PreparedStatement inst = lig.prepareStatement("SELECT * FROM produtos_has_historico WHERE Produtos_idProdutos AND Historico_idHistorico");

    ResultSet a = inst.executeQuery();
    while (a.next()){
        DefaultListModel model = new DefaultListModel();
        model.addElement(a);
    }               
} catch(Exception e){}

I'm going to get idHistorico and idProduto , but I also wanted to get the name and price, which is in the other produtos table. Can I add to try other Select ?

    
asked by anonymous 25.05.2015 / 12:16

2 answers

1

Yes, you can put multiple queries in the same try, and specifically in this case you're using for the generic catch exception, you can use other methods that trigger a throw, circled in the same try, the only thing that would force you to create another catch, it would not be a new try, but a new catch, as in the example below, would be if your first exception did not include the required one for the second, in the case of a specific try / catch as if by chance you used a SQLException in the first catch and an IOException for example, there would have to declare it as follows:

try{
 //código
}catch( SQLException sqle){

}
catch(IOException ioe){
}

But since your code uses the parent Exception, you can declare any code within the try, which will trigger the same exception for any error. The only misfortune you will have will be to selectively process the exception or intercept it, which I believe is not your case.

    
25.05.2015 / 14:24
0

Would not it be easier for you to use JOIN to get this data back to your object? You could do it that way

 public List<Status> pesquisarComboAtendimento(int idPerfil) throws SQLException, NamingException
{
ArrayList<Status> listaSelect = new ArrayList<Status>();

listaSelect.add(new Status(0, "Selecione"));

try (Connection con = pool.getConnection(); PreparedStatement pstmt = con.prepareStatement("select s.* from TBPERFIL_STA ps inner join TBSTATUS s on s.ID_STATUS = ps.ID_STATUS and s.ST_NOVA = 0 and s.st_conclui = 0 and s.st_aprova = 0 and (not exists (select * from tbdpto dpto where dpto.sta_atend = s.id_status)) where ps.ID_PERFIL = ?");)
{

    pstmt.setInt(1, idPerfil);

    try (ResultSet rs = pstmt.executeQuery(); WebRowSet webRowSet = new WebRowSetImpl();)
    {
    webRowSet.populate(rs);
    while (webRowSet.next())
    {
        Status status = new Status(webRowSet.getInt("id_status"), webRowSet.getString("ds_status"), webRowSet.getBoolean("st_nova"), webRowSet.getBoolean("st_aprova"), webRowSet.getBoolean("st_conclui"));
        listaSelect.add(status);
    }
    }
    listaSelect.trimToSize();
}

return listaSelect;
}

I have this example to illustrate what you could do, above you perform a join on the tables that you want to retrieve your data then fills your resultSet with that return.

    
25.05.2015 / 14:44