Dan berikut listingnya :
package newpackage;
import java.sql.*;
import javax.swing.*;
/**
*
* @author m4rry
*/
public class NewClass
{
void testNewClass()
{
try
{
String dbname="jdbc:odbc:menu"; //nama ODBC yang sudah kita buat di Access
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection(dbname,"admin","admin");
JOptionPane.showMessageDialog(null,"Koneksi Sukses","Insert Data",JOptionPane.INFORMATION_MESSAGE);
con.close();
}
catch(Exception e)
{
JOptionPane.showInternalMessageDialog(null, "error"+e,"Gagal",JOptionPane.WARNING_MESSAGE);
}
}
public static void main(String args[])
{
NewClass test=new NewClass();
test.testNewClass();
System.exit(0);
}
}
package newpackage;
import java.sql.Statement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import javax.swing.JOptionPane;
/**
*
* @author m4rry
*/
public class tabel extends javax.swing.JFrame {
/** Creates new form tabel */
public tabel() {
initComponents();
}
/** 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")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
kdbarang = new javax.swing.JTextField();
jLabel3 = new javax.swing.JLabel();
nmbarang = new javax.swing.JTextField();
jLabel4 = new javax.swing.JLabel();
hrgbarang = new javax.swing.JTextField();
jLabel5 = new javax.swing.JLabel();
stkbarang = new javax.swing.JTextField();
jLabel6 = new javax.swing.JLabel();
spbarang = new javax.swing.JTextField();
jLabel7 = new javax.swing.JLabel();
jmhbeli = new javax.swing.JTextField();
jLabel8 = new javax.swing.JLabel();
ttl = new javax.swing.JTextField();
bsimpan = new javax.swing.JButton();
btambah = new javax.swing.JButton();
bcari = new javax.swing.JButton();
bhapus = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jLabel1.setText("TABEL_BARANG");
jLabel2.setText("kode barang");
jLabel3.setText("Nama barang");
jLabel4.setText("Harga barang");
jLabel5.setText("Stok barang");
jLabel6.setText("Suplier barang");
jLabel7.setText("Jumlah beli");
jmh_beli.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jmh_beliActionPerformed(evt);
}
});
jLabel8.setText("Total");
bsimpan.setText("Simpan");
bsimpan.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
bsimpanActionPerformed(evt);
}
});
btambah.setText("tambah");
btambah.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btambahActionPerformed(evt);
}
});
bcari.setText("cari");
bcari.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
bcariActionPerformed(evt);
}
});
bhapus.setText("Delete");
bhapus.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
bhapusActionPerformed(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()
.addGap(144, 144, 144)
.addComponent(jLabel1)
.addContainerGap(182, Short.MAX_VALUE))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(63, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel2)
.addComponent(jLabel3)
.addComponent(jLabel4)
.addComponent(jLabel5)
.addComponent(jLabel6)
.addComponent(jLabel8)
.addComponent(jLabel7))
.addGap(33, 33, 33)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(ttl)
.addComponent(jmhbeli)
.addComponent(spbarang)
.addComponent(stkbarang)
.addComponent(hrgbarang)
.addComponent(nmbarang)
.addComponent(kdbarang, javax.swing.GroupLayout.DEFAULT_SIZE, 145, Short.MAX_VALUE)))
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(bcari, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(bsimpan, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGap(72, 72, 72)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(bhapus, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(btambah, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
.addGap(90, 90, 90))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(21, 21, 21)
.addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(kd_barang, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel2))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel3)
.addComponent(nmbarang, 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(jLabel4)
.addComponent(hrgbarang, 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(stkbarang, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel5))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(sp_barang, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel6))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jmh_beli, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel7))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(ttl, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel8))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(bsimpan)
.addComponent(btambah))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(bcari)
.addComponent(bhapus))
.addContainerGap(19, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void b_tambahActionPerformed(java.awt.event.ActionEvent evt) {
kdbarang.setText("");
nmbarang.setText("");
hrgbarang.setText("");
stkbarang.setText("");
spbarang.setText("");
jmhbeli.setText("");
ttl.setText("");
kdbarang.requestFocus();
nmbarang.setEditable(true);
hrgbarang.setEditable(true);
stkbarang.setEditable(true);
spbarang.setEditable(true);
jmhbeli.setEditable(true);
ttl.setEditable(true);
}
private void bsimpanActionPerformed(java.awt.event.ActionEvent evt) {
String kodebarang =kdbarang.getText();
String namabarang =nmbarang.getText();
String hargabarang =hrgbarang.getText();
String stokbarang =stkbarang.getText();
String suplierbarang =spbarang.getText();
String jumlahbeli =jmhbeli.getText();
String total=ttl.getText();
try
{
String dbname="jdbc:odbc:menu";
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection(dbname,"admin","admin");
java.sql.Statement statement = con.createStatement();
String sql = "insert into tabelbarang values('"+kode_barang+"','"+namabarang+"',"+hargabarang+","+stokbarang+",'"+suplierbarang+"',"+jumlahbeli+","+total+");";
statement.executeUpdate(sql);
statement.close();
JOptionPane.showMessageDialog(null,"Data Berhasil Dimasukan..","Insert Data", JOptionPane.INFORMATION_MESSAGE);
con.close();
}
catch(Exception e)
{
JOptionPane.showMessageDialog(null,"Error"+e,"Gagal",JOptionPane.WARNING_MESSAGE);
}
}
private void bcariActionPerformed(java.awt.event.ActionEvent evt) {
String kode =kdbarang.getText();
try
{
String dbname="jdbc:odbc:menu";
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection(dbname,"admin","admin");
Statement statement = con.createStatement();
String sql = "SELECT * FROM tabel_barang WHERE kd_barang like '"+kode+"'";
ResultSet rs = statement.executeQuery(sql);
if(rs.next())
{
kdbarang.setText(rs.getString(1));
nmbarang.setText(rs.getString(2));
hrgbarang.setText(rs.getString(3));
stkbarang.setText(rs.getString(4));
spbarang.setText(rs.getString(5));
jmhbeli.setText(rs.getString(6));
ttl.setText(rs.getString(7));
JOptionPane.showMessageDialog(null,"Data ditemukan","Insert Data",JOptionPane.INFORMATION_MESSAGE);
}
else
{
JOptionPane.showMessageDialog(null,"Data tidak ditemukan","Insert Data",JOptionPane.INFORMATION_MESSAGE);
}
statement.close();
con.close();
}
catch (Exception e)
{
JOptionPane.showMessageDialog(null,"Error:"+e,"Gagal",JOptionPane.WARNING_MESSAGE);
}
}
private void b_hapusActionPerformed(java.awt.event.ActionEvent evt) {
String kode2 =kdbarang.getText();
try
{
String dbname="jdbc:odbc:menu";
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection(dbname,"admin","admin");
Statement statement = con.createStatement();
String sql = "DELETE from tabelbarang Where kdbarang like '"+kode2+"'";
statement.executeUpdate(sql);
b_tambah.doClick();
JOptionPane.showMessageDialog(null,"Data ditemukan","hapus data",JOptionPane.INFORMATION_MESSAGE);
statement.close();
con.close();
}
catch (Exception e)
{
JOptionPane.showMessageDialog(null,"kata m4rry : "+e,"Gagal",JOptionPane.WARNING_MESSAGE);
}
}
private void jmhbeliActionPerformed(java.awt.event.ActionEvent evt) {
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(tabel.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(tabel.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(tabel.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(tabel.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new tabel().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton bcari;
private javax.swing.JButton bhapus;
private javax.swing.JButton bsimpan;
private javax.swing.JButton btambah;
private javax.swing.JTextField hrgbarang;
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.JLabel jLabel7;
private javax.swing.JLabel jLabel8;
private javax.swing.JTextField jmhbeli;
private javax.swing.JTextField kdbarang;
private javax.swing.JTextField nmbarang;
private javax.swing.JTextField spbarang;
private javax.swing.JTextField stkbarang;
private javax.swing.JTextField ttl;
}
Selamat Mencoba ye,,,,,,,,,,,,,,,,,,,,,,,,,,,,
maaf kk.. punya fb ga?kebetulan saya baru belajar java..jadi pengen tanya2 lebih banyak sama kk
BalasHapus