0% found this document useful (0 votes)
91 views

Membuat Form Data Buku: TXTKD Txtjudul Cbkategori

The document describes the code for a Java GUI application to manage book data. It includes methods for initializing the form, clearing fields, populating a JTable from a database, and handling buttons to save, update, delete and cancel book records. The code imports SQL connection classes, declares form components as private fields and uses a DefaultTableModel to populate and edit the table.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
91 views

Membuat Form Data Buku: TXTKD Txtjudul Cbkategori

The document describes the code for a Java GUI application to manage book data. It includes methods for initializing the form, clearing fields, populating a JTable from a database, and handling buttons to save, update, delete and cancel book records. The code imports SQL connection classes, declares form components as private fields and uses a DefaultTableModel to populate and edit the table.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Membuat Form Data Buku

txtkd
txtjudul
cbkategori

txtsinopsis

bsimpan bubah bhapus bbatal bkeluar

tabelbuku

1. Import dan Method Buku

package tampilan;
import java.sql.*;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import koneksi.koneksi;

public class formBuku extends javax.swing.JFrame {


private Connection conn = new koneksi().connect();
private DefaultTableModel tabmode;

public formbuku() {
initComponents();
kosong();
aktif();
datatable();
}
2. Method Aktif

protected void aktif(){


txtkd.requestFocus();
cbkategori.setSelectedItem(null);
}

3. Method Kosong

protected void kosong(){


txtkd.setText("");
txtjudul.setText("");
cbkategori.setSelectedItem(null);
txtsinopsis.setText("");
}

4. Datatable

protected void datatable(){


Object[] Baris ={"Kode Buku","Judul Buku","kategori","Sinopsis"};
tabmode = new DefaultTableModel(null, Baris);

try {
String sql = "SELECT * FROM buku";
java.sql.Statement stat = conn.createStatement();
ResultSet hasil = stat.executeQuery(sql);
while (hasil.next()){
tabmode.addRow(new Object[]{
hasil.getString(1),
hasil.getString(2),
hasil.getString(3),
hasil.getString(4)
});
}
tabelbuku.setModel(tabmode);
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "Data Gagal Tampil" +e);
}
}
5. Tombol Simpan

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


String sql = "insert into buku values (?,?,?,?)";
try {
PreparedStatement stat = conn.prepareStatement (sql);
stat.setString (1, txtkd.getText());
stat.setString (2, txtjudul.getText());
stat.setString (3, cbkategori.getSelectedItem().toString());
stat.setString (4, txtsinopsis.getText());

stat.executeUpdate();
JOptionPane.showMessageDialog(null, "Data Berhasil Disimpan");
kosong ();
txtkd.requestFocus();
}
catch (SQLException e) {
JOptionPane.showMessageDialog(null, "Data Gagal Disimpan "+e);
}
datatable();
}

6. Tombol Ubah

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


try{
String sql = "update buku set judul=?,kategori=?,sinopsis=? where kdbuku ='"+txtkd.getText()+"'";
PreparedStatement stat = conn.prepareStatement(sql);
stat.setString(1, txtjudul.getText());
stat.setString(2, cbkategori.getSelectedItem().toString());
stat.setString(3, txtsinopsis.getText());

stat.executeUpdate();
JOptionPane.showMessageDialog(null, "Data Berhasil Diubah");
kosong();
txtkd.requestFocus();
}
catch (SQLException e){
JOptionPane.showMessageDialog(null, "Data Gagal Diubah"+e);
}
datatable();
}
7. Tombol Hapus

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


int ok = JOptionPane.showConfirmDialog(null,"Hapus","Konfirmasi Dialog",JOptionPane.YES_NO_OPTION);
if (ok==0){
String sql = "delete from buku where kdbuku ='"+txtkd.getText()+"'";
try{
PreparedStatement stat = conn.prepareStatement(sql);
stat.executeUpdate();
JOptionPane.showMessageDialog(null, "Data Berhasil Dihapus");
kosong();
txtkd.requestFocus();
}
catch (SQLException e){
JOptionPane.showMessageDialog(null, "Data Gagal Dihapus"+e);
}
datatable();
}
}

8. Tombol Keluar

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


dispose();
}

9. Tombol Batal

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


kosong();
datatable();
}

10. Table Klik (Events>Mouse>mouseClicked)

private void tabelbukuMouseClicked(java.awt.event.MouseEvent evt) {


int bar = tabelbuku.getSelectedRow();
String a = tabmode.getValueAt(bar, 0).toString();
String b = tabmode.getValueAt(bar, 1).toString();
String c = tabmode.getValueAt(bar, 2).toString();
String d = tabmode.getValueAt(bar, 3).toString();

txtkd.setText(a);
txtjudul.setText(b);
cbkategori.setSelectedItem(c);
txtsinopsis.setText(d);
}

You might also like