Showing posts with label java. Show all posts
Showing posts with label java. Show all posts

Sunday, November 16, 2014

Form Input Data : Perpaduan jTextField dan JDateChooser

Pagi ini sudah berhasil membuat sebuah form input data yang menggabungkan antara inputan dari jTextField dengan JDateChooser yang datanya dimasukan ke 1 tabel yg sama. Source Code nya ada di bawah ini:


/*
 * 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.
 */
package latihan6;


import com.toedter.calendar.JDateChooser;

import javax.swing.*;

import java.awt.*;

import java.text.DateFormat;

import java.text.SimpleDateFormat;

import java.util.Calendar;

import java.util.Date;

import java.sql.*;

/**
 *
 * @author steven
 */
public class Frame6 extends javax.swing.JFrame {

    /**
     * Creates new form Frame6
     */
    public Frame6() {
        initComponents();
    }

    /**
     * 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">                         
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jTextField2 = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jTextField3 = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jTextField4 = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        jDateChooser1 = new com.toedter.calendar.JDateChooser();
        jLabel6 = new javax.swing.JLabel();
        jDateChooser2 = new com.toedter.calendar.JDateChooser();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Form Input Data");

        jLabel1.setFont(new java.awt.Font("Cantarell", 1, 17)); // NOI18N
        jLabel1.setText("ID Pengguna");

        jTextField1.setFont(new java.awt.Font("Cantarell", 1, 17)); // NOI18N
        jTextField1.setEnabled(false);

        jLabel2.setFont(new java.awt.Font("Cantarell", 1, 17)); // NOI18N
        jLabel2.setText("Nama Lengkap");

        jTextField2.setFont(new java.awt.Font("Cantarell", 1, 17)); // NOI18N

        jLabel3.setFont(new java.awt.Font("Cantarell", 1, 17)); // NOI18N
        jLabel3.setText("Nama Pengguna");

        jTextField3.setFont(new java.awt.Font("Cantarell", 1, 17)); // NOI18N

        jLabel4.setFont(new java.awt.Font("Cantarell", 1, 17)); // NOI18N
        jLabel4.setText("Kata Kunci");

        jTextField4.setFont(new java.awt.Font("Cantarell", 1, 17)); // NOI18N

        jLabel5.setFont(new java.awt.Font("Cantarell", 1, 17)); // NOI18N
        jLabel5.setText("Tanggal Mulai");

        jDateChooser1.setFont(new java.awt.Font("Cantarell", 1, 17)); // NOI18N

        jLabel6.setFont(new java.awt.Font("Cantarell", 1, 17)); // NOI18N
        jLabel6.setText("Tanggal Berakhir");

        jDateChooser2.setFont(new java.awt.Font("Cantarell", 1, 17)); // NOI18N

        jButton1.setFont(new java.awt.Font("Cantarell", 1, 17)); // NOI18N
        jButton1.setText("Test Koneksi");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setFont(new java.awt.Font("Cantarell", 1, 17)); // NOI18N
        jButton2.setText("Simpan Data");
        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()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2)
                            .addComponent(jLabel1)
                            .addComponent(jLabel3)
                            .addComponent(jLabel4)
                            .addComponent(jLabel5)
                            .addComponent(jLabel6))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jDateChooser2, javax.swing.GroupLayout.DEFAULT_SIZE, 297, Short.MAX_VALUE)
                            .addComponent(jTextField3, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jTextField1, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jTextField2, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jTextField4, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jDateChooser1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButton1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jButton2)
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(12, 12, 12)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(15, 15, 15)
                        .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jTextField2, javax.swing.GroupLayout.DEFAULT_SIZE, 32, Short.MAX_VALUE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(13, 13, 13)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(jTextField4, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jDateChooser1, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jDateChooser2, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, 39, Short.MAX_VALUE)
                    .addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>                       

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                        
        // TODO add your handling code here:
       
       
        Connection Koneksi = null;
       
        try {
           
            String url = "jdbc:postgresql://localhost:5432/pdam";
           
            String user = "steven";
           
            String password = "kucing";
           
            Koneksi = DriverManager.getConnection(url,user,password);
           
           
            if (Koneksi != null) {
               
               
                JOptionPane.showMessageDialog(this, "Sudah Berhasil Terkoneksi", "Koneksi Ke Server", JOptionPane.INFORMATION_MESSAGE);
               
               
                Koneksi.close();
               
               
            }
           
           
        } catch (SQLException ex) {
           
           
            System.out.println();
           
            ex.printStackTrace();
           
           
           
            }
       
    }                                       

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                        
        // TODO add your handling code here:
       
        String namaLengkap = jTextField2.getText();
       
        String namaPengguna = jTextField3.getText();
       
        String kataKunci = jTextField4.getText();
       
       
        java.sql.Date tanggalMulai = new java.sql.Date(jDateChooser1.getDate().getTime());
       
        java.sql.Date tanggalBerakhir = new java.sql.Date(jDateChooser2.getDate().getTime());
       
       
        Connection Koneksi = null;
       
        PreparedStatement PSInsert = null;
       
        String masukanData = "INSERT INTO pengguna" + "(namalengkap,namapengguna,katakunci,tanggalmulai,tanggalberakhir) VALUES" + "(?,?,?,?,?)";
       
       
        try {
           
            String url = "jdbc:postgresql://localhost:5432/pdam";
           
            String user = "steven";
           
            String password = "kucing";
           
           
            Koneksi = DriverManager.getConnection(url,user,password);
           
            Koneksi.setAutoCommit(false);
           
            if (Koneksi != null){
               
               
                PSInsert = Koneksi.prepareStatement(masukanData);
               
                PSInsert.setString(1, namaLengkap);
               
                PSInsert.setString(2, namaPengguna);
               
                PSInsert.setString(3, kataKunci);
               
               
                PSInsert.setDate(4, tanggalMulai);
               
                PSInsert.setDate(5, tanggalBerakhir);
               
               
                PSInsert.executeUpdate();
               
                Koneksi.commit();
               
                Koneksi.close();
               
               
            }
           
           
        } catch (SQLException ex) {
           
           
            System.out.println();
           
            ex.printStackTrace();
           
               
            }
           
           
           
       
    }                                       

    /**
     * @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(Frame6.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Frame6.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Frame6.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Frame6.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 Frame6().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify                    
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private com.toedter.calendar.JDateChooser jDateChooser1;
    private com.toedter.calendar.JDateChooser jDateChooser2;
    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.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JTextField jTextField3;
    private javax.swing.JTextField jTextField4;
    // End of variables declaration                  
}



Penampakan Programnya:


Saturday, November 15, 2014

Kode Java SE Untuk Menyimpan Tanggal Ke PostgreSQL

Kode Java SE yg berhasil dijalankan untuk menyimpan data tanggal dari jDateChooser ke tabel PostgreSQL di Debian 7:


 /*
 * 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.
 */
package latihan6;


import com.toedter.calendar.JDateChooser;
      
import javax.swing.*;

import java.awt.*;

import java.text.DateFormat;

import java.text.SimpleDateFormat;

import java.util.Calendar;

import java.util.Date;

import java.sql.*;

/**
 *
 * @author steven
 */
public class Frame4 extends javax.swing.JFrame {

    /**
     * Creates new form Frame4
     */
    public Frame4() {
        initComponents();
    }

    /**
     * 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">                        
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jDateChooser1 = new com.toedter.calendar.JDateChooser();
        jLabel2 = new javax.swing.JLabel();
        jDateChooser2 = new com.toedter.calendar.JDateChooser();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Input Tanggal");

        jLabel1.setFont(new java.awt.Font("Cantarell", 1, 17)); // NOI18N
        jLabel1.setText("Tanggal Mulai:");

        jLabel2.setFont(new java.awt.Font("Cantarell", 1, 17)); // NOI18N
        jLabel2.setText("Tanggal Berakhir:");

        jButton1.setFont(new java.awt.Font("Cantarell", 1, 17)); // NOI18N
        jButton1.setText("Test Koneksi");
        jButton1.setToolTipText("");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setFont(new java.awt.Font("Cantarell", 1, 17)); // NOI18N
        jButton2.setText("Simpan Tanggal");
        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()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(jLabel1)
                        .addComponent(jDateChooser1, javax.swing.GroupLayout.DEFAULT_SIZE, 330, Short.MAX_VALUE)
                        .addComponent(jLabel2)
                        .addComponent(jDateChooser2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButton1)
                        .addGap(18, 18, 18)
                        .addComponent(jButton2)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jDateChooser1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jDateChooser2, 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(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(94, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>                      

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                       
        // TODO add your handling code here:
      
      
        // Membuat perintah untuk tombol test koneksi
      
      
        Connection Koneksi = null;
      
        try {
          
            String url = "jdbc:postgresql://localhost:5432/pdam";
          
            String user = "steven";
          
            String password = "kucing";
          
          
            Koneksi = DriverManager.getConnection(url,user,password);
          
          
            if (Koneksi != null) {
              
                JOptionPane.showMessageDialog(this, "Sudah Berhasil Terkoneksi", "Koneksi Ke Server", JOptionPane.INFORMATION_MESSAGE);
              
              
                Koneksi.close();
              
              
            }
          
        } catch (SQLException ex) {
          
            System.out.println();
          
          
            ex.printStackTrace();
            }
      
    }                                      

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                       
        // TODO add your handling code here:
      
      
      java.sql.Date tanggalMulai = new java.sql.Date(jDateChooser1.getDate().getTime());
    
      java.sql.Date tanggalBerakhir = new java.sql.Date(jDateChooser2.getDate().getTime());
      
      
      
        Connection Koneksi = null;
      
        PreparedStatement PSInsert = null;
      
        String masukanData = "INSERT INTO penanggalan" + "(tanggalmulai,tanggalberakhir) VALUES" + "(?,?)";
      
      
        try {
          
            String url = "jdbc:postgresql://localhost:5432/pdam";
          
            String user = "steven";
          
            String password = "kucing";
          
          
            Koneksi = DriverManager.getConnection(url, user, password);
          
          
            Koneksi.setAutoCommit(false);
          
            if (Koneksi != null) {
              
              
                PSInsert = Koneksi.prepareStatement(masukanData);
              
              
                PSInsert.setDate(1, tanggalMulai);
              
                PSInsert.setDate(2, tanggalBerakhir);
              
                PSInsert.executeUpdate();
              
                Koneksi.commit();
              
                Koneksi.close();
              
            }
          
          
        } catch (SQLException ex) {
          
          
            System.out.println();
          
            ex.printStackTrace();
              
                      
                      
              
            }
                  
                  
          
      
     
     
     
     
     
     
      
      
    }                                      

    /**
     * @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(Frame4.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Frame4.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Frame4.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Frame4.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 Frame4().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify                   
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private com.toedter.calendar.JDateChooser jDateChooser1;
    private com.toedter.calendar.JDateChooser jDateChooser2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    // End of variables declaration                 
}




Penampakan di pgAdmin3 sebagai bukti data berhasil disimpan:


Konversi java.util.Date Ke java.sql.Date

tengah malam ketemu pemecahan masalah konversi nilai tanggal dari java.util.Date ke java.sql.Date . hasil merangkai dari berbagai tulisan disini:


http://stackoverflow.com/questions/530012/how-to-convert-java-util-date-to-java-sql-date

http://www.java2s.com/Tutorial/Java/0040__Data-Type/ConvertfromajavautilDateObjecttoajavasqlDateObject.htm

http://stackoverflow.com/questions/23564363/getting-value-from-jdatechooser-and-saving-to-ms-sql-db

http://www.java2s.com/Code/PostgreSQL/Insert-Delete-Update/Insertdatedatatotable.htm


http://stackoverflow.com/questions/23447694/problems-using-jdatechooser-with-a-database


http://www.java2s.com/Code/PostgreSQL/Insert-Delete-Update/Insertdatedatatotable.htm

hasil akhir formulasi codingnya:


 /*
 * 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.
 */
package latihan6;

import com.toedter.calendar.JDateChooser;

import javax.swing.*;

import java.awt.*;

import java.text.DateFormat;

import java.text.SimpleDateFormat;

import java.util.Calendar;

import java.util.Date;

import java.sql.*;

/**
 *
 * @author steven
 */
public class Frame5 extends javax.swing.JFrame {

    /**
     * Creates new form Frame5
     */
    public Frame5() {
        initComponents();
    }

    /**
     * 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">                        
    private void initComponents() {

        jDateChooser1 = new com.toedter.calendar.JDateChooser();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jDateChooser1.setFont(new java.awt.Font("Cantarell", 1, 17)); // NOI18N

        jButton1.setFont(new java.awt.Font("Cantarell", 1, 17)); // NOI18N
        jButton1.setText("Print java.util.Date");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setFont(new java.awt.Font("Cantarell", 1, 17)); // NOI18N
        jButton2.setText("Print java.sql.Date");
        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()
                .addGap(20, 20, 20)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButton1)
                        .addGap(18, 18, 18)
                        .addComponent(jButton2))
                    .addComponent(jDateChooser1, javax.swing.GroupLayout.PREFERRED_SIZE, 357, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jDateChooser1, javax.swing.GroupLayout.DEFAULT_SIZE, 41, Short.MAX_VALUE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 54, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 54, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(30, 30, 30))
        );

        pack();
    }// </editor-fold>                      

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                       
        // TODO add your handling code here:
      
        Date tanggal = jDateChooser1.getDate();
      
        System.out.print(tanggal);
      
      
    }                                      

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                       
        // TODO add your handling code here:
      
      
        // Date tanggal = jDateChooser1.getDate;
      
        java.sql.Date tanggal2 = new java.sql.Date(jDateChooser1.getDate().getTime());
      
        System.out.print(tanggal2);
      
      
      
      
      
      
    }                                      

    /**
     * @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(Frame5.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Frame5.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Frame5.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Frame5.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 Frame5().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify                   
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private com.toedter.calendar.JDateChooser jDateChooser1;
    // End of variables declaration                 
}


Penampakan Program Kecilnya Ketika Dijalankan:











kalau menjalankan tombol Print java.util.Date maka format tanggal yg keluar adalah :

Mon Nov 03 00:00:00 WITA 2014

kalau menjalankan tombol Print java.sql.Date maka format tanggal yg keluar adalah :

2014-11-03

 
Format tanggal 2014-11-03 inilah yg katanya bisa dimasukan ke kolom tabel PostgreSQL yg memiliki type data Date.


Bolak balik nyari cara supaya outputnya JDateChooser adalah 2014-11-03 . ternyata format defaultnya java.sql.Date udah seperti itu. yah udah cocok lah. dan ternyata output standardnya JDateChooser adalah java.util.Date , jadi kalau outputnya mau dimasukan ke database harus di konversi dulu ke java.sql.Date




Monday, November 10, 2014

Java SE : Kode Untuk JFrame, JTextField, JPanel, JButton yang Sudah Berhasil Berjalan

di bawah ini source codenya:

/*
 * 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.
 */
package latihan4;


import java.awt.*;

import javax.swing.*;

/**
 *
 * @author steven
 */
public class Frame13 extends JFrame {
   
   
    public static void main (String []args){
   
   
        // Bikin Frame dulu
       
       
        JFrame bingkai = new JFrame("Form Input Data");
       
        bingkai.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       
        bingkai.getContentPane().setBackground(Color.CYAN);
       
       
        bingkai.setLayout(new GridLayout(5,1));
       
       
       
       
        // Bikin Label
       
       
        JLabel idPenggunaLabel = new JLabel("ID Pengguna: ");
       
        JLabel namaLengkapLabel = new JLabel("Nama Lengkap: ");
       
        JLabel namaPenggunaLabel = new JLabel("Nama Pengguna: ");
       
        JLabel kataKunciLabel = new JLabel("Kata Kunci: ");
       
       
       
        // Bikin textbox
       
        JTextField idPenggunaText = new JTextField(10);
       
        JTextField namaLengkapText = new JTextField(10);
       
        JTextField namaPenggunaText = new JTextField(10);
       
        JTextField kataKunciText = new JTextField(10);
       
       
       
       
       
        // Bikin tombol
       
       
        JButton simpan = new JButton("Simpan");
       
        JButton update = new JButton("Update");
       
        JButton hapus = new JButton("Hapus");
       
       
       
       
        // Bikin Panel
       
       
        JPanel idPenggunaPanel = new JPanel();
       
        idPenggunaPanel.setBackground(Color.red);
       
       
        idPenggunaPanel.add(idPenggunaLabel);
       
        idPenggunaPanel.add(idPenggunaText);
       
       
       
       
       
        JPanel namaLengkapPanel = new JPanel();
       
        namaLengkapPanel.setBackground(Color.magenta);
       
        namaLengkapPanel.add(namaLengkapLabel);
       
        namaLengkapPanel.add(namaLengkapText);
       
       
       
       
       
       
       
        JPanel namaPenggunaPanel = new JPanel();
       
        namaPenggunaPanel.setBackground(Color.PINK);
       
       
        namaPenggunaPanel.add(namaPenggunaLabel);
       
        namaPenggunaPanel.add(namaPenggunaText);
       
       
       
       
       
        JPanel kataKunciPanel = new JPanel();
       
        kataKunciPanel.setBackground(Color.ORANGE);
       
        kataKunciPanel.add(kataKunciLabel);
       
        kataKunciPanel.add(kataKunciText);
       
       
       
       
       
       
        JPanel tombolPanel = new JPanel();
       
        tombolPanel.setBackground(Color.white);
       
        tombolPanel.add(simpan);
       
        tombolPanel.add(update);
       
        tombolPanel.add(hapus);
       
       
       
       
        bingkai.add(idPenggunaPanel);
       
        bingkai.add(namaLengkapPanel);
       
        bingkai.add(namaPenggunaPanel);
       
        bingkai.add(kataKunciPanel);
       
        bingkai.add(tombolPanel);
       
       
       
        bingkai.pack();
       
        bingkai.setSize(500, 500);
       
        bingkai.setVisible(true);
       
   
       
    }
   
}