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: