7
Lesson2
Tools:
Target:
In this lesson will build an application that loads
data from the database MyCustomers the table Customer and displays them in
a table managed by JTable component Swing.
Also performing searches on the data according to field FirstName
and Zip by means of a filter.
The application performs operations Edit, Cancellation and Inserting
records.
Programming Beans:
8: Swing: JButton NameVariable: Aggiorna Enabled: False Code for Event: ActionPerformed
try {
/* ATTENTION: CHECK DATA for code,firstname,name,date,zip,balance */
frs.absolute(TabMia.getSelectedRow()+1);
frs.updateString("code",(String)TabMia.getValueAt(TabMia.getSelectedRow(),0));
frs.updateString("firstname",(String)TabMia.getValueAt(TabMia.getSelectedRow(),1));
frs.updateString("name",(String)TabMia.getValueAt(TabMia.getSelectedRow(),2));
Date d=Date.valueOf((TabMia.getValueAt(TabMia.getSelectedRow(),3).toString()));
frs.updateDate("date",d);
frs.updateString("zip",(String)TabMia.getValueAt(TabMia.getSelectedRow(),4));
frs.updateFloat("balance",(Float)TabMia.getValueAt(TabMia.getSelectedRow(),5));
frs.setFilter(null);
frs.updateRow();
frs.acceptChanges(con);
if (fil!=null) {
frs.setFilter(fil);
frs.execute(con);
}
} catch (SQLException ex) {
ex.printStackTrace();
jOptionPane1.showMessageDialog(null,"Error: operation failed!","Error",jOptionPane1.ERROR_MESSAGE);
}
|
9:
Swing: JButton
NameVariable: Inserisci
Enabled:False
CodeforEvent:ActionPerformed
try {
frs.setCommand("INSERT INTO
Customer(code,firstname,name,date,zip,balance) values('ABB9',' ','
','2007-01-01','12340',0)");
frs.execute(con);
frs.setCommand("SELECT code,firstname,name,date,zip,balance FROM
Customer");
frs.execute(con);
} catch (SQLException ex) {
ex.printStackTrace();
jOptionPane1.showMessageDialog(null,"Error: operation
failed!","Error",jOptionPane1.ERROR_MESSAGE);
}
|
10:
Swing: JButton
NameVariable:
Cancella Enabled:False
CodeforEvent:ActionPerformed
try {
frs.absolute(TabMia.getSelectedRow()+1);
frs.setFilter(null);
frs.deleteRow();
frs.acceptChanges(con);
if (fil!=null) {
frs.setFilter(fil);
frs.execute(con);
}
frs.rowSetPopulated(new RowSetEvent(frs),1);
} catch (SQLException ex) {
ex.printStackTrace();
jOptionPane1.showMessageDialog(null,"Error: operation
failed!","Error",jOptionPane1.ERROR_MESSAGE);
}
|
Customize
FilteredRowSetPopolaActionPerformed
code event:
private void
FilteredRowSetPopolaActionPerformed(java.awt.event.ActionEvent evt) {
FilteredRowSetPopola.setEnabled(false);
try {
con=DriverManager.getConnection(url, "root", "root");
con.setAutoCommit(false);
frs=new FilteredRowSetImpl();
frs.addRowSetListener(TabMia);
frs.setCommand("SELECT
code,firstname,name,date,zip,balance FROM Customer");
frs.execute(con);
} catch (SQLException ex) {ex.printStackTrace();}
} |
private void
FilteredRowSetPopolaActionPerformed(java.awt.event.ActionEvent evt) {
FilteredRowSetPopola.setEnabled(false);
Aggiorna.setEnabled(true);
Cancella.setEnabled(true);
Inserisci.setEnabled(true);
ricercaOn.setEnabled(true);
try {
con=DriverManager.getConnection(url, "root", "root");
con.setAutoCommit(false);
frs=new FilteredRowSetImpl();
frs.addRowSetListener(TabMia);
frs.setCommand("SELECT
code,firstname,name,date,zip,balance FROM Customer");
frs.execute(con);
} catch (SQLException ex) {ex.printStackTrace();}
} |
Compile the project.
|
|