of 27/27
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" +

Koneksi menggunakan netbeans

  • View
    134

  • Download
    4

Embed Size (px)

Text 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.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jButton2)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel4) .addComponent(txtSatuan, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jButton6)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel5)

.addComponent(txtHarga, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel6) .addComponent(txtStok, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 38, Short.MAX_VALUE) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jButton3) .addComponent(jButton4) .addComponent(jButton5)) .addGap(40, 40, 40)) ); pack(); }// private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { cr = 'k'; cari(txtKode.getText()); // TODO add your handling code here: // TODO add your handling code here: } private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) { try { upData(); // TODO add your handling code here: } catch (ClassNotFoundException ex) { Logger.getLogger(Barang.class.getName()).log(Level.SEVERE, null, ex); } catch (SQLException ex) { Logger.getLogger(Barang.class.getName()).log(Level.SEVERE, null, ex); }// TODO add your handling code here: } private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) { cr = 'n'; cari(txtKode.getText()); // TODO add your handling code here: // TODO add your handling code here: } private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) { hapus();// TODO add your handling code here: } private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) { this.dispose();// TODO add your handling code here: } private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) { this.clear();// TODO add your handling code here: } /** * @param args the command line arguments */ public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { Barang dialog = new Barang(new javax.swing.JFrame(), true); dialog.addWindowListener(new java.awt.event.WindowAdapter() { public void windowClosing(java.awt.event.WindowEvent e) { System.exit(0); } });

dialog.setVisible(true); } }); } // Variables declaration - do not modify private javax.swing.JButton jButton1; private javax.swing.JButton jButton2; private javax.swing.JButton jButton3; private javax.swing.JButton jButton4; private javax.swing.JButton jButton5; private javax.swing.JButton jButton6; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel2; private javax.swing.JLabel jLabel3; private javax.swing.JLabel jLabel4; private javax.swing.JLabel jLabel5; private javax.swing.JLabel jLabel6; private javax.swing.JTextField txtHarga; private javax.swing.JTextField txtKode; private javax.swing.JTextField txtNama; private javax.swing.JTextField txtSatuan; private javax.swing.JTextField txtStok; // End of variables declaration } Main /* * To change this template, choose Tools | Templates * and open the template in the editor. */ package quantum; /** * * @author Stifler */ public class Main { /** * @param args the command line arguments */ public static void main(String[] args) { Sekilas s = new Sekilas(); s.setLocationRelativeTo(null); s.setVisible(true); try { Thread.sleep(1000); } catch (InterruptedException e) { } s.dispose(); Utama u = new Utama(); u.setLocationRelativeTo(null); u.setVisible(true); //Jalankan Penjualan di awal Penjualan p = new Penjualan(u,true); p.setLocationRelativeTo(u); p.setVisible(true); // TODO code application logic here } }

Form penjualan /* * To change this template, choose Tools | Templates * and open the template in the editor. */ /* * Penjualan.java * * Created on 29 Nov 08, 2:46:03 */ package quantum; import java.util.logging.Level; import javax.swing.JOptionPane; import javax.swing.event.*; import javax.swing.table.*; import java.sql.*; import java.text.SimpleDateFormat; import java.util.logging.Logger; import java.util.Date; import java.util.HashMap; import java.util.Vector; import net.sf.jasperreports.engine.*; import net.sf.jasperreports.engine.data.JRTableModelDataSource; /** * * @author Stifler */ public class Penjualan extends javax.swing.JDialog implements TableModelListener { /** Creates new form Penjualan */ public Penjualan(java.awt.Frame parent, boolean modal) { super(parent, modal); ////DataTabel = initComponents(); txtTgl.setText(tgl()); lihatMaxNota(); } SimpleDateFormat sdf; TableModel DataTabel; int ch = 0, h2 = 0, maxnota; //String harga = txtHarga.getText() //int h2, int maxnota, String tgl, String harga, String bayar, String kembalian, TableModel DataTabel public String tgl(){ sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss"); return sdf.format(new Date()); } public void hitung(){ String[] dts; dts = new String[10]; int[] h1 = new int[10]; int i; //JOptionPane.showMessageDialog(rootPane, DataTabel.getValueAt(0, 4)); for (i=0;i