qui c'è la parte finale della swing e la classe di dominio e la classe dao...
codice:javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 657, Short.MAX_VALUE) ); pack(); }// </editor-fold> private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: } private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: } private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: } private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: } private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: } private void jSpinner1KeyPressed(java.awt.event.KeyEvent evt) { } private void jSpinner1InputMethodTextChanged(java.awt.event.InputMethodEvent evt) { // TODO add your handling code here: } /** * @param args the command line arguments */ public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { CorsoSwing dialog = new CorsoSwing(new javax.swing.JFrame(), true); dialog.addWindowListener(new java.awt.event.WindowAdapter() { public void windowClosing(java.awt.event.WindowEvent e) { System.exit(0); } }); dialog.setVisible(true); } }); } // Variables declaration - do not modify private javax.swing.JButton jButton1; private javax.swing.JButton jButton2; private javax.swing.JButton jButton3; private javax.swing.JButton jButton4; private javax.swing.JButton jButton5; private javax.swing.JComboBox jComboBox1; private com.toedter.calendar.JDateChooser jDateChooser1; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel2; private javax.swing.JLabel jLabel3; private javax.swing.JLabel jLabel4; private javax.swing.JLabel jLabel5; private javax.swing.JLabel jLabel6; private javax.swing.JLabel jLabel7; private javax.swing.JLabel jLabel9; private javax.swing.JPanel jPanel1; private javax.swing.JRadioButton jRadioButton1; private javax.swing.JRadioButton jRadioButton2; private javax.swing.JSpinner jSpinner1; private javax.swing.JSpinner jSpinner2; private javax.swing.JTextField jTextField1; // End of variables declaration }codice:package DomainClasses; import java.sql.Date; import java.sql.Time; import dac.dao.ClienteDAO; import dac.dao.CorsoDAO; public class Corso { private final int codice; private String nome; private Date data; private Time ora; private Istruttore istruttore; private Prenotazione prenotazione; private int oldCodice; private String oldNome; private Date oldData; private Time oldOra; private Istruttore oldIstruttore; private Prenotazione oldPrenotazione; public Corso(int codice) { this.codice = codice; } public String getNome() { return nome; } public void setNome(String nome) { this.nome = nome; } public Date getData() { return data; } public void setData(Date data) { this.oldData = this.data; this.data = data; } public Time getOra() { return ora; } public void setOra(Time ora) { this.oldOra = this.ora; this.ora = ora; } public Istruttore getIstruttore() { return istruttore; } public void setIstruttore(Istruttore istruttore) { this.oldIstruttore = this.istruttore; this.istruttore = istruttore; } public Prenotazione getPrenotazione() { return prenotazione; } public void setPrenotazione(Prenotazione prenotazione) { this.oldPrenotazione = this.prenotazione; this.prenotazione = prenotazione; } public int getCodice() { return codice; } public boolean equals(Object o) { if (o == null) return false; if (!(o instanceof Corso)) return false; Corso o1 = (Corso) o; return this.codice == o1.codice; } public int hashCode() { return codice; } public String toString() { return codice + nome.toString() + data.toString() + ora.toString() + istruttore.toString() + prenotazione.toString(); } public int getOldCodice() { return oldCodice; } public String getOldNome() { return oldNome; } public Date getOldData() { return oldData; } public Time getOldOra() { return oldOra; } public Istruttore getOldIstruttore() { return oldIstruttore; } public Prenotazione getOldPrenotazione() { return oldPrenotazione; } public void leggiDatidaDB() throws Exception { CorsoDAO.load(this); } public void inserisciDatisuDB() throws Exception { CorsoDAO.insert(this); } public void aggiornaDatisuDB() throws Exception { CorsoDAO.update(this); } public void cancellaDatidaDB() throws Exception { CorsoDAO.delete(this); } }codice:package dac.dao; import DomainClasses.*; import java.util.*; import java.sql.*; import utilityClasses.MyException; import dac.ConnectionManager; /* Data Access Object per l�entita� Cliente * Incapsula le funzioni ed i tipi dato necessari * per manipolare le informazioni * della base dati pertinenti a detta entita�. * Si tratta di una utility class * non istanziabile. */ public class CorsoDAO { private CorsoDAO() { } // Comando SQL per l�inserimento di una nuova istanza private static final String INSERT_SQL = "INSERT INTO Corso VALUES (?, ?, ?, ?, ?, ?)"; public static void insert(Corso corso) throws SQLException, ClassNotFoundException { Connection con = null; PreparedStatement pstmt = null; con = ConnectionManager.getConnection(); pstmt = con.prepareStatement(INSERT_SQL); pstmt.setInt(1, corso.getCodice()); pstmt.setString(2, corso.getNome()); pstmt.setDate(3, corso.getData()); pstmt.setTime(4, corso.getOra()); pstmt.setInt(5, corso.getPrenotazione().getCodice()); pstmt.setString(6, corso.getIstruttore().getCodiceFis()); pstmt.executeUpdate(); pstmt.close(); con.close(); // si assume la modalita� autocommit } // Comando SQL per l�ottenimento di una nuova istanza private static final String FIND_BY_CODICE = "SELECT * FROM Corso WHERE codice = ?"; public static void load(Corso corso) throws SQLException, ClassNotFoundException { Connection con = null; PreparedStatement pstmt = null; ResultSet rs = null; con = ConnectionManager.getConnection(); pstmt = con.prepareStatement(FIND_BY_CODICE); pstmt.setInt(1, corso.getCodice()); rs = pstmt.executeQuery(); rs.next(); corso.setNome(rs.getString("nome")); corso.setData(rs.getDate("data")); corso.setData(rs.getDate("ora")); Prenotazione p = new Prenotazione(rs.getInt("prenotazione")); PrenotazioneDAO.load(p); corso.setPrenotazione(p); Istruttore i = new Istruttore(rs.getString("ingresso")); IstruttoreDAO.load(i); corso.setIstruttore(i); rs.close(); pstmt.close(); con.close(); } // Comando SQL per l�aggiornamento di una nuova istanza private static final String UPDATE_BY_CODICE = "UPDATE Corso SET nome = ?, data = ?, ora = ?, prenotazione = ?, istruttore=?, WHERE codice = ?"; // public static void update(Corso corso) throws SQLException, // ClassNotFoundException { // Connection con = null; // PreparedStatement pstmt = null; // ResultSet rs = null; // con = ConnectionManager.getConnection(); // pstmt = con.prepareStatement(UPDATE_BY_CODICE); // pstmt.setInt(6, corso.getCodice()); // pstmt.setString(1, corso.getNome()); // pstmt.setDate(2, corso.getData()); // pstmt.setDate(3, corso.getOra()); // pstmt.setInt(4, corso.getPrenotazione().getCodice()); // pstmt.setString(5, corso.getIstruttore().getCodiceFis()); // // pstmt.executeUpdate(); // pstmt.close(); // con.close(); public static void update(Corso corso) throws MyException, ClassNotFoundException { Connection con = null; PreparedStatement pstmt = null; ResultSet rs = null; try { con = ConnectionManager.getConnection(); con.setAutoCommit(false); con.setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ); pstmt = con.prepareStatement(FIND_BY_CODICE); pstmt.setInt(1, corso.getCodice()); rs = pstmt.executeQuery(); if (rs.next()) if (rs.getString(2) == corso.getOldNome() && rs.getDate(3) == corso.getOldData() && rs.getTime(4) == corso.getOra() && rs.getInt(5) == corso.getPrenotazione().getCodice() && rs.getString(6) == corso.getIstruttore() .getCodiceFis()) { pstmt = con.prepareStatement(UPDATE_BY_CODICE); pstmt.setInt(1, corso.getCodice()); pstmt.setString(2, corso.getNome()); pstmt.setDate(3, corso.getData()); pstmt.setTime(4, corso.getOra()); pstmt.setInt(5, corso.getPrenotazione().getCodice()); pstmt.setString(6, corso.getIstruttore().getCodiceFis()); pstmt.executeUpdate(); con.commit(); } else { // lancia una eccezione poiche� la verifica // sullo stato e� fallita con.setAutoCommit(true); con .setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ); throw new MyException("errore di aggiornamento"); } } catch (SQLException ex) { if (con != null) { try { con.rollback(); } catch (SQLException excep) { throw new MyException("Error: " + excep.getMessage()); } }// end if }// end catch (ex) finally { if (con != null) try { pstmt.close(); con.setAutoCommit(true); con .setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED); con.close(); } catch (SQLException ex2) { throw new MyException("Error: " + ex2.getMessage()); } }// end finally }// end update // Comando SQL per la cancellazione di un'istanza private static final String DELETE_SQL = "DELETE * FROM Corso WHERE codice = ?"; public static void delete(Corso corso) throws SQLException, ClassNotFoundException { Connection con = null; PreparedStatement pstmt = null; ResultSet rs = null; con = ConnectionManager.getConnection(); pstmt = con.prepareStatement(DELETE_SQL); pstmt.setInt(1, corso.getCodice()); rs = pstmt.executeQuery(); rs.close(); pstmt.close(); con.close(); } }

Rispondi quotando