Tutorial Membuat CRUD berbasis OOP

Jumpa lagi di tutorial Rekayasa Perangkat Lunak, pada kesempatan kali ini saya akan membuat tutorial mengenai Pembuatan CRUD berbasis OOP. Di tutorial ini kita akan mengenal tentang Database dan menerapkannya di konsep OOP.

Sebelumnya CRUD merupakan singkatan dari CREATE, READ, UPDATE, DELETE. Tidak asing lagi kata-kata tersebut merupakan kata-kata dimana kita akan membuat sebuah aplikasi yang bisa DIBUAT, DITAMPILKAN, DIPERBAHARUI dan DIHAPUS. Oke, langsung saja ikuti tutorialnya.

Perlengkapan

  • MySql Library
  • Netbeans
  • Design Aplikasi

Membuat Database Baru

[contoh-gambar]

Buatlah Database serta table yang sesuai dengan tampilan design yang sudah disediakan.

Membuat Codingan pada Class Koneksi

Buatlah Script untuk mengkoneksikan databasenya, berikut merupakan scriptnya.

public class KoneksiDatabase {

static final String DB_URL = “jdbc:mysql://localhost/javadatabase”;
static final String USER = “root”;
static final String PASSWORD = “”;
public static Connection conn = null;

void setdriver() {
try {
Class.forName(“com.mysql.jdbc.Driver”);
} catch (ClassNotFoundException ex) {
JOptionPane.showMessageDialog(null, “Error”, “Warning”, JOptionPane.OK_OPTION);
}
}

void koneksi() {

try {
conn = DriverManager.getConnection(DB_URL, USER, PASSWORD);
} catch (SQLException ex) {
JOptionPane.showMessageDialog(null, “Gagal Koneksi Database”, “Warning”, JOptionPane.OK_OPTION);
}
}

Membuat Codingan pada Tampilan Aplikasi

Berikut merupakan script untuk fungsi-fungsi setiap tombolnya.

public class PerpustakaanMiniDatabase extends javax.swing.JFrame {

public String a, b, c, d, e, f, g, h, i, j;
DefaultTableModel tabModel;

public PerpustakaanMiniDatabase() {
initComponents();
showData();
}

void showData() {
try {
KoneksiDatabase x = new KoneksiDatabase();
x.setdriver();
x.koneksi();
Object[] row = {“Kode Pelanggan”, “Nama Pelanggan”, “Jenis Buku”, “Nama Buku”, “Lama Pinjam”, “Total”};
tabModel = new DefaultTableModel(null, row);
jTable1.setModel(tabModel);
String query = “SELECT kodepelanggan, nama, jenis, namabuku, lama, total FROM miniperpus”;
Statement s = x.conn.createStatement();
ResultSet rs = s.executeQuery(query);
while (rs.next()) {
a = rs.getString(“kodepelanggan”);
b = rs.getString(“nama”);
c = rs.getString(“jenis”);
d = rs.getString(“namabuku”);
e = rs.getString(“lama”);
f = rs.getString(“total”);
String[] data = {a, b, c, d, e, f};
tabModel.addRow(data);
}
} catch (SQLException ex) {
JOptionPane.showMessageDialog(null, “Gagal Show dari Database”, “Warning”, JOptionPane.OK_OPTION);
}
}

/**
* 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() {

jPanel1 = new javax.swing.JPanel();
jLabel1 = new javax.swing.JLabel();
KodPel = new javax.swing.JTextField();
jLabel2 = new javax.swing.JLabel();
NamPel = new javax.swing.JTextField();
jLabel3 = new javax.swing.JLabel();
jScrollPane1 = new javax.swing.JScrollPane();
Alamats = new javax.swing.JTextArea();
jPanel2 = new javax.swing.JPanel();
jLabel4 = new javax.swing.JLabel();
KodBuk = new javax.swing.JTextField();
jLabel5 = new javax.swing.JLabel();
jLabel6 = new javax.swing.JLabel();
NamBuk = new javax.swing.JTextField();
jLabel7 = new javax.swing.JLabel();
LamPin = new javax.swing.JTextField();
CBJenis = new javax.swing.JComboBox();
jScrollPane2 = new javax.swing.JScrollPane();
jList1 = new javax.swing.JList();
jButton1 = new javax.swing.JButton();
jPanel4 = new javax.swing.JPanel();
jLabel11 = new javax.swing.JLabel();
jLabel12 = new javax.swing.JLabel();
bayarr = new javax.swing.JTextField();
jLabel13 = new javax.swing.JLabel();
jLabel14 = new javax.swing.JLabel();
jLabel15 = new javax.swing.JLabel();
outTot = new javax.swing.JLabel();
kembalian = new javax.swing.JLabel();
jButton3 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jScrollPane3 = new javax.swing.JScrollPane();
jTable1 = new javax.swing.JTable();
jButton4 = new javax.swing.JButton();
jButton5 = new javax.swing.JButton();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)), “Data Transaksi”));

jLabel1.setText(“Kode Pelanggan”);

jLabel2.setText(“Nama Pelanggan”);

jLabel3.setText(“Alamat Pelanggan”);

Alamats.setColumns(20);
Alamats.setRows(5);
jScrollPane1.setViewportView(Alamats);

javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel1)
.addComponent(jLabel2)
.addComponent(jLabel3))
.addGap(18, 18, 18)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane1)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(NamPel, javax.swing.GroupLayout.PREFERRED_SIZE, 187, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(KodPel, javax.swing.GroupLayout.PREFERRED_SIZE, 187, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(0, 66, Short.MAX_VALUE)))
.addContainerGap())
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1)
.addComponent(KodPel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(NamPel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel3)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);

jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)), “Data Transaksi”));

jLabel4.setText(“Kode Buku”);

jLabel5.setText(“Jenis Buku”);

jLabel6.setText(“Nama Buku”);

jLabel7.setText(“Lama Pinjam”);

LamPin.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
LamPinActionPerformed(evt);
}
});

CBJenis.setModel(new javax.swing.DefaultComboBoxModel(new String[] { “==SELECT==”, “Komik”, “Majalah”, “Koran”, “Novel” }));
CBJenis.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
CBJenisActionPerformed(evt);
}
});

jList1.setFont(new java.awt.Font(“Trebuchet MS”, 0, 14)); // NOI18N
jList1.setModel(new javax.swing.AbstractListModel() {
String[] strings = { “Komik\t: @3000”, “Majalah\t: @2000”, “Koran\t: @1000”, “Novel\t: @1000” };
public int getSize() { return strings.length; }
public Object getElementAt(int i) { return strings[i]; }
});
jList1.setEnabled(false);
jScrollPane2.setViewportView(jList1);

jButton1.setText(“Proses”);
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});

javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
jPanel2.setLayout(jPanel2Layout);
jPanel2Layout.setHorizontalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addContainerGap(20, Short.MAX_VALUE)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel4)
.addComponent(jLabel5)
.addComponent(jLabel6))
.addGap(18, 18, 18)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(CBJenis, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(KodBuk, javax.swing.GroupLayout.DEFAULT_SIZE, 160, Short.MAX_VALUE)
.addComponent(NamBuk)))
.addGroup(jPanel2Layout.createSequentialGroup()
.addComponent(jLabel7)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 97, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(LamPin, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE))))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 16, Short.MAX_VALUE)
.addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap())
);
jPanel2Layout.setVerticalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel4)
.addComponent(KodBuk, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel5)
.addComponent(CBJenis, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel6)
.addComponent(NamBuk, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(17, 17, 17)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel7)
.addComponent(LamPin, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGroup(jPanel2Layout.createSequentialGroup()
.addGap(2, 2, 2)
.addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 86, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGap(18, 18, 18)
.addComponent(jButton1)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);

jPanel4.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)), “Data Transaksi”));

jLabel11.setText(“Total”);

jLabel12.setText(“Bayar”);

bayarr.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
bayarrActionPerformed(evt);
}
});

jLabel13.setText(“Kembali”);

jLabel14.setText(“Rp.”);

jLabel15.setText(“Rp.”);

outTot.setText(“null”);

kembalian.setText(“null”);

jButton3.setText(“Bayar”);
jButton3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton3ActionPerformed(evt);
}
});

javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
jPanel4.setLayout(jPanel4Layout);
jPanel4Layout.setHorizontalGroup(
jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel4Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel4Layout.createSequentialGroup()
.addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel12)
.addComponent(jLabel11))
.addGap(26, 26, 26)
.addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel4Layout.createSequentialGroup()
.addComponent(jLabel14)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(outTot))
.addComponent(bayarr, javax.swing.GroupLayout.PREFERRED_SIZE, 187, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGroup(jPanel4Layout.createSequentialGroup()
.addComponent(jLabel13)
.addGap(18, 18, 18)
.addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel4Layout.createSequentialGroup()
.addComponent(jLabel15)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(kembalian))
.addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 113, javax.swing.GroupLayout.PREFERRED_SIZE))))
.addContainerGap(42, Short.MAX_VALUE))
);
jPanel4Layout.setVerticalGroup(
jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel4Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel11)
.addComponent(jLabel14)
.addComponent(outTot))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel12)
.addComponent(bayarr, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel13)
.addComponent(jLabel15)
.addComponent(kembalian))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);

jButton2.setFont(new java.awt.Font(“Tahoma”, 1, 18)); // NOI18N
jButton2.setText(“Simpan”);
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});

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, null, null},
{null, null, null, null, null, null}
},
new String [] {
“Kode Pelanggan”, “Nama Pelanggan”, “Jenis Buku”, “Nama Buku”, “Lama Pinjam”, “Total”
}
) {
boolean[] canEdit = new boolean [] {
false, false, false, false, false, false
};

public boolean isCellEditable(int rowIndex, int columnIndex) {
return canEdit [columnIndex];
}
});
jTable1.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
jTable1MouseClicked(evt);
}
});
jScrollPane3.setViewportView(jTable1);

jButton4.setFont(new java.awt.Font(“Tahoma”, 1, 18)); // NOI18N
jButton4.setText(“Hapus”);
jButton4.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton4ActionPerformed(evt);
}
});

jButton5.setFont(new java.awt.Font(“Tahoma”, 1, 18)); // NOI18N
jButton5.setText(“Ubah”);
jButton5.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton5ActionPerformed(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, false)
.addGroup(layout.createSequentialGroup()
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(layout.createSequentialGroup()
.addComponent(jButton2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jButton5, javax.swing.GroupLayout.PREFERRED_SIZE, 88, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton4)
.addGap(148, 148, 148))))
.addGroup(layout.createSequentialGroup()
.addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 436, 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()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton5, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton4, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 196, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap())
);

pack();
}// </editor-fold>
public static double harga, total, lama, kembali, bayar;
public static String jenisbuku;

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

lama = Double.parseDouble(LamPin.getText());
total = harga * lama;
outTot.setText(String.valueOf(total));
}

private void CBJenisActionPerformed(java.awt.event.ActionEvent evt) {
if (this.CBJenis.getSelectedIndex() == 1) {
harga = 3000;
} else if (this.CBJenis.getSelectedIndex() == 2) {
harga = 2000;
} else if (this.CBJenis.getSelectedIndex() == 3) {
harga = 1000;
} else if (this.CBJenis.getSelectedIndex() == 4) {
harga = 1000;
}
}

private void LamPinActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}

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

}

private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
bayar = Double.parseDouble(bayarr.getText());
kembali = bayar – total;
kembalian.setText(String.valueOf(kembali));
}

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
try {
KoneksiDatabase x = new KoneksiDatabase();
x.setdriver();
x.koneksi();
String query = “INSERT INTO miniperpus VALUES(?,?,?,?,?,?,?,?,?,?)”;
PreparedStatement ps = x.conn.prepareStatement(query);
ps.setString(1, KodPel.getText());
ps.setString(2, NamPel.getText());
ps.setString(3, Alamats.getText());
ps.setString(4, KodBuk.getText());
ps.setString(5, CBJenis.getSelectedItem().toString());
ps.setString(6, NamBuk.getText());
ps.setString(7, LamPin.getText());
ps.setString(8, outTot.getText());
ps.setString(9, bayarr.getText());
ps.setString(10, kembalian.getText());
ps.executeUpdate();
JOptionPane.showMessageDialog(null, “Berhasil Menyimpan”, “Warning”, JOptionPane.OK_OPTION);
} catch (SQLException ex) {
JOptionPane.showMessageDialog(null, “Gagal Pengiriman ke Database”, “Warning”, JOptionPane.OK_OPTION);
}
showData();
}

private void jTable1MouseClicked(java.awt.event.MouseEvent evt) {
try {
int row = jTable1.getSelectedRow();
String klik = (jTable1.getModel().getValueAt(row, 0).toString());

KoneksiDatabase x = new KoneksiDatabase();
x.setdriver();
x.koneksi();
String query = “SELECT * FROM miniperpus WHERE kodepelanggan = ‘” + klik + “‘”;
Statement s = x.conn.createStatement();
ResultSet rs = s.executeQuery(query);
if (rs.next()) {
a = rs.getString(“kodepelanggan”);
KodPel.setText(a);
b = rs.getString(“nama”);
NamPel.setText(b);
c = rs.getString(“alamat”);
Alamats.setText(c);
d = rs.getString(“kodebuku”);
KodBuk.setText(d);
e = rs.getString(“jenis”);
CBJenis.setSelectedItem(e);
f = rs.getString(“namabuku”);
NamBuk.setText(f);
g = rs.getString(“lama”);
LamPin.setText(g);
h = rs.getString(“total”);
outTot.setText(h);
i = rs.getString(“bayar”);
bayarr.setText(i);
j = rs.getString(“kembali”);
kembalian.setText(j);
}
} catch (SQLException ex) {
JOptionPane.showMessageDialog(null, “Gagal Pengiriman ke Database”, “Warning”, JOptionPane.OK_OPTION);
}
}

private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {
int a;
String kode = KodPel.getText();
a = JOptionPane.showConfirmDialog(null, “Are you sure?” + kode + “Want to Delete?”, “Warning”, JOptionPane.YES_NO_OPTION);

if (a == JOptionPane.YES_NO_OPTION) {
try {
KoneksiDatabase x = new KoneksiDatabase();
x.setdriver();
x.koneksi();
String query = “DELETE FROM miniperpus WHERE kodepelanggan = ‘” + kode + “‘”;
PreparedStatement ps = x.conn.prepareStatement(query);
ps.executeUpdate();
JOptionPane.showMessageDialog(null, “Berhasil diHapus”, “Info”, JOptionPane.OK_OPTION);
showData();
} catch (SQLException ex) {
JOptionPane.showMessageDialog(null, “Gagal diHapus”, “Warning”, JOptionPane.OK_OPTION);
}
}
}

private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {
try {
KoneksiDatabase x = new KoneksiDatabase();
x.setdriver();
x.koneksi();
String kode = KodPel.getText();
String query = “UPDATE miniperpus SET kodepelanggan=?, nama=?, alamat=?, kodebuku=?, jenis=?, namabuku=?, lama=?, total=?, bayar=?,”
+ “kembali=? WHERE kodepelanggan='” + kode + “‘”;
PreparedStatement ps = x.conn.prepareStatement(query);
ps.setString(1, KodPel.getText());
ps.setString(2, NamPel.getText());
ps.setString(3, Alamats.getText());
ps.setString(4, KodBuk.getText());
ps.setString(5, CBJenis.getSelectedItem().toString());
ps.setString(6, NamBuk.getText());
ps.setString(7, LamPin.getText());
ps.setString(8, outTot.getText());
ps.setString(9, bayarr.getText());
ps.setString(10, kembalian.getText());
ps.executeUpdate();
JOptionPane.showMessageDialog(null, “Berhasil Mengubah Data”, “Warning”, JOptionPane.OK_OPTION);
} catch (SQLException ex) {
JOptionPane.showMessageDialog(null, “Gagal Update ke Database”, “Warning”, JOptionPane.OK_OPTION);
}
showData();
}

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

Hasil Akhir

Hasilnya akan terlihat seperti ini, sangat mudah sekali bukan? Kalau masih bingung saya akan share projeknya

JavaDatabase