Hallo teman-teman kali ini kita akan membuat aplikasi sewa lapangan futsal, anggaplah disini kita mempunyai 3 Lapangan dan Maks Durasi bermain 5 Jam dengan harga per jam Rp. 200.000.
Langkah pertama silahkan teman-teman aktifkan mysql dan apache nya terlebih dahulu dan membuat databasenya, oh iya teman-teman bisa mendownload databasenya disini: database_futsal.sql, setelah itu teman-teman tinggal import database tersebut.
Kemudian teman-teman buka netbeans dan create project sesuai keinginan, karena ini adalah perintah dari pak Dosen maka saya memberi nama project tersebut “UTS_M. CHANDRA NUGRAHA_181011401123”, jangan lupa juga buat new JFrame Form baru dengan nama terserah kalian
Buat design seperti dibawah ini:
Change Variable Nama seperti dibawah ini:
Komponen | Nilai |
jTextField1 | txt_idsewa_mchandranugraha |
jTextField2 | txt_nama_mchandranugraha |
jTextField3 | txt_telepon_mchandranugraha |
jTextField4 | txt_uangmuka_mchandranugraha |
jTextField5 | txt_sisabayar_mchandranugraha |
jTextField6 | txtud_idsewa_mchandranugraha |
jTextField7 | txtud_tanggalsewa_mchandranugraha |
jTextField8 | txtud_jamsewa_mchandranugraha |
jTextField9 | txtud_durasisewa_mchandranugraha |
jTextField10 | txtud_nama_mchandranugraha |
jTextField11 | txtud_telepon_mchandranugraha |
jTextField12 | txtud_uangmuka_mchandranugraha |
jTextField13 | txtud_sisabayar_mchandranugraha |
jComboBox1 | cb_kodelapangan_mchandranugraha |
jComboBox2 | cb_tanggal_mchandranugraha |
jComboBox3 | cb_bulan_mchandranugraha |
jComboBox4 | cb_tahun_mchandranugraha |
jComboBox5 | cb_jamawal_mchandranugraha |
jComboBox6 | cb_jamakhir_mchandranugraha |
jComboBox7 | cb_durasi_mchandranugraha |
jComboBox8 | cb_status_mchandranugraha |
jComboBox9 | cbud_kodelapangan_mchandranugraha |
jComboBox10 | cbud_status_mchandranugraha |
jButton1 | btn_proses_mchandranugraha |
jButton2 | btn_simpan_mchandranugraha |
jButton3 | btn_ubah_mchandranugraha |
jButton4 | btn_hapus_mchandranugraha |
jButton5 | btn_cari_mchandranugraha |
jButton6 | btn_baru_mchandranugraha |
jButton7 | btn_refresh_mchandranugraha |
jButton8 | btn_keluar_mchandranugraha |
jButton9 | btnud_proses_mchandranugraha |
jTable1 | tbl_tampil_mchandranugraha |
Klik kanan pada masing-masing combo box -> Properties -> ganti nilai Model dan selectedIndex seperti dibawah ini:
Komponen | Nilai Pada Model | Nilai Pada selectedIndex |
cb_kodelapangan_mchandranugraha | LAP001 LAP002 LAP003 |
-1 |
cb_tanggal_mchandranugraha | 01 02 …. 31 |
-1 |
cb_bulan_mchandranugraha | JANUARI FEBRUARI …. DESEMBER |
-1 |
cb_tahun_mchandranugraha | 2020 2021 2022 |
-1 |
cb_jamawal_mchandranugraha | 00:00 01:00 …. 23:00 |
-1 |
cb_jamakhir_mchandranugraha | 00:00 01:00 …. 23:00 |
-1 |
cb_durasi_mchandranugraha | 1 JAM 2 JAM 3 JAM 4 JAM 5 JAM |
-1 |
cb_status_mchandranugraha | BELUM LUNAS SUDAH LUNAS |
-1 |
cbud_kodelapangan_mchandranugraha | LAP001 LAP002 LAP003 |
-1 |
cbud_status_mchandranugraha | BELUM LUNAS SUDAH LUNAS |
-1 |
Kemudian import library MySQL JDBC Driver
Masukkan source code berikut dibawah package uts_m.chandra.nugraha_181011401123;
import java.sql.*; import java.awt.*; import javax.swing.JOptionPane; import javax.swing.table.*;
Masukkan source code berikut dibawah public class aplikasi_sewa_lapangan_futsal_mchandranugraha extends javax.swing.JFrame {
private Connection con; private Statement stat; private ResultSet res; private String t; private DefaultTableModel dtm;
Masukkan source code berikut dibawah initComponents();
koneksi(); kosongkan(); kosongkanub(); tampil(); //rata kiri dan kanan Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); Dimension frameSize = getSize(); setLocation( (screenSize.width - frameSize.width) / 3, (screenSize.height - frameSize.height) / 4);
Masukkan source code berikut dibawah (screenSize.height – frameSize.height) / 4); }
private void koneksi(){ try { Class.forName("com.mysql.jdbc.Driver"); con=DriverManager.getConnection("jdbc:mysql://localhost:3306/database_futsal?user=root&password="); stat=con.createStatement(); } catch(SQLException ex){ System.err.print(ex); } catch(ClassNotFoundException ex){ System.err.print(ex); } } private void kosongkan(){ txt_idsewa_mchandranugraha.setText(""); cb_kodelapangan_mchandranugraha.setSelectedIndex(-1); cb_tanggal_mchandranugraha.setSelectedIndex(-1); cb_bulan_mchandranugraha.setSelectedIndex(-1); cb_tahun_mchandranugraha.setSelectedIndex(-1); cb_jamawal_mchandranugraha.setSelectedIndex(-1); cb_jamakhir_mchandranugraha.setSelectedIndex(-1); cb_durasi_mchandranugraha.setSelectedIndex(-1); txt_nama_mchandranugraha.setText(""); txt_telepon_mchandranugraha.setText(""); txt_uangmuka_mchandranugraha.setText(""); txt_sisabayar_mchandranugraha.setText(""); cb_status_mchandranugraha.setSelectedIndex(-1); } private void kosongkanub(){ txtud_idsewa_mchandranugraha.setText(""); cbud_kodelapangan_mchandranugraha.setSelectedIndex(-1); txtud_tanggalsewa_mchandranugraha.setText(""); txtud_jamsewa_mchandranugraha.setText(""); txtud_durasisewa_mchandranugraha.setText(""); txtud_nama_mchandranugraha.setText(""); txtud_telepon_mchandranugraha.setText(""); txtud_uangmuka_mchandranugraha.setText(""); txtud_sisabayar_mchandranugraha.setText(""); cbud_status_mchandranugraha.setSelectedIndex(-1); } private void tampil(){ try{ Object [] rows={"ID","Kode Lapangan","Tanggal","Jam","Durasi","Nama","No. HP","Uang Muka","Sisa Bayar","Status"}; dtm=new DefaultTableModel(null,rows); tbl_tampil_mchandranugraha.setModel(dtm); tbl_tampil_mchandranugraha.setBorder(null); jScrollPane1.setVisible(true); jScrollPane1.setViewportView(tbl_tampil_mchandranugraha); int no = 1; String tbl_id="",tbl_kode_lapangan="",tbl_tanggal="",tbl_jam="",tbl_durasi="",tbl_nama="" + "",tbl_telepon="",tbl_uang_muka="",tbl_sisa_pembayaran="",tbl_status_pembayaran=""; try{ String sql="select * from data_sewa"; Statement st=con.createStatement(); ResultSet rs=st.executeQuery(sql); while(rs.next()){ tbl_id=rs.getString("tbl_id"); tbl_kode_lapangan=rs.getString("tbl_kode_lapangan"); tbl_tanggal=rs.getString("tbl_tanggal"); tbl_jam=rs.getString("tbl_jam"); tbl_durasi=rs.getString("tbl_durasi"); tbl_nama=rs.getString("tbl_nama"); tbl_telepon=rs.getString("tbl_telepon"); tbl_uang_muka=rs.getString("tbl_uang_muka"); tbl_sisa_pembayaran=rs.getString("tbl_sisa_pembayaran"); tbl_status_pembayaran=rs.getString("tbl_status_pembayaran"); String [] tampil={""+tbl_id,tbl_kode_lapangan,tbl_tanggal,tbl_jam,tbl_durasi,tbl_nama ,tbl_telepon,tbl_uang_muka,tbl_sisa_pembayaran,tbl_status_pembayaran}; dtm.addRow(tampil); } } catch(SQLException e){ e.printStackTrace(); JOptionPane.showMessageDialog(null,"Query Salah "+e); } } catch(Exception e){ e.printStackTrace(); } }
Klik kanan pada btn_proses_mchandranugraha -> Events -> Action -> actionPerformed, kemudian masukkan source code berikut:
int a= Integer.parseInt((String)cb_durasi_mchandranugraha.getSelectedItem()); int b= Integer.parseInt(txt_uangmuka_mchandranugraha.getText()); int c= 200000; int d= a*c; int Hasil= d-b; txt_sisabayar_mchandranugraha.setText(""+Hasil);
Klik kanan pada btn_simpan_mchandranugraha -> Events -> Action -> actionPerformed, kemudian masukkan source code berikut:
try { stat.executeUpdate("insert into data_sewa values (" + "'" + txt_idsewa_mchandranugraha.getText()+"'," +"'"+ cb_kodelapangan_mchandranugraha.getSelectedItem()+ "'," +"'"+ cb_tanggal_mchandranugraha.getSelectedItem()+"-"+cb_bulan_mchandranugraha.getSelectedItem()+"-"+cb_tahun_mchandranugraha.getSelectedItem()+ "'," +"'"+ cb_jamawal_mchandranugraha.getSelectedItem()+"-"+cb_jamakhir_mchandranugraha.getSelectedItem()+ "'," +"'"+ cb_durasi_mchandranugraha.getSelectedItem()+ "'," + "'" + txt_nama_mchandranugraha.getText()+"'," + "'" + txt_telepon_mchandranugraha.getText()+"'," + "'" + txt_uangmuka_mchandranugraha.getText()+"'," + "'" + txt_sisabayar_mchandranugraha.getText()+"'," +"'"+ cb_status_mchandranugraha.getSelectedItem()+ "')"); kosongkan(); tampil(); JOptionPane.showMessageDialog(null, "Berhasil Menyimpan Data"); } catch (HeadlessException | SQLException e) { JOptionPane.showMessageDialog(null, "Perintah Salah : "+e); }
Klik kanan pada btn_baru_mchandranugraha -> Events -> Action -> actionPerformed, kemudian masukkan source code berikut:
new aplikasi_sewa_lapangan_futsal_mchandranugraha().setVisible(true); dispose();
Klik kanan pada btn_hapus_mchandranugraha -> Events -> Action -> actionPerformed, kemudian masukkan source code berikut:
int ok=JOptionPane.showConfirmDialog(null,"Apakah Yakin Mendelete Record Ini???", "Confirmation",JOptionPane.YES_NO_CANCEL_OPTION); if (ok==0){ try{ String sql="delete from data_sewa where tbl_id='"+txtud_idsewa_mchandranugraha.getText()+"'"; PreparedStatement st=con.prepareStatement(sql); st.executeUpdate(); kosongkanub(); tampil(); JOptionPane.showMessageDialog(null, "Delete Data Sukses"); } catch (Exception e){ JOptionPane.showMessageDialog(null, "Delete Data Gagal"); } }
Klik kanan pada btn_refresh_mchandranugraha -> Events -> Action -> actionPerformed, kemudian masukkan source code berikut:
tampil();
Klik kanan pada btn_keluar_mchandranugraha -> Events -> Action -> actionPerformed, kemudian masukkan source code berikut:
JOptionPane.showMessageDialog(null, "Anda Akan Keluar Dari Program?"); System.exit(0);
Klik kanan pada btn_cari_mchandranugraha -> Events -> Action -> actionPerformed, kemudian masukkan source code berikut:
try { res=stat.executeQuery("select * from data_sewa where "+ "tbl_id='" +txtud_idsewa_mchandranugraha.getText()+"'" ); while (res.next()){ cbud_kodelapangan_mchandranugraha.setSelectedItem(res.getString("tbl_kode_lapangan")); txtud_tanggalsewa_mchandranugraha.setText(res.getString("tbl_tanggal")); txtud_jamsewa_mchandranugraha.setText(res.getString("tbl_jam")); txtud_durasisewa_mchandranugraha.setText(res.getString("tbl_durasi")); txtud_nama_mchandranugraha.setText(res.getString("tbl_nama")); txtud_telepon_mchandranugraha.setText(res.getString("tbl_telepon")); txtud_uangmuka_mchandranugraha.setText(res.getString("tbl_uang_muka")); txtud_sisabayar_mchandranugraha.setText(res.getString("tbl_sisa_pembayaran")); cbud_status_mchandranugraha.setSelectedItem(res.getString("tbl_status_pembayaran")); } } catch (Exception e) { JOptionPane.showMessageDialog(rootPane, e); }
Klik kanan pada btn_ubah_mchandranugraha -> Events -> Action -> actionPerformed, kemudian masukkan source code berikut:
int ok=JOptionPane.showConfirmDialog(null,"Apakah Yakin Untuk Update Record ini???","Confirmation",JOptionPane.YES_NO_OPTION); try { String sql="update data_sewa set tbl_id=?,tbl_kode_lapangan=?,tbl_tanggal=?,tbl_jam=?,tbl_durasi=?,tbl_nama=?" + ",tbl_telepon=?,tbl_uang_muka=?,tbl_sisa_pembayaran=?,tbl_status_pembayaran=? " + "where tbl_id='"+txtud_idsewa_mchandranugraha.getText()+"'"; PreparedStatement st=con.prepareStatement(sql); if(ok==0) { try { st.setString(1,txtud_idsewa_mchandranugraha.getText()); st.setString(2, (String) cbud_kodelapangan_mchandranugraha.getSelectedItem()); st.setString(3,txtud_tanggalsewa_mchandranugraha.getText()); st.setString(4,txtud_jamsewa_mchandranugraha.getText()); st.setString(5,txtud_durasisewa_mchandranugraha.getText()); st.setString(6,txtud_nama_mchandranugraha.getText()); st.setString(7,txtud_telepon_mchandranugraha.getText()); st.setString(8,txtud_uangmuka_mchandranugraha.getText()); st.setString(9,txtud_sisabayar_mchandranugraha.getText()); st.setString(10, (String) cbud_status_mchandranugraha.getSelectedItem()); st.executeUpdate(); kosongkanub(); tampil(); JOptionPane.showMessageDialog(null,"Update Data Sukses"); } catch (Exception e) { JOptionPane.showMessageDialog(null, "Update Data Gagal"); } } } catch (Exception e){}
Klik kanan pada btnud_proses_mchandranugraha -> Events -> Action -> actionPerformed, kemudian masukkan source code berikut:
int a= Integer.parseInt(txtud_durasisewa_mchandranugraha.getText()); int b= Integer.parseInt(txtud_uangmuka_mchandranugraha.getText()); int c= 200000; int d= a*c; int Hasil= d-b; txtud_sisabayar_mchandranugraha.setText(""+Hasil);
Klik kanan pada tbl_tampil_mchandranugraha -> Events -> Mouse -> mouseClicked, kemudian masukkan source code berikut:
int i = tbl_tampil_mchandranugraha.getSelectedRow(); if(i==-1){ return; } String code = (String)tbl_tampil_mchandranugraha.getValueAt(i,0); String code1 = (String)tbl_tampil_mchandranugraha.getValueAt(i,1); String code2 = (String)tbl_tampil_mchandranugraha.getValueAt(i,2); String code3 = (String)tbl_tampil_mchandranugraha.getValueAt(i,3); String code4 = (String)tbl_tampil_mchandranugraha.getValueAt(i,4); String code5 = (String)tbl_tampil_mchandranugraha.getValueAt(i,5); String code6 = (String)tbl_tampil_mchandranugraha.getValueAt(i,6); String code7 = (String)tbl_tampil_mchandranugraha.getValueAt(i,7); String code8 = (String)tbl_tampil_mchandranugraha.getValueAt(i,8); String code9 = (String)tbl_tampil_mchandranugraha.getValueAt(i,9); txtud_idsewa_mchandranugraha.setText(code); cbud_kodelapangan_mchandranugraha.setSelectedItem(code1); txtud_tanggalsewa_mchandranugraha.setText(code2); txtud_jamsewa_mchandranugraha.setText(code3); txtud_durasisewa_mchandranugraha.setText(code4); txtud_nama_mchandranugraha.setText(code5); txtud_telepon_mchandranugraha.setText(code6); txtud_uangmuka_mchandranugraha.setText(code7); txtud_sisabayar_mchandranugraha.setText(code8); cbud_status_mchandranugraha.setSelectedItem(code9);
Selesai.
Untuk demonya teman-teman bisa lihat video dibawah ini