Is it possible to make two Selects in a Try?

Asked

Viewed 81 times

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’ll get the idHistorico and the idProduto, but I also wanted to get the name and price, which is in the other table produtos. I can add to the try another Select?

  • Yes, you can do.

2 answers

1

Yes, you can put several queries in the same Ry, and specifically in this case you are using for the catch the generic Exception, you can use other methods that fire a throw, surrounded in the same Ry, the only thing that would require you to create another catch, would not be a new Ry, but a new catch, as the example below, would be if your first Exception did not contemplate the defendant for the second, for specific Try/catch as if by chance you used a Sqlexception in the first catch and an Ioexception for example, then I would have to declare it as follows:

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

}
catch(IOException ioe){
}

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

0

Wouldn’t it be easier for you to use the JOIN to get this data and return to your object? You could do so

 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 you want to recover your data after you fill your resultset with that return.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.