Java tutorial
import static com.sun.org.apache.xalan.internal.xsltc.compiler.util.Type.Int; import java.awt.Color; import java.io.IOException; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.text.NumberFormat; import java.text.ParseException; import java.util.Arrays; import java.util.logging.Level; import java.util.logging.Logger; import javax.swing.JFrame; import javax.swing.JOptionPane; import org.apache.commons.lang.ArrayUtils; import static org.eclipse.persistence.platform.database.oracle.plsql.OraclePLSQLTypes.Int; /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ /** * * @author Eric */ public class updatePledge extends javax.swing.JFrame { /** * Creates new form updatePledge */ public updatePledge() throws IOException { initComponents(); //setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); jTextField1.setColumns(8); jTextField2.setColumns(8); jTextField1.setText("MM/DD/YYYY"); jTextField2.setText("MM/DD/YYYY"); jComboBox1.removeAllItems(); jComboBox1.addItem("Item 1"); jComboBox2.removeAllItems(); jComboBox2.addItem("Item 1"); jComboBox2.setBackground(Color.lightGray); jComboBox2.setEditable(false); jComboBox2.setEnabled(false); jComboBox4.setBackground(Color.lightGray); jComboBox4.setEditable(false); jComboBox4.setEnabled(false); //Getting Donor Names and ID's try { try { Class.forName("net.ucanaccess.jdbc.UcanaccessDriver"); } catch (ClassNotFoundException ex) { Logger.getLogger(viewDonors.class.getName()).log(Level.SEVERE, null, ex); } Connection con; con = DriverManager.getConnection(DBcon.Connect(), DBcon.Login(), DBcon.Pass()); //(file path, db login, db password) - since it doesnt have a login, leave it blank Statement s = con.createStatement(); System.out.println("Connection to DB established..."); ResultSet rs = s.executeQuery("SELECT Fname, Minit, Lname, " + "DonorID FROM Individual"); System.out.println("Is connection closed: " + con.isClosed()); System.out.println("Connection to DB established..."); while (rs.next()) { //Fname, Minit, Lname System.out.print(rs.getString(1) + " "); jComboBox1.addItem(rs.getString(1) + ", " + rs.getString(2) + " " + rs.getString(3)); System.out.print(rs.getString(4)); jComboBox2.addItem(rs.getString(4)); } con.close(); System.out.println("Is connection closed: " + con.isClosed()); } catch (SQLException ex) { Logger.getLogger(userLogin.class.getName()).log(Level.SEVERE, null, ex); } jComboBox5.removeAllItems(); jComboBox5.addItem("Item 1"); jComboBox5.addItem("Weekly"); jComboBox5.addItem("Biweekly"); jComboBox5.addItem("Monthly"); } /** * This method is called from within the constructor to initialize the form. * WARNING: Do NOT modify this code. The content of this method is always * regenerated by the Form Editor. */ @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents private void initComponents() { jComboBox1 = new javax.swing.JComboBox(); jButton1 = new javax.swing.JButton(); jButton2 = new javax.swing.JButton(); jButton3 = new javax.swing.JButton(); jButton4 = new javax.swing.JButton(); jComboBox2 = new javax.swing.JComboBox(); jComboBox3 = new javax.swing.JComboBox(); jButton5 = new javax.swing.JButton(); jComboBox4 = new javax.swing.JComboBox(); jComboBox5 = new javax.swing.JComboBox(); jLabel1 = new javax.swing.JLabel(); jTextField1 = new javax.swing.JTextField(); jTextField2 = new javax.swing.JTextField(); jLabel2 = new javax.swing.JLabel(); jLabel3 = new javax.swing.JLabel(); jLabel4 = new javax.swing.JLabel(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); jComboBox1.setModel( new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" })); jComboBox1.addItemListener(new java.awt.event.ItemListener() { public void itemStateChanged(java.awt.event.ItemEvent evt) { jComboBox1ItemStateChanged(evt); } }); jComboBox1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jComboBox1ActionPerformed(evt); } }); jButton1.setText("Get Donation Info"); jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton1ActionPerformed(evt); } }); jButton2.setText("Update"); jButton2.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton2ActionPerformed(evt); } }); jButton3.setText("Back to Update Menu"); jButton3.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton3ActionPerformed(evt); } }); jButton4.setText("Help"); jButton4.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton4ActionPerformed(evt); } }); jComboBox2.setModel( new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" })); jComboBox3.setModel( new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" })); jComboBox3.addItemListener(new java.awt.event.ItemListener() { public void itemStateChanged(java.awt.event.ItemEvent evt) { jComboBox3ItemStateChanged(evt); } }); jComboBox3.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jComboBox3ActionPerformed(evt); } }); jButton5.setText("Display Donation"); jButton5.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton5ActionPerformed(evt); } }); jComboBox4.setModel( new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" })); jComboBox5.setModel( new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" })); jLabel1.setText("Payment Frequency:"); jTextField1.setText("jTextField1"); jTextField2.setText("jTextField2"); jLabel2.setText("Pay To Date:"); jLabel3.setText("Last Payment Date:"); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addContainerGap() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(jButton1) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(layout.createSequentialGroup() .addComponent(jComboBox3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(jButton5) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent( jComboBox4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup().addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) .addGroup(layout.createSequentialGroup() .addGap(0, 58, Short.MAX_VALUE) .addGroup(layout .createParallelGroup( javax.swing.GroupLayout.Alignment.TRAILING) .addComponent(jLabel1).addComponent(jLabel2) .addComponent(jLabel3)) .addGap(21, 21, 21) .addGroup(layout .createParallelGroup( javax.swing.GroupLayout.Alignment.TRAILING) .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jComboBox5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))) .addGroup(layout.createSequentialGroup().addComponent(jButton2) .addPreferredGap( javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jLabel4) .addPreferredGap( javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jButton4))) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jButton3))) .addContainerGap())); layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addContainerGap().addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jButton1).addComponent(jComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jComboBox3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jButton5).addComponent(jComboBox4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(18, 18, 18) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jComboBox5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel1)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel2)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel3)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 125, Short.MAX_VALUE) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jButton2).addComponent(jButton3).addComponent(jButton4) .addComponent(jLabel4)) .addContainerGap())); pack(); }// </editor-fold>//GEN-END:initComponents private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed // TODO add your handling code here: this.setVisible(false); new updateInfo().setVisible(true); }//GEN-LAST:event_jButton3ActionPerformed private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed // TODO add your handling code here: jComboBox3.removeAllItems(); jComboBox3.addItem("Item 1"); jComboBox4.removeAllItems(); jComboBox4.addItem("Item 1"); String DonorID = jComboBox2.getSelectedItem().toString(); try { try { Class.forName("net.ucanaccess.jdbc.UcanaccessDriver"); } catch (ClassNotFoundException ex) { Logger.getLogger(viewDonors.class.getName()).log(Level.SEVERE, null, ex); } Connection con; con = DriverManager.getConnection(DBcon.Connect(), DBcon.Login(), DBcon.Pass()); //(file path, db login, db password) - since it doesnt have a login, leave it blank Statement s = con.createStatement(); System.out.println("Connection to DB established..."); ResultSet rs = s.executeQuery("SELECT D.Amount, D.DonationID " + "FROM Donations as D INNER JOIN Pledges as P ON (D.DonationID = P.PledgeID) " + "WHERE (D.DonorID = '" + DonorID + "')"); System.out.println("Is connection closed: " + con.isClosed()); System.out.println("Connection to DB established..."); while (rs.next()) { jComboBox3.addItem(rs.getString(1)); //Amount jComboBox4.addItem(rs.getString(2)); //DonationID } con.close(); System.out.println("Is connection closed: " + con.isClosed()); } catch (SQLException ex) { Logger.getLogger(userLogin.class.getName()).log(Level.SEVERE, null, ex); } catch (IOException ex) { Logger.getLogger(updatePledge.class.getName()).log(Level.SEVERE, null, ex); } }//GEN-LAST:event_jButton1ActionPerformed private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBox1ActionPerformed // TODO add your handling code here: }//GEN-LAST:event_jComboBox1ActionPerformed private void jComboBox1ItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_jComboBox1ItemStateChanged // TODO add your handling code here: jComboBox3.removeAllItems(); jComboBox3.addItem("Item 1"); jComboBox2.setSelectedIndex(jComboBox1.getSelectedIndex()); jLabel4.setText(""); }//GEN-LAST:event_jComboBox1ItemStateChanged private void jComboBox3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBox3ActionPerformed // TODO add your handling code here: //jComboBox4.setSelectedIndex(jComboBox3.getSelectedIndex()); jLabel4.setText(""); }//GEN-LAST:event_jComboBox3ActionPerformed private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton5ActionPerformed // TODO add your handling code here: String PledgeID = jComboBox4.getSelectedItem().toString(); try { try { Class.forName("net.ucanaccess.jdbc.UcanaccessDriver"); } catch (ClassNotFoundException ex) { Logger.getLogger(viewDonors.class.getName()).log(Level.SEVERE, null, ex); } Connection con; con = DriverManager.getConnection(DBcon.Connect(), DBcon.Login(), DBcon.Pass()); //(file path, db login, db password) - since it doesnt have a login, leave it blank Statement s = con.createStatement(); System.out.println("Connection to DB established..."); ResultSet rs = s.executeQuery("SELECT PaymentFrequency, PayToDate, LastPaymentDate " + "FROM Pledges " + "WHERE (Pledges.PledgeID = '" + PledgeID + "')"); System.out.println("Is connection closed: " + con.isClosed()); System.out.println("Connection to DB established..."); while (rs.next()) { jComboBox5.setSelectedItem(rs.getString(1)); jTextField1.setText(rs.getString(2)); jTextField2.setText(rs.getString(3)); } con.close(); System.out.println("Is connection closed: " + con.isClosed()); } catch (SQLException ex) { Logger.getLogger(userLogin.class.getName()).log(Level.SEVERE, null, ex); } catch (IOException ex) { Logger.getLogger(updatePledge.class.getName()).log(Level.SEVERE, null, ex); } }//GEN-LAST:event_jButton5ActionPerformed private void jComboBox3ItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_jComboBox3ItemStateChanged // TODO add your handling code here: jComboBox4.setSelectedIndex(jComboBox3.getSelectedIndex()); jLabel4.setText(""); }//GEN-LAST:event_jComboBox3ItemStateChanged private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed String PledgeID = jComboBox4.getSelectedItem().toString(); String PaymentFrequency, LastPaymentDate; PaymentFrequency = jComboBox5.getSelectedItem().toString(); LastPaymentDate = jTextField2.getText(); String Pay = jTextField1.getText(); System.out.println(Pay); StringCC str = new StringCC(); Float PayToDate = Float.parseFloat(str.CleanUp(Pay)); try { try { Class.forName("net.ucanaccess.jdbc.UcanaccessDriver"); } catch (ClassNotFoundException ex) { Logger.getLogger(viewDonors.class.getName()).log(Level.SEVERE, null, ex); } Connection con; con = DriverManager.getConnection(DBcon.Connect(), DBcon.Login(), DBcon.Pass()); //(file path, db login, db password) - since it doesnt have a login, leave it blank Statement s = con.createStatement(); System.out.println("Connection to DB established..."); s.executeUpdate("UPDATE [Pledges] SET " //also remember to space everything correctly, program was reading SETIND instead of SET IND + "Pledges.PaymentFrequency = " + "'" + PaymentFrequency + "'," + "Pledges.PayToDate = " + "'" + PayToDate + "'," + "Pledges.LastPaymentDate = " + "'" + LastPaymentDate + "' " + "WHERE (Pledges.PledgeID = '" + PledgeID + "')"); System.out.println("Is connection closed: " + con.isClosed()); System.out.println("Connection to DB established..."); System.out.println("Update Executed."); con.commit(); con.close(); System.out.println("Is connection closed: " + con.isClosed()); } catch (SQLException ex) { Logger.getLogger(userLogin.class.getName()).log(Level.SEVERE, null, ex); } catch (IOException ex) { Logger.getLogger(updatePledge.class.getName()).log(Level.SEVERE, null, ex); } jLabel4.setText("Updated."); }//GEN-LAST:event_jButton2ActionPerformed private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed // TODO add your handling code here: JOptionPane Help = new JOptionPane(); JOptionPane.showMessageDialog(this, "Meow"); }//GEN-LAST:event_jButton4ActionPerformed /** * @param args the command line arguments */ public static void main(String args[]) { /* Set the Nimbus look and feel */ //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html */ try { for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { if ("Nimbus".equals(info.getName())) { javax.swing.UIManager.setLookAndFeel(info.getClassName()); break; } } } catch (ClassNotFoundException ex) { java.util.logging.Logger.getLogger(updatePledge.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(updatePledge.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(updatePledge.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(updatePledge.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } //</editor-fold> /* Create and display the form */ java.awt.EventQueue.invokeLater(new Runnable() { public void run() { try { new updatePledge().setVisible(true); } catch (IOException ex) { Logger.getLogger(updatePledge.class.getName()).log(Level.SEVERE, null, ex); } } }); } // Variables declaration - do not modify//GEN-BEGIN:variables 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 javax.swing.JComboBox jComboBox2; private javax.swing.JComboBox jComboBox3; private javax.swing.JComboBox jComboBox4; private javax.swing.JComboBox jComboBox5; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel2; private javax.swing.JLabel jLabel3; private javax.swing.JLabel jLabel4; private javax.swing.JTextField jTextField1; private javax.swing.JTextField jTextField2; // End of variables declaration//GEN-END:variables }