Visualizzazione dei risultati da 1 a 2 su 2

Discussione: jSpinner e time

  1. #1

    jSpinner e time

    Ciao, ho 2 problemi.

    1- nn riesco ad inserire le ore in un jSpinner. Tale valore deve essere poi memorizzato in un db.

    2- nn riesco a collegare le date dalla swing con il db(nella swing utilizzo JDateChooser).

    Vi allego le classi che ho creato(per creare le swing ho utilizzato netBeans)

    codice:
    package gui;
    
    public class CorsoSwing extends javax.swing.JDialog {
    
        /** Creates new form Corso */
        public CorsoSwing(java.awt.Frame parent, boolean modal) {
            super(parent, modal);
            initComponents();
        }
    
            @SuppressWarnings("unchecked")
        // <editor-fold defaultstate="collapsed" desc="Generated Code">
        private void initComponents() {
    
            jPanel1 = new javax.swing.JPanel();
            jLabel6 = new javax.swing.JLabel();
            jRadioButton1 = new javax.swing.JRadioButton();
            jRadioButton2 = new javax.swing.JRadioButton();
            jLabel2 = new javax.swing.JLabel();
            jTextField1 = new javax.swing.JTextField();
            jLabel3 = new javax.swing.JLabel();
            jLabel4 = new javax.swing.JLabel();
            jLabel5 = new javax.swing.JLabel();
            jComboBox1 = new javax.swing.JComboBox();
            jSpinner1 = new javax.swing.JSpinner();
            jSpinner2 = new javax.swing.JSpinner();
            jLabel1 = new javax.swing.JLabel();
            jButton1 = new javax.swing.JButton();
            jButton2 = new javax.swing.JButton();
            jButton3 = new javax.swing.JButton();
            jButton4 = new javax.swing.JButton();
            jButton5 = new javax.swing.JButton();
            jLabel7 = new javax.swing.JLabel();
            jLabel9 = new javax.swing.JLabel();
            jDateChooser1 = new com.toedter.calendar.JDateChooser();
                    
            setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
            setTitle("Corso");
            setResizable(false);
    
            jPanel1.setBackground(new java.awt.Color(204, 204, 255));
    
            jLabel6.setFont(new java.awt.Font("Verdana", 0, 12));
            jLabel6.setText("Tipo");
    
            jRadioButton1.setBackground(new java.awt.Color(204, 204, 255));
            jRadioButton1.setFont(new java.awt.Font("Verdana", 0, 12));
            jRadioButton1.setText("Corso Musicale");
    
            jRadioButton2.setBackground(new java.awt.Color(204, 204, 255));
            jRadioButton2.setFont(new java.awt.Font("Verdana", 0, 12));
            jRadioButton2.setText("Corso SPA");
    
            jLabel2.setFont(new java.awt.Font("Verdana", 0, 12));
            jLabel2.setText("Nome");
    
            jTextField1.setFont(new java.awt.Font("Verdana", 0, 12));
    
            jLabel3.setFont(new java.awt.Font("Verdana", 0, 12));
            jLabel3.setText("Data");
    
            jLabel4.setFont(new java.awt.Font("Verdana", 0, 12));
            jLabel4.setText("Ora");
    
            jLabel5.setFont(new java.awt.Font("Verdana", 0, 12));
            jLabel5.setText("Istruttore");
    
            jComboBox1.setFont(new java.awt.Font("Verdana", 0, 11));
            jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
    
            jSpinner1.setFont(new java.awt.Font("Verdana", 0, 11));
            jSpinner1.addInputMethodListener(new java.awt.event.InputMethodListener() {
                public void caretPositionChanged(java.awt.event.InputMethodEvent evt) {
                }
                public void inputMethodTextChanged(java.awt.event.InputMethodEvent evt) {
                    jSpinner1InputMethodTextChanged(evt);
                }
            });
            jSpinner1.addKeyListener(new java.awt.event.KeyAdapter() {
                public void keyPressed(java.awt.event.KeyEvent evt) {
                    jSpinner1KeyPressed(evt);
                }
            });
    
            jSpinner2.setFont(new java.awt.Font("Verdana", 0, 11));
    
            jLabel1.setFont(new java.awt.Font("Comic Sans MS", 3, 24));
            jLabel1.setText("Corso");
    
            jButton1.setFont(new java.awt.Font("Verdana", 1, 11));
            jButton1.setText("  Inserisci  ");
            jButton1.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jButton1ActionPerformed(evt);
                }
            });
    
            jButton2.setFont(new java.awt.Font("Verdana", 1, 11));
            jButton2.setText("  Ricerca  ");
            jButton2.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jButton2ActionPerformed(evt);
                }
            });
    
            jButton3.setFont(new java.awt.Font("Verdana", 1, 11));
            jButton3.setText("  Modifica   ");
            jButton3.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jButton3ActionPerformed(evt);
                }
            });
    
            jButton4.setFont(new java.awt.Font("Verdana", 1, 11));
            jButton4.setText("  Elimina  ");
            jButton4.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jButton4ActionPerformed(evt);
                }
            });
    
            jButton5.setFont(new java.awt.Font("Verdana", 1, 11));
            jButton5.setText("    Esci    ");
            jButton5.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jButton5ActionPerformed(evt);
                }
            });
    
            jLabel7.setIcon(new javax.swing.ImageIcon("C:\\Users\\Simone\\Desktop\\Università\\Progetto basi 2008\\immagini\\fitness.jpg")); // NOI18N
            jLabel7.setText("jLabel7");
    
            jLabel9.setIcon(new javax.swing.ImageIcon("C:\\Users\\Simone\\Desktop\\Università\\Progetto basi 2008\\immagini\\piscina.jpg")); // NOI18N
            jLabel9.setText("jLabel7");
    
            jDateChooser1.setDateFormatString("dd-MM-yyyy");
    
            javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
            jPanel1.setLayout(jPanel1Layout);
            jPanel1Layout.setHorizontalGroup(
                jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup()
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jPanel1Layout.createSequentialGroup()
                            .addGap(209, 209, 209)
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jLabel2)
                                .addComponent(jLabel4)
                                .addComponent(jLabel5)
                                .addComponent(jLabel6)
                                .addComponent(jLabel3))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jDateChooser1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGroup(jPanel1Layout.createSequentialGroup()
                                    .addComponent(jSpinner2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(jSpinner1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGroup(jPanel1Layout.createSequentialGroup()
                                    .addComponent(jRadioButton1)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(jRadioButton2))
                                .addComponent(jTextField1, javax.swing.GroupLayout.DEFAULT_SIZE, 292, Short.MAX_VALUE)
                                .addComponent(jComboBox1, 0, 292, Short.MAX_VALUE)))
                        .addGroup(jPanel1Layout.createSequentialGroup()
                            .addGap(238, 238, 238)
                            .addComponent(jButton1)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(jButton2)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(jButton3))
                        .addGroup(jPanel1Layout.createSequentialGroup()
                            .addGap(472, 472, 472)
                            .addComponent(jLabel1))
                        .addGroup(jPanel1Layout.createSequentialGroup()
                            .addGap(84, 84, 84)
                            .addComponent(jLabel9, javax.swing.GroupLayout.PREFERRED_SIZE, 376, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jLabel7, javax.swing.GroupLayout.PREFERRED_SIZE, 376, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGroup(jPanel1Layout.createSequentialGroup()
                            .addComponent(jButton4)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(jButton5)))
                    .addGap(75, 75, 75))
            );
            jPanel1Layout.setVerticalGroup(
                jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jLabel1)
                    .addGap(42, 42, 42)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addComponent(jLabel2)
                        .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGap(10, 10, 10)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jLabel3)
                        .addComponent(jDateChooser1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGap(9, 9, 9)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel4)
                        .addComponent(jSpinner2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jSpinner1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGap(18, 18, 18)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel5)
                        .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGap(18, 18, 18)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel6)
                        .addComponent(jRadioButton1)
                        .addComponent(jRadioButton2))
                    .addGap(71, 71, 71)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jButton1)
                        .addComponent(jButton2)
                        .addComponent(jButton3)
                        .addComponent(jButton4)
                        .addComponent(jButton5))
                    .addGap(33, 33, 33)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jLabel9, javax.swing.GroupLayout.PREFERRED_SIZE, 238, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel7))
                    .addGap(28, 28, 28))
            );

  2. #2
    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();
    	}
    
    }

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.