|
|||||||
|
|
|
![]() |
|
|
Strumenti |
|
|
#1 |
|
Senior Member
Iscritto dal: Jul 2007
Città: Somewhere
Messaggi: 389
|
[JAVA] Problema ricerca in database e Document Listener
Salve a tutti!
Ho un problema che non riesco a risolvere, ho provato anche a googlare e a cercare qui, ma non ho trovato niente, o, meglio, niente che risolva il mio problema :P Avrei bisogno che in un JTextField, ad ogni lettera digitata si effettui una query di ricerca e che riaggiorni la tabella. Ho fatto così, ma -ovviamente- non funziona Codice:
// metodo che esegue la query e aggiorna la tabella
public void popola(String query) {
ResultSet rs = db.eseguiQuery(query);
try {
java.sql.ResultSetMetaData rsmd = rs.getMetaData();
// definisco il modello della tabella
String[] tableColumnsName = {"Codice","Tessera","Nome","Cognome"};
DefaultTableModel aModel = (DefaultTableModel) taBBella.getModel();
aModel.setColumnIdentifiers(tableColumnsName);
// trasferimento sul modello
int colNo = rsmd.getColumnCount();
while(rs.next()){
Object[] objects = new Object[colNo];
for(int i=0;i<colNo;i++){
objects[i]=rs.getObject(i+1);
}
aModel.addRow(objects);
}
taBBella.setModel(aModel);
} catch(SQLException e) {
}
}
// il document listener sulla texfield 'cerca'
cerca.getDocument().addDocumentListener(new DocumentListener(){
public void changedUpdate(DocumentEvent e){check();}
public void removeUpdate(DocumentEvent e){check();}
public void insertUpdate(DocumentEvent e){check();}
});
// metodo 'check' che esegue il listener
public void check() {
String getcampo = " '" + cerca.getText() + "%' ";
String campo = "SELECT * FROM tabella_clienti WHERE 'nome' LIKE " + getcampo + ";"; //esempio di ricerca
popola(campo);
}
Grazie in anticipo. Saluti! |
|
|
|
|
| Strumenti | |
|
|
Tutti gli orari sono GMT +1. Ora sono le: 14:40.



















