Se ho ben compreso il tuo problema, una soluzione potrebbe essere questa (non è la più elegante).
Per prima cosa creiamo due POJO che rispecchiano le due tabelle.
POJO per la tabella PDF
codice:
public class Pdf
{
private int id;
private String nome;
private Date date;
private List<PdfContent> contents;
public Pdf(int id, String nome, Date date)
{
this.id = id;
this.nome = nome;
this.date = date;
this.contents = new ArrayList<PdfContent>();
}
/**
GETTER AND SETTER OMESSI
**/
@Override
public int hashCode()
{
final int prime = 31;
int result = 1;
result = prime * result + ((date == null) ? 0 : date.hashCode());
result = prime * result + id;
result = prime * result + ((nome == null) ? 0 : nome.hashCode());
return result;
}
@Override
public boolean equals(Object obj)
{
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Pdf other = (Pdf) obj;
if (date == null)
{
if (other.date != null)
return false;
}
else if (!date.equals(other.date))
return false;
if (id != other.id)
return false;
if (nome == null)
{
if (other.nome != null)
return false;
}
else if (!nome.equals(other.nome))
return false;
return true;
}
@Override
public String toString()
{
return "Pdf [id=" + id + ", nome=" + nome + ", date=" + date + ", contents=" + contents + "]";
}
}
Pojo per la tabella del contenuto del Pdf
codice:
public class PdfContent
{
private int id;
private int idPdf;
private int numeroPagina;
private String testo;
public PdfContent(int id, int idPdf, int numeroPagina, String testo)
{
this.id = id;
this.idPdf = idPdf;
this.numeroPagina = numeroPagina;
this.testo = testo;
}
/**
GETTER AND SETTER OMESSI
**/
@Override
public int hashCode()
{
final int prime = 31;
int result = 1;
result = prime * result + id;
result = prime * result + idPdf;
result = prime * result + numeroPagina;
result = prime * result + ((testo == null) ? 0 : testo.hashCode());
return result;
}
@Override
public boolean equals(Object obj)
{
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
PdfContent other = (PdfContent) obj;
if (id != other.id)
return false;
if (idPdf != other.idPdf)
return false;
if (numeroPagina != other.numeroPagina)
return false;
if (testo == null)
{
if (other.testo != null)
return false;
}
else if (!testo.equals(other.testo))
return false;
return true;
}
@Override
public String toString()
{
return "PdfContent [id=" + id + ", idPdf=" + idPdf + ", numeroPagina=" + numeroPagina + ", testo=" + testo + "]";
}
}
Come puoi notare all'interno dell'oggetto PDF ho dichiarato una lista di PdfContent che sarà popolata con le righe della tabella PDF_CONTENT.
Il metodo per l'esecuzione della query diventa questo:
codice:
public Vector<Pdf> queryRicerca(String query)
{
Statement statement;
try
{
statement = con.createStatement();
ResultSet result = statement.executeQuery(query);
Vector<Pdf> vector = new Vector<Pdf>();
int tmpIdPdf = -1;
Pdf pdf = null;
while (result.next())
{
int pdfId = result.getInt(1);// id del pdf
if (tmpIdPdf != pdfId)
{
if (pdf != null)
{
vector.add(pdf);
}
pdf = new Pdf(result.getInt(1), result.getString(2), result.getDate(3));
tmpIdPdf = pdfId;
}
PdfContent content = new PdfContent(result.getInt(4), result.getInt(5), result.getInt(6), result.getString(7));
pdf.getContents().add(content);
}
if (tmpIdPdf == pdf.getId())
{
vector.add(pdf);
}
return vector;
}
catch (SQLException e)
{
throw new RuntimeException("Errore durante l'esecuzione della query", e);
}
}
Nota bene l'uso dei generics (Vector<Pdf>)
e di conseguenza il metodo per il test
codice:
public static void main(String[] args) throws ClassNotFoundException, SQLException
{
System.out.println("Mi connettto");
Connessione c = new Connessione();
String nome = "TestConnessione";
System.out.println("Eseguo query con ritorno");
// CHIAMO LA PRIMA TABELLA PDF(ID,NOME,DATA)
// CHIAMO LA SECONDA TABELLA PDF_CONTENT(ID,ID_PDF,N_PAGINA,TESTO);
Vector<Pdf> vector = c
.queryRicerca("SELECT P.ID,P.NOME,P.DATA,C.ID,C.ID_PDF,C.N_PAGINA,C.TESTO FROM PDF P, PDF_CONTENT C where P.ID = C.ID_PDF AND P.NOME = '"
+ nome + "'");
for (Pdf pdf : vector)
{
System.out.println(pdf);
}
System.out.println("mi disconnetto");
c.chiudi();
System.out.println("Sono diconnesso");
}
Ciao