Java tutorial
/* * 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 KIDDY */ import java.sql.*; import java.text.DateFormat; import java.text.SimpleDateFormat; import javax.swing.JOptionPane; import org.joda.time.DateTime; import org.joda.time.Days; public class UpdateCustomer extends javax.swing.JFrame { /** * Creates new form UpdateCustomer */ public UpdateCustomer() { initComponents(); dp_birthdate.setFormats(new String[] { "yyyy-MM-dd" }); dp_birthdate.setDate(null); } /** * 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() { tf_IDNumber = new javax.swing.JTextField(); tf_name = new javax.swing.JTextField(); tf_phone = new javax.swing.JTextField(); jLabel2 = new javax.swing.JLabel(); jLabel3 = new javax.swing.JLabel(); jLabel4 = new javax.swing.JLabel(); dp_birthdate = new org.jdesktop.swingx.JXDatePicker(); jLabel5 = new javax.swing.JLabel(); jLabel6 = new javax.swing.JLabel(); jScrollPane1 = new javax.swing.JScrollPane(); ta_address = new javax.swing.JTextArea(); jLabel7 = new javax.swing.JLabel(); jLabel1 = new javax.swing.JLabel(); btn_check = new javax.swing.JButton(); btn_update = new javax.swing.JButton(); btn_delete = new javax.swing.JButton(); jTextField1 = new javax.swing.JTextField(); jButton2 = new javax.swing.JButton(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); tf_IDNumber.setText(" "); tf_IDNumber.addKeyListener(new java.awt.event.KeyAdapter() { public void keyTyped(java.awt.event.KeyEvent evt) { tf_IDNumberKeyTyped(evt); } }); tf_name.setText(" "); tf_phone.setText(" "); tf_phone.addKeyListener(new java.awt.event.KeyAdapter() { public void keyTyped(java.awt.event.KeyEvent evt) { tf_phoneKeyTyped(evt); } }); jLabel2.setText("ID Customer"); jLabel3.setText("ID Number"); jLabel4.setText("Name"); jLabel5.setText("Birthdate"); jLabel6.setText("Phone"); ta_address.setColumns(20); ta_address.setRows(5); jScrollPane1.setViewportView(ta_address); jLabel7.setText("Address"); jLabel1.setFont(new java.awt.Font("Myriad Pro", 1, 18)); // NOI18N jLabel1.setText("Update Customer"); btn_check.setText("Check"); btn_check.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btn_checkActionPerformed(evt); } }); btn_update.setText("Update"); btn_update.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btn_updateActionPerformed(evt); } }); btn_delete.setText("Delete"); btn_delete.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btn_deleteActionPerformed(evt); } }); jTextField1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jTextField1ActionPerformed(evt); } }); jButton2.setText("Close"); jButton2.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton2ActionPerformed(evt); } }); 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(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jLabel1).addGap(67, 67, 67) .addComponent( jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap()) .addGroup(layout.createSequentialGroup().addGap(74, 74, 74) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) .addComponent(jLabel2) .addComponent(jLabel3, javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel4, javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel5, javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel6, javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel7, javax.swing.GroupLayout.Alignment.LEADING)) .addGap(18, 18, 18) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addComponent(btn_update) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(btn_delete)) .addGroup(layout.createSequentialGroup() .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(btn_check))) .addContainerGap(90, Short.MAX_VALUE)) .addGroup(layout.createSequentialGroup().addGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addComponent(tf_name).addComponent(tf_phone) .addComponent(dp_birthdate, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jScrollPane1).addComponent(tf_IDNumber, javax.swing.GroupLayout.PREFERRED_SIZE, 166, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(0, 0, Short.MAX_VALUE))))); layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addGap(22, 22, 22).addComponent(jLabel1)) .addGroup(layout.createSequentialGroup().addContainerGap().addComponent(jButton2))) .addGap(39, 39, 39) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel2).addComponent(btn_check).addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel3).addComponent(tf_IDNumber, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel4).addComponent(tf_name, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel5).addComponent(dp_birthdate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel6).addComponent(tf_phone, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel7).addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(btn_update).addComponent(btn_delete)) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))); pack(); }// </editor-fold>//GEN-END:initComponents private void btn_checkActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_checkActionPerformed // TODO add your handling code here: try { Koneksi kon = new Koneksi(); Connection con = kon.open(); String sql = "SELECT * FROM tb_cust where id_customer = ?"; PreparedStatement ps = con.prepareStatement(sql); ps.setString(1, jTextField1.getText()); ResultSet rs = ps.executeQuery(); if (rs.next()) { tf_IDNumber.setText(rs.getString("id_number")); tf_name.setText(rs.getString("name")); tf_phone.setText(rs.getString("phone")); ta_address.setText(rs.getString("address")); dp_birthdate.setDate(rs.getDate("birthdate")); } else { JOptionPane.showMessageDialog(this, "Data not found."); } } catch (Exception e) { System.err.println(e); } }//GEN-LAST:event_btn_checkActionPerformed private void btn_deleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_deleteActionPerformed // TODO add your handling code here: if (Validation() == 0) { try { int result = JOptionPane.showConfirmDialog(this, "Are you sure?", "Delete Confirmation", JOptionPane.YES_NO_OPTION); if (result == JOptionPane.YES_OPTION) { Koneksi kon = new Koneksi(); Connection con = kon.open(); String sql = "delete from tb_cust where id_customer = ?"; PreparedStatement ps = con.prepareStatement(sql); ps.setString(1, jTextField1.getText()); int row = ps.executeUpdate(); if (row != 0) { ClearForm(); JOptionPane.showMessageDialog(this, "Successful delete data."); } } else { ClearForm(); } } catch (Exception e) { System.err.println(e); } } }//GEN-LAST:event_btn_deleteActionPerformed private void btn_updateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_updateActionPerformed // TODO add your handling code here: // TODO add your handling code here: if (Validation() == 0) { String idcustomer = jTextField1.getText(); String idnumber = tf_IDNumber.getText(); String name = tf_name.getText(); String phone = tf_phone.getText(); String address = ta_address.getText(); DateFormat df = new SimpleDateFormat("yyyy-MM-dd"); String date = df.format(dp_birthdate.getDate()); try { Koneksi kon = new Koneksi(); Connection con = kon.open(); String sql = "update tb_cust set id_number = ?, " + "name = ?, phone = ?, " + "birthdate = ?, address = ? where id_customer = ?"; PreparedStatement ps = con.prepareStatement(sql); ps.setString(1, idnumber); ps.setString(2, name); ps.setString(3, phone); ps.setString(4, date); ps.setString(5, address); ps.setString(6, idcustomer); int rows = ps.executeUpdate(); if (rows != 0) { JOptionPane.showMessageDialog(this, "Data sucessfull updated."); ClearForm(); } else { JOptionPane.showMessageDialog(this, "Check Error."); } } catch (Exception e) { System.err.println(e); } } }//GEN-LAST:event_btn_updateActionPerformed private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextField1ActionPerformed // TODO add your handling code here: }//GEN-LAST:event_jTextField1ActionPerformed private void tf_IDNumberKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tf_IDNumberKeyTyped // TODO add your handling code here: char c = evt.getKeyChar(); if (!((c >= '0') && (c <= '9') || (c == evt.VK_BACK_SPACE) || (c == evt.VK_DELETE))) { evt.consume(); } }//GEN-LAST:event_tf_IDNumberKeyTyped private void tf_phoneKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tf_phoneKeyTyped // TODO add your handling code here: char c = evt.getKeyChar(); if (!((c >= '0') && (c <= '9') || (c == evt.VK_BACK_SPACE) || (c == evt.VK_DELETE))) { evt.consume(); } }//GEN-LAST:event_tf_phoneKeyTyped private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed // TODO add your handling code here: this.hide(); }//GEN-LAST:event_jButton2ActionPerformed public void ClearForm() { jTextField1.setText(null); tf_IDNumber.setText(null); tf_name.setText(null); tf_phone.setText(null); ta_address.setText(null); dp_birthdate.setDate(null); } private int Validation() { int flag = 0; String error = ""; if (jTextField1.getText().equals("")) { flag = 1; error += "ID Number cannot be blank.\n"; } if (tf_IDNumber.getText().equals("")) { flag = 1; error += "ID Number cannot be blank.\n"; } if (tf_phone.getText().equals("")) { flag = 1; error += "Phone Number cannot be blank.\n"; } if (ta_address.getText().equals("")) { flag = 1; error += "Address cannot be blank.\n"; } if (dp_birthdate.getDate() == null) { flag = 1; error += "Tanggal harus dipilih.\n"; } else { java.util.Date today = new java.util.Date(); //mengambil tanggal hari ini java.util.Date tgllahir = dp_birthdate.getDate(); int days = getDaysDiff(today, tgllahir); // tanggal lahir harus sebelum tanggal hari ini. if (today.before(tgllahir)) { flag = 1; error += "Tanggal lahir tidak valid.\n"; } // validasi cek umur dibawah 17 tahun boolean umur = isUnderSeventeenYears(tgllahir); if (umur) { flag = 1; error += "Harus 17 tahun.\n"; } } if (flag == 0) { } else { JOptionPane.showMessageDialog(this, error); } return flag; } private int getDaysDiff(java.util.Date startdate, java.util.Date enddate) { DateTime start = new DateTime(startdate.getYear(), startdate.getMonth(), startdate.getDay(), 0, 0, 0, 0); DateTime end = new DateTime(enddate.getYear(), enddate.getMonth(), enddate.getDay(), 0, 0, 0, 0); int days = Days.daysBetween(start, end).getDays(); return days; } private Boolean isUnderSeventeenYears(java.util.Date dateBirth) { Boolean res = false; if (dateBirth != null) { java.util.Date now = new java.util.Date(); int nowYear = now.getYear(); int nowMonth = now.getMonth(); int nowDate = now.getDate(); int dateYear = dateBirth.getYear(); int dateMonth = dateBirth.getMonth(); int dateDate = dateBirth.getDate(); int resYear = nowYear - dateYear; if (resYear < 17 || (resYear == 17 && nowMonth < dateMonth) || (resYear == 17 && nowMonth == dateMonth && nowDate < dateDate)) { res = true; } } return res; } /** * @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(UpdateCustomer.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(UpdateCustomer.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(UpdateCustomer.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(UpdateCustomer.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() { new UpdateCustomer().setVisible(true); } }); } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton btn_check; private javax.swing.JButton btn_delete; private javax.swing.JButton btn_update; private org.jdesktop.swingx.JXDatePicker dp_birthdate; private javax.swing.JButton jButton1; private javax.swing.JButton jButton2; 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.JScrollPane jScrollPane1; private javax.swing.JTextField jTextField1; private javax.swing.JTextArea ta_address; private javax.swing.JTextField tf_IDNumber; private javax.swing.JTextField tf_name; private javax.swing.JTextField tf_phone; // End of variables declaration//GEN-END:variables }