Koneksi menggunakan netbeans

Click here to load reader

  • date post

    28-Mar-2015
  • Category

    Documents

  • view

    128
  • download

    4

Embed Size (px)

Transcript of Koneksi menggunakan netbeans

Koneksi menggunakan netbeans package quantum; /** * * @author Rachmad Hakim S. (Stifler) */ public class Koneksi { static String driver = "com.mysql.jdbc.Driver"; static String database = "jdbc:mysql://localhost:3306/sib"; static String user = "root"; static String pass = ""; //Connection konek; } Form barang /* * To change this template, choose Tools | Templates * and open the template in the editor. */ /* * Barang.java * * Created on 30 Nov 08, 20:12:04 */ package quantum; import java.sql.*; import java.util.logging.Level; import java.util.logging.Logger; import javax.swing.JOptionPane; /** * * @author Stifler */ public class Barang extends javax.swing.JDialog { /** Creates new form Barang */ public Barang(java.awt.Frame parent, boolean modal) { super(parent, modal); initComponents(); } //String itembarang[][]; int cr='o'; public void upData() throws ClassNotFoundException, SQLException{ int respons = JOptionPane.showConfirmDialog(this, "Apakah data akan dimasukkan?\n"+ "Kode: "+ txtKode.getText()+"\n" + "Nama : "+txtNama.getText()+"\n" + "Satuan: "+txtSatuan.getText()+"\n" + "Harga: "+txtHarga.getText()+"\n" + "Stok: "+txtStok.getText()+"\n", "Penambahan Data", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE); if (respons == 0){ try { Class.forName(Koneksi.driver); Connection konek1 = DriverManager.getConnection(Koneksi.database,Koneksi.user,Koneksi.pass);

Statement st1 = konek1.createStatement(); ResultSet rs1 = st1.executeQuery("SELECT STOK FROM BARANG WHERE KODEBARANG=\'"+ txtKode.getText()+"\';"); if(rs1.next()){ st1.executeUpdate("UPDATE barang SET NAMABARANG=\'"+txtNama.getText()+"\', SATUAN=\'"+txtSatuan.getText()+"\', " + "HARGA=\'"+txtHarga.getText()+"\', STOK=\'"+txtStok.getText()+"\' WHERE KODEBARANG=\'"+ txtKode.getText()+"\';"); } else{ st1.executeUpdate("INSERT INTO barang VALUES (\'"+ txtKode.getText()+"\', \'"+txtNama.getText() +"\', \'"+txtSatuan.getText()+"\', \'" +txtHarga.getText()+"\', \'"+txtStok.getText()+"\');"); } rs1.close(); //upTampilanBarang(); //JOptionPane.showMessageDialog(rootPane, "Data Barang telah ditambahkan\n" ); st1.close(); konek1.close(); }catch(Exception e){ JOptionPane.showMessageDialog(rootPane, "Koneksi gagal"); } //this.clear(); } } /*public void upTampilanBarang() throws SQLException, ClassNotFoundException{ Class.forName(Koneksi.driver); Connection konek1 = DriverManager.getConnection(Koneksi.database,Koneksi.user,Koneksi.pass); Statement st1 = konek1.createStatement(); //Update combo box cari namabarang ResultSet rs1 = st1.executeQuery("SELECT * FROM barang;"); rs1.last(); int i = 0, n = rs1.getRow(); rs1.beforeFirst(); st1.close(); konek1.close(); }*/ public void cari(String x){ try { Class.forName(Koneksi.driver); Connection konek1; konek1 = DriverManager.getConnection(Koneksi.database, Koneksi.user, Koneksi.pass); Statement st1 = konek1.createStatement(); ResultSet rs1; if (cr == 'n'){ rs1 = st1.executeQuery("SELECT * FROM barang WHERE namabarang LIKE \'%"+x+"%\';"); } else { rs1 = st1.executeQuery("SELECT * FROM barang WHERE kodebarang=\'"+x+"\';"); } rs1.next(); txtKode.setText(rs1.getString(1)); txtNama.setText(rs1.getString(2)); txtSatuan.setText(rs1.getString(3)); txtHarga.setText(rs1.getString(4)); txtStok.setText(rs1.getString(5)); st1.close(); konek1.close(); } catch (ClassNotFoundException ex) { JOptionPane.showMessageDialog(rootPane, "Class tidak ditemukan");

Logger.getLogger(Barang.class.getName()).log(Level.SEVERE, null, ex); } catch (SQLException ex) { JOptionPane.showMessageDialog(rootPane, "Data tidak ditemukan"); Logger.getLogger(Barang.class.getName()).log(Level.SEVERE, null, ex); } }

public void hapus() { int respons = JOptionPane.showConfirmDialog(this, "Apakah data akan dihapus?\n"+ "Kode: "+ txtKode.getText()+"\n" + "Nama : "+txtNama.getText()+"\n" + "Satuan: "+txtSatuan.getText()+"\n" + "Harga: "+txtHarga.getText()+"\n" + "Stok: "+txtStok.getText()+"\n", "Penghapusan Data", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE); if (respons == 0){ try { Class.forName(Koneksi.driver); Connection konek1; konek1 = DriverManager.getConnection(Koneksi.database, Koneksi.user, Koneksi.pass); Statement st1 = konek1.createStatement(); int ud1 = st1.executeUpdate("DELETE FROM barang WHERE kodebarang="+txtKode.getText()+";"); //upTampilanBarang(); JOptionPane.showMessageDialog(rootPane, "Data Barang telah dihapus\n" + "Kode: "+ txtKode.getText()+"\n" + "Nama :"+txtNama.getText()+"\n" + "Satuan: "+txtSatuan.getText()+"\n" + "Harga: "+txtHarga.getText()+"\n" + "Stok: "+txtStok.getText()+"\n"); this.clear(); } catch (ClassNotFoundException ex) { JOptionPane.showMessageDialog(rootPane, "Class tidak ditemukan"); Logger.getLogger(Barang.class.getName()).log(Level.SEVERE, null, ex); }catch (SQLException ex) { JOptionPane.showMessageDialog(rootPane, "Koneksi gagal"); Logger.getLogger(Barang.class.getName()).log(Level.SEVERE, null, ex); } } } public void clear(){ txtKode.setText(""); txtNama.setText(""); txtSatuan.setText(""); txtHarga.setText(""); txtStok.setText(""); } /** 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") // private void initComponents() { 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(); txtKode = new javax.swing.JTextField(); txtNama = new javax.swing.JTextField(); txtSatuan = new javax.swing.JTextField(); txtHarga = new javax.swing.JTextField(); txtStok = new javax.swing.JTextField(); jButton1 = new javax.swing.JButton(); jButton2 = new javax.swing.JButton(); jButton3 = new javax.swing.JButton(); jButton4 = new javax.swing.JButton(); jButton5 = new javax.swing.JButton(); jButton6 = new javax.swing.JButton(); setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE); jLabel1.setFont(new java.awt.Font("Tahoma", 1, 36)); jLabel1.setText("Master Barang"); jLabel2.setText("Kode barang"); jLabel3.setText("Nama barang"); jLabel4.setText("Satuan"); jLabel5.setText("Harga"); jLabel6.setText("Stok"); txtSatuan.setText("bungkus"); jButton1.setText("Cari"); jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton1ActionPerformed(evt); } }); jButton2.setText("Cari"); jButton2.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton2ActionPerformed(evt); } }); jButton3.setText("Tutup"); jButton3.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton3ActionPerformed(evt); } }); jButton4.setText("Hapus"); jButton4.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton4ActionPerformed(evt); } }); jButton5.setText("Simpan"); jButton5.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton5ActionPerformed(evt); } }); jButton6.setText("Clear"); jButton6.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) { jButton6ActionPerformed(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) .addComponent(jLabel3) .addComponent(jLabel4) .addComponent(jLabel5) .addComponent(jLabel6) .addGroup(layout.createSequentialGroup() .addComponent(jLabel2) .addGap(26, 26, 26) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel1) .addGroup(layout.createSequentialGroup() .addGap(10, 10, 10) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false) .addComponent(txtNama, javax.swing.GroupLayout.Alignment.LEADING) .addComponent(txtKode, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 139, Short.MAX_VALUE) .addComponent(txtSatuan, javax.swing.GroupLayout.Alignment.LEADING) .addComponent(txtHarga, javax.swing.GroupLayout.Alignment.LEADING) .addComponent(txtStok, javax.swing.GroupLayout.Alignment.LEADING)) .addGap(18, 18, 18) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addComponent(jButton6, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jButton2, 0, 0, Short.MAX_VALUE) .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))) .addGroup(layout.createSequentialGroup() .addComponent(jButton3) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jButton4) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jButton5))) .addContainerGap(117, Short.MAX_VALUE)) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGap(21, 21, 21) .addComponent(jLabel1) .addGap(18, 18, 18) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel2) .addComponent(txtKode, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jButton1)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel3) .addComponent(txtNama, javax.swing.GroupLayout.