Berikut tampilan programnya :
Berikut listing programnya :
package kategori;
import java.sql.Statement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import javax.swing.JOptionPane;
/**
*
* @author m4rry
*/
public class Kategori_1 extends javax.swing.JFrame {
/** Creates new form Kategori_1 */
public Kategori_1() {
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();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
f1 = new javax.swing.JTextField();
f2 = new javax.swing.JTextField();
b_tambah = new javax.swing.JButton();
b_simpan = new javax.swing.JButton();
b_edit = new javax.swing.JButton();
b_hapus = new javax.swing.JButton();
b_exit = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jLabel1.setText("KATEGORI");
jLabel2.setText("kode kategori :");
jLabel3.setText("Nama Kategori :");
b_tambah.setText("Tambah");
b_tambah.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
b_tambahActionPerformed(evt);
}
});
b_simpan.setText("Simpan");
b_simpan.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
b_simpanActionPerformed(evt);
}
});
b_edit.setText("edit");
b_edit.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
b_editActionPerformed(evt);
}
});
b_hapus.setText("Hapus");
b_hapus.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
b_hapusActionPerformed(evt);
}
});
b_exit.setText("Exit");
b_exit.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
b_exitActionPerformed(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()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(163, 163, 163)
.addComponent(jLabel1))
.addGroup(layout.createSequentialGroup()
.addGap(73, 73, 73)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel2)
.addComponent(jLabel3)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addComponent(b_edit, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(b_tambah, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(40, 40, 40)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(f2)
.addComponent(f1, javax.swing.GroupLayout.DEFAULT_SIZE, 123, Short.MAX_VALUE)))
.addGroup(layout.createSequentialGroup()
.addGap(29, 29, 29)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(bhapus, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(bsimpan, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGap(18, 18, 18)
.addComponent(b_exit, javax.swing.GroupLayout.PREFERRED_SIZE, 61, javax.swing.GroupLayout.PREFERRED_SIZE)))))
.addContainerGap(74, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(28, 28, 28)
.addComponent(jLabel1)
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(f1, 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(jLabel3)
.addComponent(f2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(29, 29, 29)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(b_tambah)
.addComponent(b_simpan))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(b_edit)
.addComponent(b_hapus)))
.addGroup(layout.createSequentialGroup()
.addGap(47, 47, 47)
.addComponent(b_exit)))
.addContainerGap(96, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void bhapusActionPerformed(java.awt.event.ActionEvent evt) {
String f_satu=f1.getText();
try
{
String dbname="jdbc:odbc:koneksi";
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection(dbname,"admin","admin");
Statement statement = con.createStatement();
String sql = "DELETE from vian Where kd_barang like '"+f_satu+"'";
statement.executeUpdate(sql);
btambah.doClick();
JOptionPane.showMessageDialog(null,"Data ditemukan","hapus data",JOptionPane.INFORMATION_MESSAGE);
statement.close();
con.close();
}
catch (Exception e)
{
JOptionPane.showMessageDialog(null,"kata m4rry : "+e,"Gagal",JOptionPane.WARNING_MESSAGE);
}
}
private void b_tambahActionPerformed(java.awt.event.ActionEvent evt) {
f1.setText("");
f2.setText("");
f1.requestFocus(true);
f2.requestFocus(true);
}
private void b_simpanActionPerformed(java.awt.event.ActionEvent evt) {
String f_satu=f1.getText();
String f_dua=f2.getText();
try
{
String dbname="jdbc:odbc:koneksi";
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection(dbname,"admin","admin");
java.sql.Statement statement = con.createStatement();
String sql = "insert into vian values('"+f_satu+"','"+f_dua+"');";
statement.executeUpdate(sql);
statement.close();
JOptionPane.showMessageDialog(null,"Data berhasil dimasukan..","m4rry", JOptionPane.INFORMATION_MESSAGE);
con.close();
}
catch(Exception e)
{
JOptionPane.showMessageDialog(null,"Error"+e,"Gagal",JOptionPane.WARNING_MESSAGE);
}
}
private void b_editActionPerformed(java.awt.event.ActionEvent evt) {
String f_satu=f1.getText();
String f_dua=f2.getText();
try{
String dbname="jdbc:odbc:koneksi";
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection(dbname,"admin","admin");
Statement statement = con.createStatement();
String sql = "UPDATE vian SET kode='"+f_satu+"',nama='"+f_dua+"'WHERE kode LIKE'"+f_satu+"'";
statement.executeUpdate(sql);
statement.close();
JOptionPane.showMessageDialog(null,"Data berhasil di Edit..","Edit Data",JOptionPane.INFORMATION_MESSAGE);
con.close();
}
catch (Exception e){
JOptionPane.showMessageDialog(null,"Error:"+e,"Gagal",JOptionPane.WARNING_MESSAGE);
}
}
private void b_exitActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
/**
* @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(kata_2.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(kata_2.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(kata_2.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(kata_2.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 Kategori_1().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton b_edit;
private javax.swing.JButton b_exit;
private javax.swing.JButton b_hapus;
private javax.swing.JButton b_simpan;
private javax.swing.JButton b_tambah;
private javax.swing.JTextField f1;
private javax.swing.JTextField f2;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
// End of variables declaration
}
Selamat Mencoba ye.................