You are on page 1of 9

/*

* 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 duk;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
/**
*
* @author zzz
*/
public class JformAgama extends javax.swing.JFrame {
private static Connection koneksi; //Koneksi
private DefaultTableModel model; //Model Tabel
/**
* Creates new form JformAgama
*/
public JformAgama() {
initComponents();
model=new DefaultTableModel();
this.jTable1.setModel(model);
model.addColumn("ID");
model.addColumn("Agama");
}
/**
* 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();
jLabel3 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
txtNamaAgama = new javax.swing.JTextField();
jScrollPane1 = new javax.swing.JScrollPane();
jTable1 = new javax.swing.JTable();
cmdAdd = new javax.swing.JButton();
cmdEdit = new javax.swing.JButton();
cmdDelete = new javax.swing.JButton();
cmdKeluar = new javax.swing.JButton();
cmdRefresh = new javax.swing.JButton();
txtId = new javax.swing.JTextField();
cmdSimpan = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setResizable(false);
addWindowListener(new java.awt.event.WindowAdapter() {
public void windowActivated(java.awt.event.WindowEvent evt) {

formWindowActivated(evt);
}
});
jLabel1.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N
jLabel1.setText("DATA AGAMA");
jLabel3.setText("Id Agama");
jLabel4.setText("Nama Agama");
jTable1.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{null, null, null, null},
{null, null, null, null},
{null, null, null, null},
{null, null, null, null}
},
new String [] {
"Title 1", "Title 2", "Title 3", "Title 4"
}
));
jTable1.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
jTable1MouseClicked(evt);
}
});
jScrollPane1.setViewportView(jTable1);
cmdAdd.setText("Add");
cmdAdd.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
cmdAddActionPerformed(evt);
}
});
cmdEdit.setText("Edit");
cmdEdit.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
cmdEditActionPerformed(evt);
}
});
cmdDelete.setText("Delete");
cmdDelete.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
cmdDeleteActionPerformed(evt);
}
});
cmdKeluar.setText("Exit");
cmdKeluar.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
cmdKeluarActionPerformed(evt);
}
});
cmdRefresh.setText("Refresh");
cmdRefresh.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {

cmdRefreshActionPerformed(evt);
}
});
cmdSimpan.setText("Simpan");
cmdSimpan.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
cmdSimpanActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentP
ane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING
)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Ali
gnment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLa
yout.Alignment.LEADING)
.addComponent(jLabel3)
.addComponent(jLabel4))
.addGap(39, 39, 39)
.addGroup(layout.createParallelGroup(javax.swing.GroupLa
yout.Alignment.LEADING)
.addComponent(txtNamaAgama)
.addGroup(layout.createSequentialGroup()
.addComponent(txtId, javax.swing.GroupLayout.PRE
FERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(0, 0, Short.MAX_VALUE))))
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRE
D_SIZE, 0, Short.MAX_VALUE)
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel1)
.addGap(0, 0, Short.MAX_VALUE))
.addGroup(layout.createSequentialGroup()
.addComponent(cmdAdd)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacem
ent.RELATED)
.addComponent(cmdEdit)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacem
ent.RELATED)
.addComponent(cmdDelete)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacem
ent.UNRELATED)
.addComponent(cmdRefresh)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacem
ent.RELATED, 79, Short.MAX_VALUE)
.addComponent(cmdKeluar)))
.addContainerGap())
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createS
equentialGroup()
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX
_VALUE)
.addComponent(cmdSimpan)
.addGap(94, 94, 94))

);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING
)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel1)
.addGap(28, 28, 28)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Ali
gnment.BASELINE)
.addComponent(jLabel3)
.addComponent(txtId, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELA
TED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Ali
gnment.BASELINE)
.addComponent(jLabel4)
.addComponent(txtNamaAgama, javax.swing.GroupLayout.PREFERRE
D_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_
SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELA
TED)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SI
ZE, 156, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELA
TED, 19, Short.MAX_VALUE)
.addComponent(cmdSimpan)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRE
LATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Ali
gnment.BASELINE)
.addComponent(cmdAdd)
.addComponent(cmdEdit)
.addComponent(cmdDelete)
.addComponent(cmdKeluar)
.addComponent(cmdRefresh))
.addContainerGap())
);
pack();
}// </editor-fold>
private void cmdAddActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
Connection c=buka_koneksi(); //panggil function koneksi
if("Add".equals(this.cmdAdd.getText())) //Jika Button Add teks nya Add
{
autonomor ();
this.cmdAdd.setText("Save"); //Buat Teks Button Add yaitu Save
this.cmdEdit.setText("Cancel");
this.cmdDelete.enable(false);
this.cmdRefresh.enable(false);
txtId.enable(false);
txtNamaAgama.enable(true);
//this.lblKode.setText("0"); //Kosongkan Nilai LabelKode
// this.txtId.setText("");

this.txtNamaAgama.setText("");
txtNamaAgama.requestFocus();
}else if("Save".equals(this.cmdAdd.getText()))
{
//bikin sql query tambah data
String sqlkode="Insert into t_agama (idAgama, uAgama) "
+ "values ("+this.txtId.getText()+","
+ "'"+this.txtNamaAgama.getText()+"')";
try { //jalankan query tersebut
PreparedStatement p2=(PreparedStatement) c.prepareStatement(sqlko
de);
p2.executeUpdate();
p2.close();
} catch (SQLException ex) {
JOptionPane.showMessageDialog(this, "Terjadi kesalahan "+ex.getM
essage());
}
this.cmdAdd.setText("Add");
this.cmdEdit.setText("Edit");
this.cmdDelete.enable(true);
this.cmdRefresh.enable(true);
this.txtId.setText("");
}else if("Update".equals(this.cmdAdd.getText()))
{//bikin query update data
String sqlkode="Update t_agama SET uAgama='"+this.txtNamaAgama.getT
ext()+"'"
+ "Where idAgama='"+this.txtId.getText()+"'";
try {
PreparedStatement p2=(PreparedStatement) c.prepareStatement(sqlko
de);
p2.executeUpdate();
p2.close();
} catch (SQLException ex) {
JOptionPane.showMessageDialog(this, "Terjadi kesalahan "+ex.getM
essage());
}
this.cmdAdd.setText("Add");
this.cmdEdit.setText("Edit");
this.cmdDelete.enable(true);
this.cmdRefresh.enable(true);
this.txtId.setText("");
}
// Bersihkan layar kembali
ambil_data_tabel();
this.txtNamaAgama.setText("");
}
private void cmdEditActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
if("Edit".equals(this.cmdEdit.getText()))
{
this.cmdAdd.setText("Update");
this.cmdEdit.setText("Cancel");
this.cmdDelete.enable(false);
this.cmdRefresh.enable(false);

this.txtNamaAgama.enable(true);
txtNamaAgama.requestFocus();
}else if("Cancel".equals(this.cmdEdit.getText()))
{
this.cmdAdd.setText("Add");
this.cmdEdit.setText("Edit");
this.cmdDelete.enable(true);
this.cmdRefresh.enable(true);
this.txtNamaAgama.enable(false);
//bersihkan layar kembali
ambil_data_tabel();
this.txtId.setText("");
this.txtNamaAgama.setText("");
}
}
private void cmdDeleteActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
Connection c=buka_koneksi();
String sqlkode="Delete from t_agama "
+ "Where idAgama='"+this.txtId.getText()+"'";
try {
PreparedStatement p2=(PreparedStatement) c.prepareStatement(sqlko
de);
p2.executeUpdate();
p2.close();
} catch (SQLException ex) {
JOptionPane.showMessageDialog(this, "Terjadi kesalahan "+ex.getM
essage());
}
}
private void cmdRefreshActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
ambil_data_tabel();
this.txtId.setText("");
this.txtNamaAgama.setText("");
}
private void jTable1MouseClicked(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
this.ambil_tabel_klik();
}
private void cmdKeluarActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
this.dispose();
}
private void formWindowActivated(java.awt.event.WindowEvent evt) {
// TODO add your handling code here:
txtId.enable(false);
txtNamaAgama.enable(false);
}

private void cmdSimpanActionPerformed(java.awt.event.ActionEvent evt) {


// TODO add your handling code here:
//bikin sql query tambah data
String sqlkode="Insert into t_agama (idAgama, uAgama) "
+ "values ("+this.txtId.getText()+","
+ "'"+this.txtNamaAgama.getText()+"')";
try { //jalankan query tersebut
PreparedStatement p2=(PreparedStatement) c.prepareStatement(sqlko
de);
p2.executeUpdate();
p2.close();
} catch (SQLException ex) {
JOptionPane.showMessageDialog(this, "Terjadi kesalahan "+ex.getM
essage());
}
this.cmdDelete.enable(true);
this.cmdRefresh.enable(true);
this.txtId.setText("");
}
private static Connection buka_koneksi() {
if (koneksi==null) {
try {
String url="jdbc:mysql://localhost:3306/DUK"; //nama database pe
rpustakaan
String user="root"; //user mysql
String password=""; //password mysql
DriverManager.registerDriver(new com.mysql.jdbc.Driver());
koneksi=DriverManager.getConnection(url,user,password);
}catch (SQLException t) {
System.out.println("Error membuat koneksi");
}
}
return koneksi;
}
/**
* @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 cod
e (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the d
efault look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lo
okandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIMana
ger.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}

} catch (ClassNotFoundException ex) {


java.util.logging.Logger.getLogger(JformAgama.class.getName()).log(j
ava.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(JformAgama.class.getName()).log(j
ava.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(JformAgama.class.getName()).log(j
ava.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(JformAgama.class.getName()).log(j
ava.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new JformAgama().setVisible(true);
}
});
}
private void ambil_data_tabel()
{
model.getDataVector().removeAllElements();
model.fireTableDataChanged();
try {
Connection c=buka_koneksi();
Statement s= c.createStatement();
String sql="Select * from t_agama";
ResultSet r=s.executeQuery(sql);
while (r.next()) {
Object[] o=new Object[2];
o[0]=r.getString("idAgama");
o[1]=r.getString("uAgama");
model.addRow(o);
}
r.close();
s.close();
ambil_tabel_klik();
}catch(SQLException e) {
System.out.println("Terjadi kesalahan "+e.getMessage());
}
}
private void ambil_tabel_klik()
{
int i=this.jTable1.getSelectedRow();
if(i==-1)
{
return;
}
String kode=(String) model.getValueAt(i, 0);
this.txtId.setText(kode);
String nama=(String) model.getValueAt(i, 1);
this.txtNamaAgama.setText(nama);

}
private void autonomor(){
try{
Connection c=buka_koneksi();
Statement s= c.createStatement();
String sql="select max(idAgama) from t_agama";
ResultSet r=s.executeQuery(sql);
while (r.next()){
int a = r.getInt(1);
this.txtId.setText(""+ Integer.toString(a+1));
}
r.close();
s.close();
}catch (Exception e){
System.out.println(""+ e.getMessage());
}
}
// Variables declaration - do not modify
private javax.swing.JButton cmdAdd;
private javax.swing.JButton cmdDelete;
private javax.swing.JButton cmdEdit;
private javax.swing.JButton cmdKeluar;
private javax.swing.JButton cmdRefresh;
private javax.swing.JButton cmdSimpan;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTable jTable1;
private javax.swing.JTextField txtId;
private javax.swing.JTextField txtNamaAgama;
// End of variables declaration
}

You might also like