1 JAVA non OOP/IMW/PAGI 1. Buat Database dengan Ms.Access dengan nama database “perpustakaan.mdb” 2. Buat tabel barang dengan struktur sbb: Field Kdbarang Nmbarang Satuan Harga Type Text Text Text Currency Size 4 30 6 Keterangan Primary Key 3. Buat data source name untuk koneksi dengan java netbeand caranya: Menu start Control Panel Administratife tools Data source(ODBC) Tampil seperti gambar: • Klik tombol Add, tampil seperti gambar: • • Pilih Microsoft Access driver(*.mdb) Klik tombol Finish, tampil seperti gambar: 2 JAVA non OOP/IMW/PAGI • • Isi Data Source Name seperti gambar diatas: Kemudian pilih database dengan meng klik tombol select, tampil seperti gambar: • • Pilih Drives dimana tempat database disimpan, pilih directories(folder tempat database disimpan), dan pilih Database name Klik Ok, klik Ok lagi. 4. 5. Buka Netbeans kemudian buat project baru dengan nama perpus Buat form baru dengan nama database.java, kemudian design seperti gambar: Name:tkode Name:tnama Name:tsatuan Name:tharga Name:badd Name:bupdat Name:bupdate Name:bupdate Tampilan setelah dijalankan Kemudian Keting listingnya seperti dibawah: 3 JAVA non OOP/IMW/PAGI package Perpus; import java.sql.*; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.sql.Statement; import java.util.logging.Level; import java.util.logging.Logger; import javax.swing.JOptionPane; public class database extends javax.swing.JFrame { /** * Creates new form database */ public database() { initComponents(); } @SuppressWarning(“Unchecked”) public Connection cn; static final String drivernya="sun.jdbc.odbc.JdbcOdbcDriver"; static final String alamatdsn="jdbc:odbc:koneksiDB"; //ini method void buka koneksi, merupakan listing membuka koneksi dengan database Access public void bukakoneksi() { try { Class.forName(database.drivernya); } catch (ClassNotFoundException ex) { Logger.getLogger(database.class.getName()).log(Level.SEVERE, null, ex); } try { cn=DriverManager.getConnection(database.alamatdsn, "", ""); } catch (SQLException ex) { Logger.getLogger(database.class.getName()).log(Level.SEVERE, null, ex); } System.out.println("Database Terkoneksi"); } //ini method void tutupkoneksi, merupakan listing penutup koneksi ke database Access public void tutupkoneksi() { try { if(!cn.isClosed()){ cn.close(); 4 JAVA non OOP/IMW/PAGI System.out.println("Database Terputus"); } } catch (SQLException ex) { Logger.getLogger(database.class.getName()).log(Level.SEVERE, null, ex); } } //listing Tombol Close, klik kanan pd tombol close event Action ActionPerformed private void BcloseActionPerformed(java.awt.event.ActionEvent evt) { System.exit(1); // TODO add your handling code here: } //listing form load, klik kanan di form event window windowOpened private void formWindowOpened(java.awt.event.WindowEvent evt) { bukakoneksi(); // TODO add your handling code here: } //listing tombol Add, klik kanan pd tombol Add event Action ActionPerformed private void baddActionPerformed(java.awt.event.ActionEvent evt) { boolean a=false; String sql; if(tkode.getText().trim().equals("") || tnama.getText().trim().equals("") ||tsatuan.getText().trim().equals("") || tharga.getText().trim().equals("")){ JOptionPane.showMessageDialog(this,"Data Harus diisi Lengkap"); }else{ sql="insert into barang values('"+tkode.getText()+"','"+tnama.getText()+"','"+tsatuan.getText()+"','"+Integer.valueOf(tharga.getText() )+"')"; // TODO add your handling code here: try { Statement st = cn.createStatement(); st.executeUpdate(sql); JOptionPane.showMessageDialog(rootPane,"Data Tersimpan"); tkode.setText(""); tnama.setText(""); tsatuan.setText(""); tharga.setText(""); a=true; } catch (SQLException ex) { Logger.getLogger(database.class.getName()).log(Level.SEVERE, null, ex); a=false; } } } 5 JAVA non OOP/IMW/PAGI //listing tkode keypress, klik kanan pd textbox kode barang event private void tkodeKeyPressed(java.awt.event.KeyEvent evt) { boolean x=false; String sql; key keyPressed //jika di tekan tombol enter, maka mengerjakan if(evt.getKeyCode() == evt.VK_ENTER){ sql="SELECT * from barang WHERE kdbarang='"+tkode.getText()+"'"; try { Statement st = cn.createStatement(); ResultSet rs=st.executeQuery(sql); if(rs.next()){ JOptionPane.showMessageDialog(rootPane,"Data Sudah Ada"); tkode.setText(rs.getString("kdbarang")); tnama.setText(rs.getString("nmbarang")); tsatuan.setText(rs.getString("satuan")); tharga.setText(String.valueOf(rs.getInt("harga"))); tkode.setEnabled(false); x=true; }else{ JOptionPane.showMessageDialog(rootPane,"Data Belum Terdaftar, Input Data Baru"); tnama.requestFocus(); } } catch (SQLException ex) { Logger.getLogger(database.class.getName()).log(Level.SEVERE, null, ex); x=false; } }// TODO add your handling code here: } //listing tombol update, klik kanan pd tombol Update event Action ActionPerformed private void bupdateActionPerformed(java.awt.event.ActionEvent evt) { boolean u=false; String sql; if(tkode.getText().trim().equals("") || tnama.getText().trim().equals("") || tsatuan.getText().trim().equals("") || tharga.getText().trim().equals("")){ JOptionPane.showMessageDialog(this,"Data Harus diisi Lengkap"); }else{ sql="UPDATE barang set nmbarang='"+tnama.getText()+"',satuan='"+tsatuan.getText()+"',harga='"+Integer.valueOf(tharga.getText())+ "' where kdbarang='"+tkode.getText()+"'" ;// TODO add your handling code here: try { Statement st = cn.createStatement(); st.executeUpdate(sql); JOptionPane.showMessageDialog(rootPane,"Data Sudah diubah"); tkode.setEnabled(true); tkode.setText(""); 6 JAVA non OOP/IMW/PAGI tnama.setText(""); tsatuan.setText(""); tharga.setText(""); u=true; } catch (SQLException ex) { Logger.getLogger(database.class.getName()).log(Level.SEVERE, null, ex); u=false; } } } //listing tombol delete, klik kanan pd tombol delete event Action ActionPerformed private void bdeleteActionPerformed(java.awt.event.ActionEvent evt) { boolean h=false; String sql; if(tkode.getText().trim().equals("") || tnama.getText().trim().equals("") ||tsatuan.getText().trim().equals("") || tharga.getText().trim().equals("")){ JOptionPane.showMessageDialog(this,"Data Harus diisi Lengkap"); }else{ sql="delete from barang where kdbarang='"+tkode.getText()+"'" ; try { Statement st = cn.createStatement(); st.executeUpdate(sql); JOptionPane.showMessageDialog(rootPane,"Data Sudah dihapus"); tkode.setText(""); tnama.setText(""); tsatuan.setText(""); tharga.setText(""); tkode.setEnabled(true); h=true; } catch (SQLException ex) { Logger.getLogger(database.class.getName()).log(Level.SEVERE, null, ex); h=false; } }// TODO add your handling code here: }