You are on page 1of 6

Pemrograman CRUD menggunakan database MySQL

Komponen form:

- jLabel (4)
- jTextField (2)
- jRadioButton (2)
- jComboBox (1)
- jButton (4)
- jTable (1)
- buttonGroup (1)

A. Database MySQL
(1) Di netbenas
Di menu project:
- Klik kanan Library
- Klik Add library
- Klik 2x MySQL JDBC Driver
(2) Di localhost/phpmyadmin
Buat database dan tabel
Atribut tabel :

Nama Field Tipe Data Volume Keterangan


NIM Varchar 11 Primary key
Nama Varchar 30 -
Gender Varchar 11 -
Jurusan varchar 20 -

Isi tabel

NIM Nama Gender Jurusan


1607055001 Tony Laki – Laki Ilmu Komputer
1607055002 Jack Laki – Laki Ilmu Komputer
1607065003 Venny Perempuan Teknik Informatika

B. Class Koneksi
New -> Java Class -> masukkan nama kelas

Dibawah package
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

dibawah class
private static Connection mysqlconfig;
public static Connection configDB()throws SQLException{
try {
String url="jdbc:mysql://localhost:3306/jcrud";
String user="root";
String pass="";
DriverManager.registerDriver(new com.mysql.jdbc.Driver());
mysqlconfig=DriverManager.getConnection(url, user, pass);
} catch (Exception e) {
System.err.println("koneksi gagal "+e.getMessage());
}
return mysqlconfig;
}

C. Koding Form
Dibawah package
import java.awt.event.MouseEvent;
import java.sql.Connection;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

dibawah public (nama form)


initComponents();
load_table();
kosong();

method load_table()
DefaultTableModel model = new DefaultTableModel();
model.addColumn("NIM");
model.addColumn("Nama");
model.addColumn("Gender");
model.addColumn("Jurusan");

try {
int no = 1;
String sql = "select * from tcrud";
java.sql.Connection conn = (Connection) koneksi.configDB();
java.sql.Statement stm = conn.createStatement();
java.sql.ResultSet res = stm.executeQuery(sql);
while (res.next()) {
model.addRow(new Object[]{res.getString(1), res.getString(2),
res.getString(3), res.getString(4)});
}
jTable1.setModel(model);
} catch (Exception e) {

}
jTextField1.setEnabled(true);
jButton1.setEnabled(true);

method kosong()
jTextField1.setText("");
jTextField2.setText("");
buttonGroup1.clearSelection();
jComboBox1.setSelectedIndex(0);

tombol simpan
if(jComboBox1.getSelectedItem() == "--PILIH JURUSAN--" ||
"".equals(jTextField1.getText()) || "".equals(jTextField2.getText())){
JOptionPane.showMessageDialog(null, "Lengkapi Data Dulu
Mblo");}
else{
String jk = "";
if (jRadioButton1.isSelected()){
jk = jRadioButton1.getText();
}
else if (jRadioButton2.isSelected()){
jk = jRadioButton2.getText();
}
try {

String sql = "INSERT INTO tcrud VALUES


('"+jTextField1.getText()+"','"+jTextField2.getText()
+"','"+jk+"','"+jComboBox1.getSelectedItem()+"')";
java.sql.Connection conn = (Connection) koneksi.configDB();
java.sql.PreparedStatement pst = conn.prepareStatement(sql);
pst.execute();
JOptionPane.showMessageDialog(null, "Penyimpanan Data
"+jTextField2.getText()+" Berhasil");
} catch (Exception e) {
JOptionPane.showMessageDialog(this, "Penyimpanan Data Gagal");
load_table();
kosong();
}
jTextField1.setEnabled(true);
load_table();
kosong();
}

tombol edit
String jk = "";
if (jRadioButton1.isSelected()){
jk = jRadioButton1.getText();
}
else if (jRadioButton2.isSelected()){
jk = jRadioButton2.getText();
}
try{
String sql = "UPDATE tcrud SET NIM = '"+jTextField1.getText()+"',
Nama = '"+jTextField2.getText()+"', Gender = '"+jk+"', Jurusan =
'"+jComboBox1.getSelectedItem()+"' WHERE NIM =
'"+jTextField1.getText()+"'";
java.sql.Connection conn = (Connection)koneksi.configDB();
java.sql.PreparedStatement pst=conn.prepareStatement(sql);
pst.execute();
JOptionPane.showMessageDialog(null, "Data "+jTextField2.getText()
+" Berhasil Di Edit");
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "Data
"+jTextField2.getText()+" Gagal Di Edit");
}
load_table();
kosong();

tombol hapus
try{
String sql = "DELETE from tcrud where
NIM='"+jTextField1.getText()+"'";
java.sql.Connection conn = (Connection)koneksi.configDB();
java.sql.PreparedStatement pst=conn.prepareStatement(sql);
pst.execute();
JOptionPane.showMessageDialog(null, "Data
"+jTextField2.getText()+" Berhasil Di Hapus");
} catch (Exception e) {
JOptionPane.showMessageDialog(this, e.getMessage());
}
load_table();
kosong();

tombol reset
kosong();
load_table();

tabel (klik jTable di form -> Events -> Mouse ->mouseClicked)


int baris = jTable1.rowAtPoint(evt.getPoint());
String nim = jTable1.getValueAt(baris, 0).toString();
jTextField1.setText(nim);
String nama = jTable1.getValueAt(baris, 1).toString();
jTextField2.setText(nama);
if ("Laki - Laki".equals(jTable1.getValueAt(baris, 2).toString())) {
jRadioButton1.setSelected(true);
jRadioButton2.setSelected(false);
}
else if ("Perempuan".equals(jTable1.getValueAt(baris, 2).toString())){
jRadioButton2.setSelected(true);
jRadioButton1.setSelected(false);
}
String jurusan = jTable1.getValueAt(baris, 3).toString();
jComboBox1.setSelectedItem(jurusan);

jTextField1.setEnabled(false);
jButton1.setEnabled(false);

You might also like