Anda di halaman 1dari 20

Juni 4, 2016

[MODUL PRAKTIKUM PEMROGRAMAN BERORIENTASI OBYEK]

MODUL X
JAVA SWING JDBC CRUD

1.
a.
b.
c.

2.

Tujuan
Mahasiswa melakukan mengkoneksikan program Java GUI dengan basis data
Mahasiswa mampu mengimplementasikan CRUD melalui Java GUI.
Mahasiswa mampu mengolah statement SQL melalui program Java GUI.

Latihan praktikum
Pada kegiatan praktikum ini, IDE NetBeans digunakan sebagai alat perangkat lunak untuk
melakukan pemrograman Java. IDE NetBeans tidak wajib digunakan pada umumnya tidak wajib
atau bukan patokan sebagai IDE untuk program Java terutama untuk integrasinya ke database.
Adapun jenis database yang dipakai adalah MySQL yang terdapat pada program Web Server
seperti XAMPP, WAMP, dan lainnya. Kegiatan latihan terdiri dari kegiatan-kegiatan berikut ini:
1) Buka IDE NetBeans hingga muncul window yang siap pakai

Juni 4, 2016

[MODUL PRAKTIKUM PEMROGRAMAN BERORIENTASI OBYEK]

2) Aktifkan Web Server yang telah terinstal

3) Pastikan MySQL telah aktif, dan amati port yang dibuka (ditandai kotak warna merah)

4) Kembali ke IDE NetBeans, dan buat project baru (File-New Project)

Juni 4, 2016

[MODUL PRAKTIKUM PEMROGRAMAN BERORIENTASI OBYEK]

5) Pilih tipe Java Java Application, klik Next

6) Beri nama project, klik Finish

7) Project baru terbentuk

Juni 4, 2016

[MODUL PRAKTIKUM PEMROGRAMAN BERORIENTASI OBYEK]

8) Klik kanan Libraries Add Library

9) Cari dan Pilih JDBC MySQL Driver, Klik Add Library

10) Muncul driver MySQL

11) Klik kanan package, pilih New, pilih Other

Juni 4, 2016

[MODUL PRAKTIKUM PEMROGRAMAN BERORIENTASI OBYEK]

12) Pada Categories pilih Swing GUI Forms, dan pada File Types pilih JDialog Form, klik Next

13) Pada menu berikutnya, ganti nama form, dan klik Finish

14) Form terbentuk, lanjutkan dengan klik ganda Form tersebut

Juni 4, 2016

[MODUL PRAKTIKUM PEMROGRAMAN BERORIENTASI OBYEK]

15) Muncul menu Palette

16) Drag drop komponen Palette dengan struktur dan komponen yang sama pada contoh di
bawah

.
Nomor
1.
2.
3.
4.
5.
6.
7.
8.

Komponen
JTabel
JPanel1
JLabel1
JLabel2
JLabel3
JLabel4
JLabel5
JButton1

9.

JButton2

10.

JButton3

11.

JButton4

Property
model
border
text
text
text
text
text
text
variabel
text
variabel
text
variabel
text
variabel

Keterangan/nilai
Hapus semua baris yang tercipta secara default
Pilih titled border
Id Pegawai
Nama Lengkap
Jenis Kelamin
Departemen
Posisi
Tambah
tombol_tambah
Update
tombol_update
Hapus
tombol_hapus
Print All
tombol_print_all

Juni 4, 2016

12.

JButton5

13.

JTextField1

14.

JTextField2

15.

JCombo

16.

JTextField3

17.

JTextField4

18.

JTextField5

[MODUL PRAKTIKUM PEMROGRAMAN BERORIENTASI OBYEK]

text
variabel
text
variabel
text
variabel
text
variabel
text
variabel
text
variabel
text
variabel

Close
tombol_close
<kosong>
txtIdPegawai
<kosong>
txtNamaLengkap
Tambahkan item Laki-laki dan Perempuan
cboJenisKelamin
<kosong>
txtDepartemen
<kosong>
txtPosisi
<kosong>
txtGaji

17) Buat database beserta isinya berdasarkan keterangan di bawah ini.


No
1.
2.

Jenis
Database
Tabel

Attribut:

Attribut: Id_Pegawai
Attribut: nama_lengkap
Attribut: jenis_kelamin
Attribut: departemen
Attribut: posisi
Attibut: gaji
Tabel: data_pegawai
Id_Pegawai nama_lengkap
100
Bruce Wayne
101
Barbara Gordon

Keterangan
pegawai
data_pegawai
INTEGER (3) PRIMARY KEY NOT NULL
VARCHAR (40)
VARCHAR (10)
VARCHAR (45)
VARCHAR (45)
INTEGER (12)

jenis_kelamin departemen
Laki-laki
Admin
Perempuan
IT

posisi
CEO
Staff

gaji
18000
9000

18) Buat class Config.java untuk koneksi ke Form


package jdbc_crud;
public class Config {
public
public
public
public

static
static
static
static

final
final
final
final

String
String
String
String

DATABASE_NAME = "PEGAWAI";
DATABASE_SERVER = "localhost";
DATABASE_USER_ID = "root";
DATABASE_PASSWORD = "";

public static final String connection_url = "jdbc:mysql://" + DATABASE_SERVER +


"/" + DATABASE_NAME;
}

Juni 4, 2016

[MODUL PRAKTIKUM PEMROGRAMAN BERORIENTASI OBYEK]

19) Buat class DBUtilities.java untuk inisialisasi class dan database


package jdbc_crud;
import java.sql.*;
public class DBUtilities {
Connection connection = null;
Statement statement = null;
ResultSet resultSet = null;
public DBUtilities() throws SQLException {
try {
connection = DriverManager.getConnection(Config.connection_url,
Config.DATABASE_USER_ID, Config.DATABASE_PASSWORD);
} catch (SQLException ex) {
System.out.println("Terjadi kesalahan: " + ex.getMessage());
}
}
public Connection getConnection() {
return connection;
}
public void ExecuteSQLStatement(String sql_stmt) {
try {
statement = connection.createStatement();
statement.executeUpdate(sql_stmt);
} catch (SQLException ex) {
System.out.println("Terjadi kesalahan: " + ex.getMessage());
}
}
}

20) Buat class ResultSetTableModel.java untuk menerima parameter string statement SQL yang
digunakan untuk menerima data.
package jdbc_crud;
import java.sql.*;
import javax.swing.table.AbstractTableModel;
public class ResultSetTableModel extends AbstractTableModel {
private
private
private
private
private

Connection connection;
final Statement statement;
ResultSet resultSet;
ResultSetMetaData metaData;
int numberOfRows;

private boolean connectedToDatabase = false;


private void SetDatabaseURL() throws SQLException {
try {
connection = DriverManager.getConnection(Config.connection_url,
Config.DATABASE_USER_ID, Config.DATABASE_PASSWORD);
} catch (SQLException sex) {

Juni 4, 2016

[MODUL PRAKTIKUM PEMROGRAMAN BERORIENTASI OBYEK]

System.out.println(sex.getMessage());
}
}
public ResultSetTableModel(String query) throws SQLException {
SetDatabaseURL();
connection = DriverManager.getConnection(Config.connection_url,
Config.DATABASE_USER_ID, Config.DATABASE_PASSWORD);
statement = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
connectedToDatabase = true;
if (!connectedToDatabase) {
throw new IllegalStateException("Tidak tersambung dengan Database");
}
resultSet = statement.executeQuery(query);
metaData = resultSet.getMetaData();
resultSet.last();
numberOfRows = resultSet.getRow();
fireTableStructureChanged();
}
@Override
public Class getColumnClass(int column) throws IllegalStateException {
if (!connectedToDatabase) {
throw new IllegalStateException("Tidak tersambung dengan Database");
}
try {
String className = metaData.getColumnClassName(column + 1);
return Class.forName(className);
} catch (ClassNotFoundException | SQLException ex) {
System.out.println(ex.getMessage());
}
return Object.class;
}
@Override
public int getColumnCount() throws IllegalStateException {
if (!connectedToDatabase) {
throw new IllegalStateException("Tidak tersambung dengan Database");
}
try {
return metaData.getColumnCount();
} catch (SQLException sex) {
System.out.println(sex.getMessage());
}
return 0;
}
@Override
public String getColumnName(int column) throws IllegalStateException {
if (!connectedToDatabase) {
throw new IllegalStateException("Tidak tersambung dengan Database");

Juni 4, 2016

[MODUL PRAKTIKUM PEMROGRAMAN BERORIENTASI OBYEK]

}
try {
return metaData.getColumnName(column + 1);
} catch (SQLException sex) {
System.out.println(sex.getMessage());
}
return "";
}
@Override
public int getRowCount() throws IllegalStateException {
if (!connectedToDatabase) {
throw new IllegalStateException("Tidak tersambung dengan Database");
}
return numberOfRows;
}
@Override
public Object getValueAt(int row, int column)
throws IllegalStateException {
if (!connectedToDatabase) {
throw new IllegalStateException("Tidak tersambung dengan Database");
}
try {
resultSet.absolute(row + 1);
return resultSet.getObject(column + 1);
} catch (SQLException sex) {
System.out.println(sex.getMessage());
}
return "";
}
public void disconnectFromDatabase() {
if (connectedToDatabase) {
try {
resultSet.close();
statement.close();
connection.close();
} catch (SQLException sex) {
System.out.println(sex.getMessage());
} finally {
connectedToDatabase = false;
}
}
}
}

21) Pada class project yang utama, masukkan kode berikut


/*
* 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 jdbc_crud;

Juni 4, 2016
import
import
import
import
import

[MODUL PRAKTIKUM PEMROGRAMAN BERORIENTASI OBYEK]

java.sql.SQLException;
javax.swing.JOptionPane;
javax.swing.SwingConstants;
javax.swing.event.ListSelectionEvent;
javax.swing.table.DefaultTableCellRenderer;

/**
*
* @author Enggar
*/
public class FormCRUD extends javax.swing.JDialog {
boolean addRecord = false;
private void clearInputBoxes() {
txtIdPegawai.setText("");
txtNamaLengkap.setText("");
cboJenisKelamin.setSelectedItem("");
txtDepartemen.setText("");
txtPosisi.setText("");
txtGaji.setText("");
}
private void addNew() throws SQLException {
String sql_stmt = "INSERT INTO Data_Pegawai
(Id_Pegawai,nama_lengkap,departemen, jenis_kelamin,posisi,gaji)";
sql_stmt += " VALUES ('"+ txtIdPegawai.getText() + "','" +
txtNamaLengkap.getText() + "','" + cboJenisKelamin.getSelectedItem().toString()
+ "','" + txtDepartemen.getText() + "','" + txtPosisi.getText() + "','" +
txtGaji.getText() + "')";
DBUtilities dbUtilities = new DBUtilities();
dbUtilities.ExecuteSQLStatement(sql_stmt);
}
private void updateRecord() throws SQLException {
String sql_stmt = "UPDATE Data_Pegawai SET Id_Pegawai = '" +
txtIdPegawai.getText() + "'";
sql_stmt += "'nama_lengkap = '" + txtNamaLengkap.getText() + "'";
sql_stmt += ",jenis_kelamin = '" +
cboJenisKelamin.getSelectedItem().toString() + "'";
sql_stmt += ",departemen = '" + txtDepartemen.getText() + "'";
sql_stmt += ",posisi = '" + txtPosisi.getText() + "'";
sql_stmt += ",gaji = '" + txtGaji.getText() + "'";
sql_stmt += " WHERE Id_Pegawai = '" + txtIdPegawai.getText() + "'";
DBUtilities dbUtilities = new DBUtilities();
dbUtilities.ExecuteSQLStatement(sql_stmt);
}
private void deleteRecord() throws SQLException {
String sql_stmt = "DELETE FROM Data_Pegawai WHERE Id_Pegawai = '" +
txtIdPegawai.getText() + "'";
DBUtilities dbUtilities = new DBUtilities();
dbUtilities.ExecuteSQLStatement(sql_stmt);
}
private void loadRecords() throws SQLException {

Juni 4, 2016

[MODUL PRAKTIKUM PEMROGRAMAN BERORIENTASI OBYEK]

String sql_stmt = "SELECT * FROM Data_Pegawai;";


ResultSetTableModel tableModel = new ResultSetTableModel(sql_stmt);
jTable1.setModel(tableModel);
jTable1.getSelectionModel().addListSelectionListener((ListSelectionEvent event)
-> {
try {
if (jTable1.getSelectedRow() >= 0) {
Object Id_Pegawai =
jTable1.getValueAt(jTable1.getSelectedRow(), 0);
Object nama_lengkap =
jTable1.getValueAt(jTable1.getSelectedRow(), 1);
Object jenis_kelamin =
jTable1.getValueAt(jTable1.getSelectedRow(), 2);
Object departemen =
jTable1.getValueAt(jTable1.getSelectedRow(), 3);
Object posisi =
jTable1.getValueAt(jTable1.getSelectedRow(), 4);
Object gaji = jTable1.getValueAt(jTable1.getSelectedRow(),
5);
txtIdPegawai.setText(Id_Pegawai.toString());
txtNamaLengkap.setText(nama_lengkap.toString());
cboJenisKelamin.setSelectedItem(jenis_kelamin.toString());
txtDepartemen.setText(departemen.toString());
txtPosisi.setText(posisi.toString());
txtGaji.setText(gaji.toString());
}
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
});
DefaultTableCellRenderer rightRenderer = new
DefaultTableCellRenderer();
rightRenderer.setHorizontalAlignment(SwingConstants.LEFT);
jTable1.getColumnModel().getColumn(0).setCellRenderer(rightRenderer);
}
/**
* Creates new form FormCRUD
*/
public FormCRUD(java.awt.Frame parent, boolean modal) {
super(parent, modal);
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() {
jScrollPane1 = new javax.swing.JScrollPane();
jTable1 = new javax.swing.JTable();

Juni 4, 2016

[MODUL PRAKTIKUM PEMROGRAMAN BERORIENTASI OBYEK]

jPanel1 = new javax.swing.JPanel();


jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
jLabel5 = new javax.swing.JLabel();
jLabel6 = new javax.swing.JLabel();
txtIdPegawai = new javax.swing.JTextField();
txtPosisi = new javax.swing.JTextField();
txtNamaLengkap = new javax.swing.JTextField();
txtGaji = new javax.swing.JTextField();
txtDepartemen = new javax.swing.JTextField();
cboJenisKelamin = new javax.swing.JComboBox<>();
tombol_tambah = new javax.swing.JButton();
tombol_update = new javax.swing.JButton();
tombol_hapus = new javax.swing.JButton();
tombol_print_all = new javax.swing.JButton();
tombol_close = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
jTable1.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{},
{},
{},
{}
},
new String [] {
}
));
jScrollPane1.setViewportView(jTable1);
jPanel1.setBorder(javax.swing.BorderFactory.createLineBorder(new
java.awt.Color(0, 0, 0)));
jPanel1.setToolTipText("Menu");
jPanel1.setName("Menu"); // NOI18N
jLabel1.setText("ID Karyawan");
jLabel2.setText("Nama Lengkap");
jLabel3.setText("Jenis Kelamin");
jLabel4.setText("Posisi");
jLabel5.setText("Departemen");
jLabel6.setText("Gaji");
cboJenisKelamin.setModel(new javax.swing.DefaultComboBoxModel<>(new
String[] { "Laki-laki", "Perempuan" }));
cboJenisKelamin.setToolTipText("");
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()

Juni 4, 2016

[MODUL PRAKTIKUM PEMROGRAMAN BERORIENTASI OBYEK]

.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.L
EADING)
.addComponent(jLabel1)
.addComponent(jLabel4)
.addComponent(jLabel3)
.addComponent(jLabel2))
.addGap(22, 22, 22)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.L
EADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.L
EADING)
.addComponent(txtNamaLengkap,
javax.swing.GroupLayout.PREFERRED_SIZE, 146,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(txtIdPegawai,
javax.swing.GroupLayout.PREFERRED_SIZE, 146,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap())
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.L
EADING)
.addComponent(txtPosisi,
javax.swing.GroupLayout.PREFERRED_SIZE, 146,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(cboJenisKelamin,
javax.swing.GroupLayout.PREFERRED_SIZE, 99,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(44, 44, 44)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.L
EADING, false)
.addComponent(jLabel6)
.addComponent(jLabel5))
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.L
EADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 20,
Short.MAX_VALUE)
.addComponent(txtGaji,
javax.swing.GroupLayout.PREFERRED_SIZE, 146,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(42, 42, 42))
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(18, 18, 18)
.addComponent(txtDepartemen,
javax.swing.GroupLayout.PREFERRED_SIZE, 146,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()

Juni 4, 2016

[MODUL PRAKTIKUM PEMROGRAMAN BERORIENTASI OBYEK]

.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.B
ASELINE)
.addComponent(jLabel1)
.addComponent(txtIdPegawai,
javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(20, 20, 20)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.B
ASELINE)
.addComponent(jLabel2)
.addComponent(txtNamaLengkap,
javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.B
ASELINE)
.addComponent(jLabel3)
.addComponent(jLabel5)
.addComponent(cboJenisKelamin,
javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(txtDepartemen,
javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.B
ASELINE)
.addComponent(jLabel4)
.addComponent(jLabel6)
.addComponent(txtPosisi,
javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(txtGaji,
javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE,
Short.MAX_VALUE))
);
tombol_tambah.setText("Tambah");
tombol_tambah.setActionCommand("tombol_tambah");
tombol_tambah.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
tombol_tambahActionPerformed(evt);
}
});
tombol_update.setText("Update");
tombol_update.setActionCommand("tombol_update");
tombol_update.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
tombol_updateActionPerformed(evt);
}
});
tombol_hapus.setText("Hapus");
tombol_hapus.setActionCommand("tombol_hapus");
tombol_hapus.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
tombol_hapusActionPerformed(evt);

Juni 4, 2016

[MODUL PRAKTIKUM PEMROGRAMAN BERORIENTASI OBYEK]

}
});
tombol_print_all.setText("Print All");
tombol_print_all.setActionCommand("tombol_print_all");
tombol_print_all.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(java.awt.event.ActionEvent evt) {
tombol_print_allActionPerformed(evt);
}
});
tombol_close.setText("Close");
tombol_close.setActionCommand("tombol_close");
tombol_close.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
tombol_closeActionPerformed(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)
.addComponent(jPanel1,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
Short.MAX_VALUE)
.addComponent(jScrollPane1)
.addGroup(layout.createSequentialGroup()
.addGap(57, 57, 57)
.addComponent(tombol_tambah)
.addGap(18, 18, 18)
.addComponent(tombol_update)
.addGap(18, 18, 18)
.addComponent(tombol_hapus)
.addGap(18, 18, 18)
.addComponent(tombol_print_all)
.addGap(18, 18, 18)
.addComponent(tombol_close)))
.addContainerGap(19, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jScrollPane1,
javax.swing.GroupLayout.PREFERRED_SIZE, 91,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)

Juni 4, 2016

[MODUL PRAKTIKUM PEMROGRAMAN BERORIENTASI OBYEK]

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE
)
.addComponent(tombol_tambah)
.addComponent(tombol_update)
.addComponent(tombol_hapus)
.addComponent(tombol_print_all)
.addComponent(tombol_close))
.addContainerGap())
);
jPanel1.getAccessibleContext().setAccessibleName("Menu Pegawai");
pack();
}// </editor-fold>
private void formWindowOpened(java.awt.event.WindowEvent evt) {
try {
loadRecords();
} catch (SQLException ex) {
System.out.println(ex.getMessage());
}
}
private void tombol_tambahActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
addRecord = true;
clearInputBoxes();
txtNamaLengkap.requestFocus();
}
private void tombol_hapusActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
int dialogResult = JOptionPane.showConfirmDialog(null, "Yakin mau
hapus?", "Konfirmasi penghapusan?", JOptionPane.YES_NO_OPTION);
if (dialogResult == JOptionPane.YES_OPTION) {
try {
deleteRecord();
loadRecords();
} catch (SQLException ex) {
System.out.println(ex.getMessage());
}
}
}

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


// TODO add your handling code here:
System.exit(1);
}
private void tombol_updateActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
int dialogResult = JOptionPane.showConfirmDialog(null, "Yakin mau
update?", "KOnfirmasi update?", JOptionPane.YES_NO_OPTION);
if (dialogResult == JOptionPane.YES_OPTION) {

Juni 4, 2016

[MODUL PRAKTIKUM PEMROGRAMAN BERORIENTASI OBYEK]

try {
if (addRecord == true) {
addNew();
} else {
updateRecord();
}
addRecord = false;
loadRecords();
} catch (SQLException ex) {
System.out.println(ex.getMessage());
}
}
}
private void tombol_print_allActionPerformed(java.awt.event.ActionEvent
evt) {
// TODO add your handling code here:
//DisplayReports displayReports = new DisplayReports();
//displayReports.showEmployees();
}
/**
* @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(FormCRUD.class.getName()).log(java.util.logg
ing.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(FormCRUD.class.getName()).log(java.util.logg
ing.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(FormCRUD.class.getName()).log(java.util.logg
ing.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(FormCRUD.class.getName()).log(java.util.logg
ing.Level.SEVERE, null, ex);
}
//</editor-fold>

Juni 4, 2016

[MODUL PRAKTIKUM PEMROGRAMAN BERORIENTASI OBYEK]

/* Create and display the dialog */


java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
FormCRUD dialog = new FormCRUD(new javax.swing.JFrame(), true);
dialog.addWindowListener(new java.awt.event.WindowAdapter() {
@Override
public void windowClosing(java.awt.event.WindowEvent e) {
System.exit(0);
}
});
dialog.setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JComboBox<String> cboJenisKelamin;
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.JPanel jPanel1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTable jTable1;
private javax.swing.JButton tombol_close;
private javax.swing.JButton tombol_hapus;
private javax.swing.JButton tombol_print_all;
private javax.swing.JButton tombol_tambah;
private javax.swing.JButton tombol_update;
private javax.swing.JTextField txtDepartemen;
private javax.swing.JTextField txtGaji;
private javax.swing.JTextField txtIdPegawai;
private javax.swing.JTextField txtNamaLengkap;
private javax.swing.JTextField txtPosisi;
// End of variables declaration
}

22) Update JDBC_CRUD.java


/*
* 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 jdbc_crud;
import javax.swing.JDialog;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
/**
*
* @author Enggar
*/
public class JDBC_CRUD {
/**
* @param args the command line arguments

Juni 4, 2016

[MODUL PRAKTIKUM PEMROGRAMAN BERORIENTASI OBYEK]

*/
public static void main(String[] args) {
// TODO code application logic here
try {
// Set System L&F
UIManager.setLookAndFeel(
UIManager.getSystemLookAndFeelClassName());
} catch (UnsupportedLookAndFeelException | ClassNotFoundException |
InstantiationException | IllegalAccessException ex) {
System.out.println(ex.getMessage());
}
FormCRUD sForm = new FormCRUD(null, false);
sForm.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
sForm.pack();
sForm.setLocationRelativeTo(null);
sForm.setVisible(true);
}
}

23) Jalankan project

3. Tugas Praktikum
1) Rubah Id_Pegawai menjadi auto_increment sehingga pada saat penambahan data, tidak
perlu menginputkan ID Pegawai!
2) Tombol Print All belum berfungsi dengan baik. Buat hasil eksekusi tombol Print All untuk
menampilkan data dalam bentuk laporan menggunakan Jasper Reports (plugin telah
disediakan, diperbolehkan menggunakan versi lain)!