Вы находитесь на странице: 1из 10

III. KODE PROGRAM ClsCD.

java
package rental_cd; import import import import java; java.sql.SQLEx.sql.Connection; java.sql.Statement; java.sql.ResultSet ception;

public class ClsCD { private String id_cd; private String nama_cd; private String jenis_cd; public ClsCD() { } public ClsCD(String idcd) { this.id_cd = idcd; } public ClsCD(String idcd, String nama_cd, String jenis_cd) { this.id_cd = idcd; this.nama_cd = nama_cd; this.jenis_cd = jenis_cd; } public String getId_cd() { return id_cd; } public void setId_cd(String id_cd) { this.id_cd = id_cd; } public String getJenis_cd() { return jenis_cd; } public void setJenis_cd(String jenis_cd) { this.jenis_cd = jenis_cd; } public String getNama_cd() { return nama_cd; } public void setNama_cd(String nama_cd) { this.nama_cd = nama_cd; }

1 2

public String[][] TampilCD(Connection mConn) { String Query; String QueryCount; int i, n; try { QueryCount = "SELECT COUNT(id_cd) AS Total FROM public.cd"; Statement mSt = mConn.createStatement(); ResultSet mRs = mSt.executeQuery(QueryCount); n = 0; while (mRs.next()) { n = Integer.parseInt(mRs.getString("Total")); } String[][] Tmp = new String[n][3]; Query = "SELECT * FROM public.cd ORDER BY id_cd ASC"; ResultSet mRsx = mSt.executeQuery(Query); i = 0; while (mRsx.next()) { Tmp[i][0] = mRsx.getString("id_cd"); Tmp[i][1] = mRsx.getString("nama_cd"); Tmp[i][2] = mRsx.getString("jenis_cd"); i++; } return Tmp; } catch (SQLException ex) { System.out.println("Error Tampil Data"); ex.printStackTrace(); return null; } } public boolean SimpanData(Connection mConn, String idcd) { String Query = ""; String Tmp; try { Query = "SELECT id_cd FROM public.cd WHERE id_cd = '" + idcd + "' LIMIT (1)"; Statement mSt = mConn.createStatement(); ResultSet mRs = mSt.executeQuery(Query); Tmp = ""; while (mRs.next()) { Tmp = mRs.getString("id_cd"); } Tmp = Tmp.trim(); if (Tmp.matches("")) { Query = "INSERT INTO public.cd VALUES " + "('" + id_cd + "','" + nama_cd + "','" + jenis_cd + "')"; } else { Query = "UPDATE public.cd SET " + "nama_cd = '" + nama_cd + "', " + "jenis_cd = '" + jenis_cd + "' " + "WHERE id_cd = '" + id_cd + "'"; }

mSt.executeUpdate(Query); return true; } catch(SQLException ex) { System.out.println("Error Simpan Data"); ex.printStackTrace(); return false; } } public boolean HapusData(Connection mConn) { String Query; try { Query = "DELETE FROM public.cd WHERE id_cd = '" + id_cd + "'"; Statement mSt = mConn.createStatement(); mSt.executeUpdate(Query); return true; } catch(SQLException ex) { System.out.println("Error Hapus Data"); ex.printStackTrace(); return false; } }

Main.java
package rental_cd; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; public class Main { private Connection mConn = null; private String Url = "jdbc:postgresql://localhost:5432/rental_cd"; public Connection GetConn() { return mConn; } public void SetConn(Connection Val) { this.mConn = Val; } public void Koneksi() {

System.out.println("Koneksi dengan Postgres SQL"); try { Class.forName("org.postgresql.Driver"); System.out.println("Driver Siap Dipakai"); } catch(ClassNotFoundException ex) { System.out.println("Driver Tidak Ditemukan"); System.out.println(ex.toString()); } System.out.println("Koneksi Dijalankan..."); try { mConn = DriverManager.getConnection(Url, "postgres", "admin"); System.out.println("Cek Url : " + Url); System.out.println("Cek Basis Data DbTransaksi..."); System.out.println("Koneksi Sukses Berjalan"); } catch(SQLException ex) { System.out.println("Koneksi Gagal Dilakukan"); System.out.println(ex.toString()); } }

public static void main(String[] args) { new Main().Koneksi(); System.out.println("Form Data Barang Ditampilkan..."); new FormCd().setVisible(true); } }

FormCD.java
package rental_cd; import java.sql.Connection; import javax.swing.table.DefaultTableModel; import javax.swing.JOptionPane; public class FormCd extends javax.swing.JFrame { public FormCd() { initComponents(); } Connection mConn = null; ClsCD ClCD = new ClsCD(); Main main = new Main(); private void TampilCD() { String[] TbHeader = {"ID CD","NAMA CD","JENIS CD"}; String[][] TbData = null;

main.Koneksi(); mConn = main.GetConn(); try { TbData = ClCD.TampilCD(mConn); System.out.println("Statement Berhasil Dibuat"); System.out.println("Akses query dijalankan..."); DefaultTableModel model = new DefaultTableModel(TbData, TbHeader); TblCd.setModel(model); System.out.println("Data Selesai Diextract"); } catch(Exception ex) { System.out.println("Statement Gagal Dibuat"); System.out.println("Data Gagal Diakses"); System.out.println(ex.toString()); } } private void ClearStat() { TxtID.setText(""); TxtNamaCd.setText(""); TxtJenisCd.setText(""); System.out.println("Form Disegarkan..."); } private boolean Validasi() { boolean val = false; System.out.println("Cek Validasi..."); if (TxtID.getText().trim().matches("")) { JOptionPane.showMessageDialog(null, "ID Belum Diisi!!!"); System.out.println("Akses Ditolak!!!"); val = false; } else if (TxtNamaCd.getText().trim().matches("")) { JOptionPane.showMessageDialog(null, "Nama Belum Diisi!!!"); System.out.println("Akses Ditolak!!!"); val = false; }else if (TxtJenisCd.getText().trim().matches("")) { JOptionPane.showMessageDialog(null, "Jenis Belum Diisi!!!"); System.out.println("Akses Ditolak!!!"); val = false; }else { System.out.println("Akses Diterima"); val = true; } return val; } private void BtnKoneksiMouseEntered(java.awt.event.MouseEvent evt) {

BtnKoneksi.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icon/koneksi on.png"))); } private void BtnKoneksiMouseExited(java.awt.event.MouseEvent evt) { BtnKoneksi.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icon/koneksi off.png"))); } private void BtnKoneksiMousePressed(java.awt.event.MouseEvent evt) { BtnKoneksi.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icon/koneksi click.png"))); } private void BtnKoneksiMouseReleased(java.awt.event.MouseEvent evt) { BtnKoneksi.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icon/koneksi on.png"))); } private void BtnSimpanMouseEntered(java.awt.event.MouseEvent evt) { BtnSimpan.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icon/simpan on.png"))); } private void BtnSimpanMouseReleased(java.awt.event.MouseEvent evt) { BtnSimpan.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icon/simpan on.png"))); } private void BtnSimpanMousePressed(java.awt.event.MouseEvent evt) { BtnSimpan.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icon/simpan click.png"))); } private void BtnSimpanMouseExited(java.awt.event.MouseEvent evt) { BtnSimpan.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icon/simpan off.png"))); } private void TxtIDActionPerformed(java.awt.event.ActionEvent evt) { }

private void BtnKeluarMouseEntered(java.awt.event.MouseEvent evt) { BtnKeluar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icon/keluar on.png"))); } private void BtnKeluarMouseExited(java.awt.event.MouseEvent evt) { BtnKeluar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icon/keluar off.png"))); } private void BtnKeluarMousePressed(java.awt.event.MouseEvent evt) { BtnKeluar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icon/keluar click.png"))); } private void BtnKeluarMouseReleased(java.awt.event.MouseEvent evt) { BtnKeluar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icon/keluar on.png"))); } private void BtnHapusMouseEntered(java.awt.event.MouseEvent evt) { BtnHapus.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icon/hapus on.png"))); } private void BtnHapusMouseExited(java.awt.event.MouseEvent evt) { BtnHapus.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icon/hapus off.png"))); } private void BtnHapusMousePressed(java.awt.event.MouseEvent evt) { BtnHapus.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icon/hapus click.png"))); } private void BtnHapusMouseReleased(java.awt.event.MouseEvent evt) { BtnHapus.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icon/hapus on.png"))); }

private void BtnKoneksiMouseClicked(java.awt.event.MouseEvent evt) { this.TampilCD(); this.ClearStat(); } private void BtnKeluarMouseClicked(java.awt.event.MouseEvent evt) { System.out.println("Sistem Ditutup"); System.exit(0); } private void BtnSimpanMouseClicked(java.awt.event.MouseEvent evt) { System.out.println("Simpan Data Diproses..."); if (Validasi()) { ClsCD CcCD = new ClsCD(TxtID.getText(), TxtNamaCd.getText(), TxtJenisCd.getText()); if (CcCD.SimpanData(mConn, TxtID.getText())) { System.out.println("Data Sudah Disimpan"); JOptionPane.showMessageDialog(null, "Data Sudah Disimpan"); } else { System.out.println("Data Gagal Disimpan"); JOptionPane.showMessageDialog(null, "Data Gagal Disimpan"); } this.TampilCD(); this.ClearStat(); } } private void BtnHapusMouseClicked(java.awt.event.MouseEvent evt) { System.out.println("Hapus Data Diproses..."); if (Validasi()) { ClsCD CcCD = new ClsCD(String.valueOf(TxtID.getText())); if (CcCD.HapusData(mConn)) { System.out.println("Data Sudah DiHapus"); JOptionPane.showMessageDialog(null, "Data Sudah DiHapus"); } else { System.out.println("Data Gagal DiHapus"); JOptionPane.showMessageDialog(null, "Data Gagal DiHapus"); } this.TampilCD(); this.ClearStat();}}

10

private void TblCdMouseClicked(java.awt.event.MouseEvent evt) { String Tmp; Tmp = TblCd.getModel().getValueAt(TblCd.getSelectedRow(), 0).toString(); TxtID.setText(Tmp); Tmp = TblCd.getModel().getValueAt(TblCd.getSelectedRow(), 1).toString(); TxtNamaCd.setText(Tmp); Tmp = TblCd.getModel().getValueAt(TblCd.getSelectedRow(), 2).toString(); TxtJenisCd.setText(Tmp); } public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new FormCd().setVisible(true); } }); } // Variables declaration - do not modify private javax.swing.JLabel BtnHapus; private javax.swing.JLabel BtnKeluar; private javax.swing.JLabel BtnKoneksi; private javax.swing.JLabel BtnSimpan; private javax.swing.JTable TblCd; private javax.swing.JTextField TxtID; private javax.swing.JTextField TxtJenisCd; private javax.swing.JTextField TxtNamaCd; private javax.swing.JLabel cdBlur; private javax.swing.JLabel icon; private javax.swing.JLabel id_cd; private javax.swing.JLabel jLabel4; private javax.swing.JScrollPane jScrollPane1; private javax.swing.JLabel jenis_cd; private javax.swing.JLabel nama_cd; // End of variables declaration }

11

Keterangan:
1. Konstruktor 2. Aksesor 3. Method TampilCD pada ClsCD 4. Method SimpanData pada ClsCD 5. Method HapusData pada ClsCD 6. Class Koneksi 7. Class Main 8. Method TampilCD pada FormCD 9. Method Validasi pada FormCD 10. Button Koneksi 11. Tabel Koneksi

IV.TAMPILAN PROGRAM