Jumat, 02 Juni 2017

import javax

import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class FormMenu_201453025 extends javax.swing.JFrame {

   

    private void mnuDataBukuActionPerformed(java.awt.event.ActionEvent evt) {                                           
        // TODO add your handling code here:
        IfrmBuku_201453025 buka = new IfrmBuku_201453025();
        jDesktopPane1.add(buka);
        buka.setVisible(true);
    }                                          
   
    private void mnuExitActionPerformed(java.awt.event.ActionEvent evt) {                                       
        // TODO add your handling code here:
        int tutup = JOptionPane.showConfirmDialog(rootPane, "Apakah ingin keluar ?","Tanya",JOptionPane.YES_NO_OPTION);
        if (tutup == 0) {
          System.exit(0); 
        }
       
    }                                      

    private void mnuaaaaActionPerformed(java.awt.event.ActionEvent evt) {                                       
        // TODO add your handling code here:
         IfrmBuku_201453025 buka = new IfrmBuku_201453025();
        jDesktopPane1.add(buka);
        buka.setVisible(true);
    }                                      

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        //</editor-fold>
        koneksiDB konek = new koneksiDB();
        konek.setKoneksi();
        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
               
               
                FormMenu_201453025 mn = new FormMenu_201453025();
                mn.setExtendedState(JFrame.MAXIMIZED_BOTH);
                mn.setVisible(true);
            }
        });
    }
}


import java.awt.Dimension;
import java.awt.Toolkit;
import java.sql.ResultSet;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

public class IfrmBuku_201453025 extends javax.swing.JInternalFrame {

    /**
     * Creates new form IfrmBuku_201453025
     */
    public IfrmBuku_201453025() {
        initComponents();
        layarpenuh();
        tampilData("");
        nonaktif();
        bersih();
        isicombo();
    }
void isicombo(){
    cbokategori.removeAllItems();
    cbokategori.addItem("Kode Buku");
    cbokategori.addItem("Judul Buku");
    cbokategori.addItem("Penulis Buku");
    cbokategori.addItem("Penerbit Buku");
}
void layarpenuh(){
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    Dimension frameSize = getSize();
    setLocation((screenSize.width - frameSize.width)/2, (screenSize.height - frameSize.height)/2);
}
String sql;
void tampilData(String aksi){
    final String[] headers = {"KODE BUKU","JUDUL BUKU","PENULIS BUKU","PENERBIT BUKU"};
    DefaultTableModel modelTabel = new DefaultTableModel(null, headers);
   
    String kataKunci = txtkatakunci.getText();
    String kategoriCari = "";
   
    if (cbokategori.getSelectedItem()== "Kode Buku") {
        kategoriCari = "kode_buku";
    }else if (cbokategori.getSelectedItem()== "Judul Buku") {
        kategoriCari = "judul_buku";
    }else if (cbokategori.getSelectedItem()== "Penulis Buku") {
        kategoriCari = "penulis_buku";
    }else if (cbokategori.getSelectedItem()== "Penerbit Buku") {
        kategoriCari = "penerbit_buku";
    }
   
    try {
        if (aksi=="cari") {
            sql = "SELECT * FROM tb_buku WHERE " + kategoriCari + " LIKE '%"+kataKunci+"%'";
        }else{
            sql = "SELECT * FROM tb_buku";
        }
        ResultSet rs = koneksiDB.executeQuery(sql);
       
        while (rs.next()) {
            String[] data = {
                rs.getString("kode_buku"),
                rs.getString("judul_buku"),
                rs.getString("penulis_buku"),
                rs.getString("penerbit_buku")
            };
            modelTabel.addRow(data);
            txtkodebuku.setText(rs.getString("kode_buku"));
            txtjudulbuku.setText(rs.getString("judul_buku"));
            txtpenulisbuku.setText(rs.getString("penulis_buku"));
            txtpenerbitbuku.setText(rs.getString("penerbit_buku"));
        }
        rs.close();
    } catch (Exception ex) {
        JOptionPane.showMessageDialog(this, "ERROR: \n" + ex.toString(),"Kesalahan", JOptionPane.ERROR_MESSAGE);
    }
    jtblbuku.setModel(modelTabel);
}
    private void jtblbukuMouseClicked(java.awt.event.MouseEvent evt) {                                      
        // TODO add your handling code here:
        try {
            txtkodebuku.setText(jtblbuku.getValueAt(jtblbuku.getSelectedRow(), 0).toString());
            txtjudulbuku.setText(jtblbuku.getValueAt(jtblbuku.getSelectedRow(), 1).toString());
            txtpenulisbuku.setText(jtblbuku.getValueAt(jtblbuku.getSelectedRow(), 2).toString());
            txtpenerbitbuku.setText(jtblbuku.getValueAt(jtblbuku.getSelectedRow(), 3).toString());
            aktif();
            txtkodebuku.setEnabled(false);
        } catch (Exception e) {
            JOptionPane.showMessageDialog(this, "ERROR: \n" + e.toString(),"Kesalahan", JOptionPane.WARNING_MESSAGE);
        }
    }                                     

    private void btnsimpanActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // TODO add your handling code here:
        if (txtkodebuku.getText().equals("")) {
            JOptionPane.showMessageDialog(this, "Kode Buku belum di isi","Informasi", JOptionPane.INFORMATION_MESSAGE);
            txtkodebuku.requestFocus();
        }else if (txtjudulbuku.getText().equals("")) {
            JOptionPane.showMessageDialog(this, "Judul Buku belum di isi","Informasi", JOptionPane.INFORMATION_MESSAGE);
            txtjudulbuku.requestFocus();
        }else if (txtpenulisbuku.getText().equals("")) {
            JOptionPane.showMessageDialog(this, "Penulis Buku belum di isi","Informasi", JOptionPane.INFORMATION_MESSAGE);
            txtpenulisbuku.requestFocus();
        }else if (txtpenerbitbuku.getText().equals("")) {
            JOptionPane.showMessageDialog(this, "Penerbit Buku belum di isi","Informasi", JOptionPane.INFORMATION_MESSAGE);
            txtpenerbitbuku.requestFocus();
        }else{
        simpanData();
        tampilData("");
        nonaktif();
        bersih();
        }
    }                                        

    private void btnubahActionPerformed(java.awt.event.ActionEvent evt) {                                       
        // TODO add your handling code here:
        ubahData();
        tampilData("");
        bersih();
    }                                       

    private void btnhapusActionPerformed(java.awt.event.ActionEvent evt) {                                        
        // TODO add your handling code here:
        hapusData();
        tampilData("");
    }                                       

    private void btnrefreshActionPerformed(java.awt.event.ActionEvent evt) {                                          
        // TODO add your handling code here:
        tampilData("");
        bersih();
        nonaktif();
    }                                         

    private void btncariActionPerformed(java.awt.event.ActionEvent evt) {                                       
        // TODO add your handling code here:
        if (btncari.getText()=="PENCARIAN") {
            btncari.setText("CARI");
            txtkatakunci.setEnabled(true);
            txtkatakunci.requestFocus();
        }else if (btncari.getText()=="CARI") {
            btncari.setText("PENCARIAN");
            tampilData("cari");
            aktif();
            txtkodebuku.setEnabled(false);
            txtkatakunci.setEnabled(false);
            txtkatakunci.setText("");
        }
       
    }                                      

    private void btntambahActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // TODO add your handling code here:
       
        if (btntambah.getText()=="TAMBAH") {
            btntambah.setText("BATAL");
            aktif();
            bersih();
        }else if (btntambah.getText()=="BATAL") {
            btntambah.setText("TAMBAH");
            nonaktif();
            bersih();
        }
    }                                        
    void bersih(){
        txtkodebuku.setText("");
        txtjudulbuku.setText("");
        txtpenulisbuku.setText("");
        txtpenerbitbuku.setText("");
    }
    void nonaktif(){
        btntambah.setText("TAMBAH");
        txtkodebuku.setEnabled(false);
        txtjudulbuku.setEnabled(false);
        txtpenulisbuku.setEnabled(false);
        txtpenerbitbuku.setEnabled(false);
        btnsimpan.setEnabled(false);
        txtkatakunci.setEnabled(false);
    }
    void aktif(){
        txtkodebuku.setEnabled(true);
        txtjudulbuku.setEnabled(true);
        txtpenulisbuku.setEnabled(true);
        txtpenerbitbuku.setEnabled(true);
        btnsimpan.setEnabled(true);
        txtkodebuku.requestFocus();
    }
    void simpanData(){
    sql = "INSERT INTO tb_buku (kode_buku, judul_buku, penulis_buku, penerbit_buku) VALUES ("
            + "'" + txtkodebuku.getText() + "',"
            + "'" + txtjudulbuku.getText() + "',"
            + "'" + txtpenulisbuku.getText() + "',"
            + "'" + txtpenerbitbuku.getText() + "')";
    int status = koneksiDB.execute(sql);
        if (status == 1) {
            JOptionPane.showMessageDialog(this, "Simpan berhasil", "Informasi", JOptionPane.INFORMATION_MESSAGE);
        }else{
            JOptionPane.showMessageDialog(this, "Simpan gagal", "Informasi", JOptionPane.INFORMATION_MESSAGE);
        }
    }
    void ubahData(){
        int ubah = JOptionPane.showConfirmDialog(rootPane, "Anda yakin mengubah data ini ?","Tanya",JOptionPane.YES_NO_OPTION);
        if (ubah == 0) {
            sql = "UPDATE tb_buku SET "
                    + "judul_buku='" + txtjudulbuku.getText() + "',"
                    + "penulis_buku='" + txtpenulisbuku.getText() + "',"
                    + "penerbit_buku='" + txtpenerbitbuku.getText() + "'"
                    + " WHERE kode_buku='" + txtkodebuku.getText() + "'";
            int status = koneksiDB.execute(sql);
            if (status == 1) {
                JOptionPane.showMessageDialog(this, "Ubah Berhasil","Informasi",JOptionPane.INFORMATION_MESSAGE);
            }else{
                JOptionPane.showMessageDialog(this, "Ubah Gagal","Informasi",JOptionPane.INFORMATION_MESSAGE);
            }
        }
       
    }
    void hapusData(){
        int busak = JOptionPane.showConfirmDialog(rootPane, "Anda yakin menghapus data ini ?","Tanya",JOptionPane.YES_NO_OPTION);
        if (busak == 0) {
          sql = "DELETE FROM tb_buku where kode_buku='" + txtkodebuku.getText() + "'";
            int status = koneksiDB.execute(sql);
            if (status == 1) {
                JOptionPane.showMessageDialog(this, "Hapus berhasil", "Informasi", JOptionPane.INFORMATION_MESSAGE);
            }else{
                JOptionPane.showMessageDialog(this, "Hapus gagal", "Informasi", JOptionPane.INFORMATION_MESSAGE);
            }
        }
       
    }
}
koneksiDB.java

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class koneksiDB {
    public static Connection setKoneksi(){
        Connection conn = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection("jdbc:mysql://localhost/db_perpus?user=root");
            System.out.println("koneksi berhasil");
        } catch (ClassNotFoundException e) {
            System.out.println("jdbc.Driver tidak ditemukan");
            System.exit(0);
        }catch (SQLException ex){
            System.out.println("koneksi gagal : \n" + ex.toString());
            System.exit(0);
        }
        return conn;
    }
   
    public static int execute(String SQL) {
        int status = 0;
        Connection koneksi = setKoneksi();
        try {
            Statement st = koneksi.createStatement();
            status = st.executeUpdate(SQL);
        } catch (SQLException ex) {
            System.out.println("ERROR : \n" + ex.toString());
        }
        return status;
    }
    public static ResultSet executeQuery(String SQL) {
        ResultSet rs = null;
        Connection koneksi = setKoneksi();
        try {
            Statement st = koneksi.createStatement();
            rs = st.executeQuery(SQL);
        } catch (SQLException ex) {
            System.out.println("ERROR: \n" + ex.toString());
        }
        return rs;
    }
}
Untuk cekKoneksi
public class cekKoneksi {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here\
        koneksiDB konek = new koneksiDB();
        konek.setKoneksi();
    }

}
Download disini

0 komentar:

Posting Komentar