Select from two tables. How to return a single list?

Asked

Viewed 451 times

1

I have a Processes table, with basic data and another Processes highlight, with more specific data. I need to make a select that calls some columns of the basic data and others of the specific data. I tried to make a list, but I can only call the data from one table.

In the first Processes table, I have the following columns: codGeral,classeProc, numProc, autorProc, ResumoProc.

The Processes table features the following columns: codDest, codGeral(chave estrangeira), ementaProc, dispProc.

The list method looks like this:

public List listarDest()
{
List lst= new ArrayList<>();

List lstDest = newArrayList<>();
try
{
stmt = con.prepareStatement(“SELECT codigoGeral.Processos, classe.Processos, numProc.Processos, autor.Processos, codDest.ProcessosDestaques, ementaProc.ProcessosDestaque FROM ProcessosDestaque JOIN Processos ON ProcessosDestaque.codGeral = Processos.codGeral”);

rs = stmt.executeQuery();

while(rs.next())

{

ProcDestaques procdestaques = new ProcDestaques();

Proc proc = new Proc();

procdestaques.setCodDest(rs.getInt(1));

procdestaques.setCodGeral(rs.getInt(2));

proc.setClasse(rs.getString(3));

proc. setNumProc(rs.getString(4));

proc.setAutor(rs.getString(5));

procdestaques.setEmentaProc(rs.getString(6));

lstDest.add(procdestaques);

lst.add(proc);

}

stmt.execute();

rs.close();

con.close();

}

catch (SQLException e)

{

throw new RuntimeException(e);

}

return lstDest;

}

Does anyone have any idea how to select to return the data from the two tables?

  • Amigo I saw in your sql that you are putting the name of the field and then the name of the table the correct one is the name of the table after the field. Tenta executa assim :"SELECT Processos.codigoGeral, Processos.classe, Processos.numProc, Processos.autor, ProcessosDestaques.codDest, ProcessosDestaque.ementaProc FROM ProcessosDestaque JOIN Processos ON ProcessosDestaque.codGeral = Processos.codGeral"

  • It depends on how you want the result. You can use JOINS, or UNION ALL. In your case that has a connection between them, the ideal would be JOIN as you did. To generate the table, for ease, generate an array, and then deal with 1 loop within another.

1 answer

0

Friend you can use query native, and the query would be more or less:

SELECT processo.codGeral, processo.classe, processo.numProc, processo.autor, detalhe.codDest,  processo.numProc FROM Processos processo, ProcessosDestaque detalhe WHERE processo.codGeral = detalhe.codGeral

I hope I’ve helped.

Browser other questions tagged

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