Advertisement
stevennathaniel

Fitur Pencarian Berdasar Tanggal ( jDateChooser )

May 20th, 2015
429
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.06 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package latihan15;
  7.  
  8. import javax.swing.SwingUtilities;
  9.  
  10. import javax.swing.JTable;
  11.  
  12. import java.sql.Connection;
  13.  
  14. import java.sql.DriverManager;
  15.  
  16. import java.sql.ResultSet;
  17.  
  18. import java.sql.ResultSetMetaData;
  19.  
  20. import java.sql.Statement;
  21.  
  22. import java.util.logging.Level;
  23.  
  24. import java.util.logging.Logger;
  25.  
  26. import javax.swing.table.DefaultTableModel;
  27.  
  28. import java.sql.*;
  29.  
  30. import java.util.Locale;
  31.  
  32. import java.util.Vector;
  33.  
  34. import java.text.SimpleDateFormat;
  35.  
  36. import java.util.Calendar;
  37.  
  38. import com.toedter.calendar.JDateChooser;
  39.  
  40. import java.text.DateFormat;
  41.  
  42.  
  43.  
  44.  
  45.  
  46.  
  47. /**
  48.  *
  49.  * @author steven
  50.  */
  51. public class CariData9 extends javax.swing.JFrame {
  52.  
  53.     /**
  54.      * Creates new form CariData9
  55.      */
  56.     public CariData9() {
  57.         initComponents();
  58.     }
  59.  
  60.     /**
  61.      * This method is called from within the constructor to initialize the form.
  62.      * WARNING: Do NOT modify this code. The content of this method is always
  63.      * regenerated by the Form Editor.
  64.      */
  65.     @SuppressWarnings("unchecked")
  66.     // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
  67.     private void initComponents() {
  68.  
  69.         jDateChooser1 = new com.toedter.calendar.JDateChooser();
  70.         jButton1 = new javax.swing.JButton();
  71.  
  72.         setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  73.  
  74.         jDateChooser1.setDateFormatString("EEEE, dd MMMM yyyy");
  75.         jDateChooser1.setFont(new java.awt.Font("Cantarell", 1, 15)); // NOI18N
  76.  
  77.         jButton1.setFont(new java.awt.Font("Cantarell", 1, 15)); // NOI18N
  78.         jButton1.setText("Cari");
  79.         jButton1.addActionListener(new java.awt.event.ActionListener() {
  80.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  81.                 jButton1ActionPerformed(evt);
  82.             }
  83.         });
  84.  
  85.         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  86.         getContentPane().setLayout(layout);
  87.         layout.setHorizontalGroup(
  88.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  89.             .addGroup(layout.createSequentialGroup()
  90.                 .addContainerGap()
  91.                 .addComponent(jDateChooser1, javax.swing.GroupLayout.PREFERRED_SIZE, 260, javax.swing.GroupLayout.PREFERRED_SIZE)
  92.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  93.                 .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE)
  94.                 .addContainerGap(248, Short.MAX_VALUE))
  95.         );
  96.         layout.setVerticalGroup(
  97.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  98.             .addGroup(layout.createSequentialGroup()
  99.                 .addContainerGap()
  100.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
  101.                     .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
  102.                     .addComponent(jDateChooser1, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE))
  103.                 .addContainerGap(258, Short.MAX_VALUE))
  104.         );
  105.  
  106.         pack();
  107.     }// </editor-fold>                        
  108.  
  109.     private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                        
  110.         // TODO add your handling code here:
  111.        
  112.         // java.sql.Date dataDate = new java.sql.Date(jDateChooser1.getDate().getTime());
  113.        
  114.         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  115.        
  116.         String dataDate = sdf.format(jDateChooser1.getDate().getTime());
  117.        
  118.        
  119.         Connection koneksi = null;
  120.        
  121.         Statement stmt = null;
  122.        
  123.         String cariTanggal = "SELECT * FROM testdatapegawai1 WHERE tanggallahir = '" + dataDate + "'";
  124.        
  125.         String url = "jdbc:postgresql://localhost:5432/pdam";
  126.        
  127.         String user = "steven";
  128.        
  129.         String password = "kucing";
  130.        
  131.        
  132.         try{
  133.            
  134.             koneksi = DriverManager.getConnection(url,user,password);
  135.            
  136.             stmt = koneksi.createStatement();
  137.            
  138.             ResultSet rs = stmt.executeQuery(cariTanggal);
  139.            
  140.             while(rs.next()){
  141.                
  142.                
  143.                 String nik = rs.getString("nik");
  144.                
  145.                 String namalengkap = rs.getString("namalengkap");
  146.                
  147.                 String jeniskelamin = rs.getString("jeniskelamin");
  148.                
  149.                
  150.                 System.out.println(nik + "\t" + namalengkap + "\t" + jeniskelamin);
  151.             }
  152.            
  153.             //koneksi.close();
  154.            
  155.        
  156.        
  157.     } catch(SQLException ex){
  158.    
  159.     ex.printStackTrace();
  160.    
  161.     }
  162.        
  163.     }                                        
  164.  
  165.     /**
  166.      * @param args the command line arguments
  167.      */
  168.     public static void main(String args[]) {
  169.         /* Set the Nimbus look and feel */
  170.         //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
  171.         /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
  172.          * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
  173.          */
  174.         try {
  175.             for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
  176.                 if ("Nimbus".equals(info.getName())) {
  177.                     javax.swing.UIManager.setLookAndFeel(info.getClassName());
  178.                     break;
  179.                 }
  180.             }
  181.         } catch (ClassNotFoundException ex) {
  182.             java.util.logging.Logger.getLogger(CariData9.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  183.         } catch (InstantiationException ex) {
  184.             java.util.logging.Logger.getLogger(CariData9.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  185.         } catch (IllegalAccessException ex) {
  186.             java.util.logging.Logger.getLogger(CariData9.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  187.         } catch (javax.swing.UnsupportedLookAndFeelException ex) {
  188.             java.util.logging.Logger.getLogger(CariData9.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  189.         }
  190.         //</editor-fold>
  191.  
  192.         /* Create and display the form */
  193.         java.awt.EventQueue.invokeLater(new Runnable() {
  194.             public void run() {
  195.                 new CariData9().setVisible(true);
  196.             }
  197.         });
  198.     }
  199.  
  200.     // Variables declaration - do not modify                    
  201.     private javax.swing.JButton jButton1;
  202.     private com.toedter.calendar.JDateChooser jDateChooser1;
  203.     // End of variables declaration                  
  204. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement