Senin, 12 November 2012

Program Simpan, Edit, Hapus dan Cari dengan Netbeans


Program Simpan, Edit, Hapus dan Cari dengan Netbeans
Pada database akademik di latihan sebelumnya, tambahkan tabel mahasiswa :

CREATE TABLE mahasiswa (
nobp CHAR(7) PRIMARY KEY,
nama VARCHAR(30),
jurusan VARCHAR(30));
 
tambahkan sebuat class dengan nama mahasiswa, disain form seperti tampilan berikut :





















kodingnya :

package prjakademik;
import java.sql.*;
import javax.swing.*;
import javax.swing.table.*;
/**
 *
 * @author Erin
 */
public class mahasiswa extends javax.swing.JFrame {
    public Connection conn;
    public Statement cn;


    /** Creates new form mahasiswa */
    public mahasiswa() {
        initComponents();
        bersih();
        tampildata();
        jComboBox1.addItem("Sistem Informasi");
        jComboBox1.addItem("Sistem Komputer");
        jComboBox1.addItem("Manajemen Informatika");
    }


tambahkan sebuah method pada class mahasiswa dengan nama koneksi, bisa juga di lihat pada lahitan sebelumnya..
public void koneksi(){
    try{
        Class.forName("com.mysql.jdbc.Driver");
        conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/akademik","root","");
        cn = conn.createStatement();
    }catch (Exception e){
        JOptionPane.showMessageDialog(null,"koneksi gagal..");
        System.out.println(e.getMessage());
        }
    }


//nethod bersih
public void bersih(){
jTextField1.setText("");
jTextField2.setText("");
jTextField3.setText("");
jComboBox1.setSelectedItem("-Pilih-");
}

//method tampil data
public void tampildata(){
DefaultTableModel tabelnyo = new DefaultTableModel();
tabelnyo.addColumn("NO BP");
tabelnyo.addColumn("NAMA ");
tabelnyo.addColumn("JURUSAN");

try{
koneksi();
String sql = "Select * from mahasiswa order by nobp asc";
ResultSet rs = cn.executeQuery(sql);
while (rs.next())
{
tabelnyo.addRow(new Object[]{
rs.getString(1),
rs.getString(2),
rs.getString(3),
});
}
jTable1.setModel(tabelnyo);

}catch (Exception e){
JOptionPane.showMessageDialog(null, "Ada Kesalahan");
}

}

method carimhs :
public void carimhs(){
try{

koneksi();
String sql = "Select * from mahasiswa where nobp='"+jTextField1.getText()+"'";
ResultSet rs = cn.executeQuery(sql);

if (rs.next())
{

jTextField1.setText(rs.getString(1));
jTextField2.setText(rs.getString(2));
jComboBox1.setSelectedItem(rs.getString(3));


JOptionPane.showMessageDialog(null,"Data mahasiswa : " + jTextField1.getText() + " sudah ada.!!!");
bersih();
}
} catch (Exception e){}
jTextField2.requestFocus();
}

program pada tombol simpan..
double klik tombol simpan atau klik kanan-events-actionperformed dan ketikan koding berikut :
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
try{

koneksi();
String sql = "insert into mahasiswa values ('"+ jTextField1.getText()+"', '" + jTextField2.getText() + "' , '" + jComboBox1.getSelectedItem() + "')";
cn.executeUpdate(sql);
conn.close();

tampildata();
bersih();
JOptionPane.showMessageDialog(null,"Data berhasil di simpan");

}catch (Exception e){
JOptionPane.showMessageDialog(null,"Proses penyimpanan gagal/koneksi gagal..");
System.out.println(e.getMessage());
}
}

program pada tombol edit..
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
try{

koneksi();
String sql = "update mahasiswa set nama='"+ jTextField2.getText() +"'," +
" jurusan='" + jComboBox1.getSelectedItem() + "'" +
"where nobp='" + jTextField1.getText() + "'";
cn.executeUpdate(sql);
conn.close();

tampildata();
bersih();
JOptionPane.showMessageDialog(null,"Data berhasil di rubah");

}catch (Exception e){
JOptionPane.showMessageDialog(null,"Proses update gagal/koneksi gagal..");
System.out.println(e.getMessage());
}
}

program pada tombol hapus :
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
try{

koneksi();
String sql = "delete from mahasiswa where nobp='"+ jTextField1.getText() +"'";
cn.executeUpdate(sql);
conn.close();

tampildata();
bersih();
JOptionPane.showMessageDialog(null,"Data berhasil di hapus");

}catch (Exception e){
JOptionPane.showMessageDialog(null,"Proses hapus gagal/koneksi gagal..");
System.out.println(e.getMessage());
}
}

klik kanan pada textfield nobp-events-key-keypresed :
if(evt.getKeyCode()==10){
carimhs();
}

klik kanan pada tabel-events-mouse-mouse clicked :
private void jTable1MouseClicked(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
int table = jTable1.getSelectedRow();
//mengambil data dari table
String a = jTable1.getValueAt(table, 0).toString();
String b = jTable1.getValueAt(table, 1).toString();
String c = jTable1.getValueAt(table, 2).toString();

jTextField1.setText(a);
jTextField2.setText(b);
jComboBox1.setSelectedItem(c);

jTextField1.setEnabled(false);
jTextField2.requestFocus();
jButton1.setEnabled(false);
}

Tidak ada komentar:

Posting Komentar