Kamis, 19 November 2015

Program System Apotek Menggunakan Eclipse (Project Akhir Java)

Diposting oleh BelajarAsik di 11.24
Assalamu’alaikum.. Kembali lagi dengan saya. Di sini saya akan berbagi tugas tentang mata Kuliah Pemrograman Berorientasi Objek (PBO).
Pada tugas ini akan digunakan UML dan juga eclipse. UML merupakan sebuah bahasa yang berdasarkan grafik/gambar untuk memvisualisasi, menspesifasikan, membangun dan pendokumentasian dari sebuah sistem pengembangan software berbasis OO (Object Oriented). UML berbentuk seperti diagram flowcart. Didalamnya ada Class, Atribut, dan Method yang akan digunakan dalam membangun sebuah program OOP. UML menunjukkan relasi antar Class dan juga kedudukan Class tersebut dalam program. Berikut adalah contoh UML System Apotek yang saya buat menggunakan StarUML.






Pada kesempatan ini, saya ingin berbagi tentang project akhir saya yang berkaitan dengan pemrograman java.. Pada project akhir semester 3 ini, saya menggunakan aplikasi eclipse untuk pemrograannya, dan menggunakan starUML untuk membuat UML nya.. Saya juga mencoba membuatnya dalam bentuk MVC.. Model, View dan control.. Saya menggunakan database xampp ya..
Oh ya.. Pada tugas akhir ini saya mengambil tema apotek.. Langsung saja ya..
Pertama, buat seluruh kelas pada view terlebih dahulu :
1. Kelas View Login
caranya sama, cukup bikin package dan kemudian  buat class dengan nama login.. Berikut ini source codenya :
package view;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class login extends JFrame implements ActionListener {

            Container konten = getContentPane();

            private JLabel lblUser = new JLabel("Username   : ");
            private JTextField txtUser = new JTextField();
            private JLabel lblPassword = new JLabel("Password   : ");
            private JPasswordField pfPassword = new JPasswordField();
            private JButton btnMasuk = new JButton("Masuk");
            private JButton btnKeluar = new JButton("Keluar");
            Color warna = Color.BLACK;

            public login() {
                        super("Login");
                        setVisible(true);
                        setSize(350, 300);
                        setLocationRelativeTo(null);
                        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

                        konten.setLayout(null);
                        konten.setBackground(warna);

                        lblUser.setBounds(130, 10, 100, 25);
                        txtUser.setBounds(70, 50, 200, 25);
                        konten.add(lblUser);
                        konten.add(txtUser);
                        txtUser.setToolTipText("Masukkan Username anda");
                        lblUser.setForeground(Color.WHITE);

                        lblPassword.setBounds(130, 95, 100, 25);
                        pfPassword.setBounds(70, 135, 200, 25);
                        konten.add(lblPassword);
                        konten.add(pfPassword);
                        pfPassword.setToolTipText("Masukkan Password anda");
                        lblPassword.setForeground(Color.WHITE);

                        btnMasuk.setBounds(60, 200, 100, 25);
                        konten.add(btnMasuk);
                        btnKeluar.setBounds(190, 200, 100, 25);
                        konten.add(btnKeluar);
                        btnMasuk.addActionListener(this);
                        btnKeluar.addActionListener(this);

            }

            @SuppressWarnings("deprecation")
            public void actionPerformed(ActionEvent act) {
                        Object obj = act.getSource();
                        if (obj == btnMasuk) {
                                    String user_app = txtUser.getText();
                                    String password_app = pfPassword.getText();
                                    if (user_app.equalsIgnoreCase("admin") && password_app.equalsIgnoreCase("admin")) {
                                                JOptionPane.showMessageDialog(null, "Selamat Datang");
                                                login.this.setVisible(false); // agar tampilan loginnya hilang
                                                                                                                                                // setelah main menu muncul
                                                new MainMenu().setVisible(true);
                                    } else {
                                                JOptionPane.showMessageDialog(null, "Password atau Username anda salah");
                                                login.this.setVisible(false);
                                    }
                        } else if (obj == btnKeluar) {
                                    JOptionPane.showMessageDialog(null, "Anda telah keluar");
                                    System.exit(0);
                        }
                        txtUser.setText(" ");
                        pfPassword.setText(" ");
                        txtUser.setRequestFocusEnabled(true);
            }

            private String pfPassword() {
                        // TODO Auto-generated method stub
                        return null;
            }
}
Dan ketika di run maka akan muncul tampilan seperti ini :

2. Buat class MainMenu pada package view :
berikut ini source code nya :
package view;

import javax.activation.DataSource;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import koneksi.Koneksi;
import java.sql.*;

public class MainMenu extends JFrame implements ActionListener {// implements
                                                                                                                                                                                                // manggil
                                                                                                                                                                                                // interface
                                                                                                                                                                                                // actionlistener
            Image image;

            public String judul, subjudul, garis, identitas;

            private static JButton btnObat;
            private static JButton btnMember;
            private static JButton btnPembelian;
            private static JButton btnKaryawan;

            Container konten = getContentPane(); // menambahkan konten
            Color warna = Color.green;

            public MainMenu() {

                        getComponents();
                        Koneksi kon = new Koneksi();
                        kon.connect();
                        JLabel lbjudul = new JLabel("Main Menu");
                        JLabel lbsubjudul = new JLabel("--Sistem Informasi Apotek--");
                        JLabel lbgaris = new JLabel(
                                              "***===***===***===***===***===***===***===***===***===***===***===***===***===***");
                        JLabel lbidentitas = new JLabel("AFINDA NOFI NURFIYANA - 5302414041");

                        btnObat = new JButton("OBAT");
                        btnMember = new JButton("MEMBER");
                        btnPembelian = new JButton("PEMBELIAN");
                        btnKaryawan = new JButton("KARYAWAN");

                        konten.setBackground(warna); // mengganti warna background
                        setLayout(null);
                        Dimension sizelbjudul = lbjudul.getPreferredSize();
                        Dimension sizelbsubjudul = lbsubjudul.getPreferredSize();
                        Dimension sizelbgaris = lbgaris.getPreferredSize();
                        Dimension sizelbidentitas = lbidentitas.getPreferredSize();

                        Dimension sizebtnObat = btnObat.getPreferredSize();
                        Dimension sizebtnMember = btnMember.getPreferredSize();
                        Dimension sizebtnPembelian = btnPembelian.getPreferredSize();
                        Dimension sizebtnKaryawan = btnKaryawan.getPreferredSize();

                        lbjudul.setBounds(150, 20, 200, 50);
                        lbjudul.setFont(new java.awt.Font("Matura MT Script Capitals", 30, 30));
                        lbsubjudul.setBounds(40, 65, 500, 50);
                        lbsubjudul.setFont(new java.awt.Font("Matura MT Script Capitals", 30, 30));
                        lbgaris.setBounds(20, 90, 500, 50);
                        lbgaris.setFont(new java.awt.Font("Matura MT Script Capitals", 10, 10));
                        lbidentitas.setBounds(8, 300, 500, 50);
                        lbidentitas.setFont(new java.awt.Font("Comic sans ms", 15, 13));

                        btnObat.setBounds(100, 160, 120, sizebtnObat.height);
                        btnMember.setBounds(100, 220, 120, sizebtnMember.height);
                        btnPembelian.setBounds(270, 160, 120, sizebtnPembelian.height);
                        btnKaryawan.setBounds(270, 220, 120, sizebtnKaryawan.height);

                        add(lbjudul);
                        add(lbsubjudul);
                        add(lbgaris);
                        add(lbidentitas);

                        add(btnObat);
                        add(btnMember);
                        add(btnPembelian);
                        add(btnKaryawan);

                        btnObat.setBackground(Color.WHITE);
                        btnMember.setBackground(Color.PINK);
                        btnPembelian.setBackground(Color.ORANGE);
                        btnKaryawan.setBackground(Color.MAGENTA);

                        btnObat.addActionListener(this);
                        btnMember.addActionListener(this);
                        btnPembelian.addActionListener(this);
                        btnKaryawan.addActionListener(this);

                        setSize(500, 380);
                        setTitle("APOTEK SEJAHTERA");
                        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            }

            public void actionPerformed(ActionEvent e) {
                        if (e.getSource() == btnObat) {
                                    try {
                                                // form di close - dispose();
                                                new Obat().setVisible(true);
                                    } catch (Exception es) {
                                                es.printStackTrace();
                                    }
                        } else if (e.getSource() == btnMember) {
                                    try {
                                                // form di close - dispose();
                                                new Member().setVisible(true);
                                    } catch (Exception es) {
                                                es.printStackTrace();
                                    }
                        }

                        else if (e.getSource() == btnPembelian) {
                                    try {
                                                // form di close - dispose();
                                                new Pembelian().setVisible(true); // panggil form guipeminjaman
                                    } catch (Exception es) {
                                                es.printStackTrace();
                                    }
                        } else if (e.getSource() == btnKaryawan) {
                                    try {
                                                // form di close - dispose();
                                                new Karyawan().setVisible(true);
                                    } catch (Exception es) {
                                                es.printStackTrace();
                                    }
                        }
            }

            public static void main(String args[]) throws Exception {
                        login s = new login();
                        s.setVisible(true);
            }
}
ketika di run akan muncul tampilan seperti ini :

3. buat class karyawan dengan source code seperti ini :
package view;

import java.awt.Color;
import java.awt.Container;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;

import koneksi.Koneksi;
import model.MdlKaryawan;

public class Karyawan extends JFrame {
            /**
             *
             */

            private JPanel contentPane;
            private JTextField txtIDKaryawan;
            private JTextField txtNama;
            private JTextField txtAlamat;
            private JTextField txtNoHP;
            private JTable tabel;
            String fieldTabel[] = { "ID Karyawan", "Nama", "Alamat", "Nomor HP" }; // membuat
                                                                                                                                                                                                                                    // field
                                                                                                                                                                                                                                    // tabel
                                                                                                                                                                                                                                    // dengan
                                                                                                                                                                                                                                    // array
                                                                                                                                                                                                                                    // 1
                                                                                                                                                                                                                                    // dimensi
            DefaultTableModel tabelModel;

            /**
             * create the frame.
             */
            /*
             * Konstruktor adalah method yang dipanggil pertama kali ketika program
             * dijalankan konstruktor namanya sama dengan nama class
             */

            public Karyawan() {

                        MdlKaryawan MdlK = new MdlKaryawan();

                        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                        setBounds(100, 100, 685, 620);
                        contentPane = new JPanel();
                        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
                        setContentPane(contentPane);
                        contentPane.setLayout(null);
                        // Buat object tabelModel dengan syntax new
                        tabelModel = new DefaultTableModel(null, fieldTabel);
                        /*
                         * null akan diisi data yang diambil dari database fieldTabel adalah
                         * nama field tabel
                         */
                        JLabel lblData = new JLabel("DATA KARYAWAN");
                        lblData.setBounds(230, 25, 300, 25);
                        contentPane.add(lblData);
                        lblData.setFont(new java.awt.Font("elephant", 10, 25));

                        JLabel lblIDKaryawan = new JLabel("ID Karyawan: ");
                        lblIDKaryawan.setBounds(15, 67, 80, 15);
                        contentPane.add(lblIDKaryawan);

                        JLabel lblNama = new JLabel("Nama          : ");
                        lblNama.setBounds(15, 95, 70, 15);
                        contentPane.add(lblNama);

                        JLabel lblAlamat = new JLabel("Alamat        : ");
                        lblAlamat.setBounds(15, 123, 70, 15);
                        contentPane.add(lblAlamat);

                        JLabel lblNoHP = new JLabel("No HP          : ");
                        lblNoHP.setBounds(15, 151, 70, 15);
                        contentPane.add(lblNoHP);

                        txtIDKaryawan = new JTextField();
                        txtIDKaryawan.setBounds(100, 65, 150, 19);
                        contentPane.add(txtIDKaryawan);
                        txtIDKaryawan.setColumns(10);

                        txtNama = new JTextField();
                        txtNama.setBounds(100, 95, 150, 19);
                        contentPane.add(txtNama);
                        txtNama.setColumns(10);

                        txtAlamat = new JTextField();
                        txtAlamat.setBounds(100, 124, 150, 19);
                        contentPane.add(txtAlamat);
                        txtAlamat.setColumns(10);

                        txtNoHP = new JTextField();
                        txtNoHP.setBounds(100, 151, 150, 19);
                        contentPane.add(txtNoHP);
                        txtNoHP.setColumns(10);

                        setTitle("APOTEK SEJAHTERA");
                        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

                        JLabel lblDaftar = new JLabel("DAFTAR KARYAWAN");
                        lblDaftar.setBounds(280, 230, 500, 15);
                        contentPane.add(lblDaftar);

                        JScrollPane scrollPane = new JScrollPane();
                        scrollPane.setBounds(70, 250, 525, 260);
                        contentPane.add(scrollPane);

                        tabel = new JTable();
                        tabel.setModel(tabelModel);
                        scrollPane.setViewportView(tabel);
                        tabel.addMouseListener(new MouseAdapter() {
                                    public void mouseReleased(MouseEvent e) {
                                                int a = tabel.getSelectedRow();
                                                if (a < 0) {
                                                            return;
                                                }
                                                String id = (String) tabelModel.getValueAt(a, 0);
                                                txtIDKaryawan.setText(id);
                                                String nama = (String) tabelModel.getValueAt(a, 1);
                                                txtNama.setText(nama);
                                                String alamat = (String) tabelModel.getValueAt(a, 2);
                                                txtAlamat.setText(alamat);
                                                String hp = (String) tabelModel.getValueAt(a, 3);
                                                txtNoHP.setText(hp);
                                    }
                        });

                        JButton btnSimpan = new JButton("Simpan");
                        btnSimpan.setBounds(70, 530, 160, 25);
                        btnSimpan.setBackground(Color.MAGENTA);
                        contentPane.add(btnSimpan);

                        btnSimpan.addActionListener(new ActionListener() {
                                    public void actionPerformed(ActionEvent evt) {
                                                // fungsi simpan
                                                // simpankaryawan();
                                                String IDKaryawan = txtIDKaryawan.getText();
                                                String Nama = txtNama.getText();
                                                String Alamat = txtAlamat.getText();
                                                String NoHP = txtNoHP.getText();
                                                if (!txtIDKaryawan.getText().isEmpty() && !txtNama.getText().isEmpty() && !txtAlamat.getText().isEmpty()
                                                                        && !txtNoHP.getText().isEmpty()) {
                                                            MdlK.simpankaryawan(IDKaryawan, Nama, Alamat, NoHP);
                                                            txtIDKaryawan.setText("");
                                                            txtNama.setText("");
                                                            txtAlamat.setText("");
                                                            txtNoHP.setText("");
                                                            tabel();
                                                } else {
                                                            JOptionPane.showMessageDialog(null, "Data belum lengkap", "Peringatan",
                                                                                    JOptionPane.WARNING_MESSAGE);
                                                }
                                    }
                        });

                        JButton btnUpdate = new JButton("Update");
                        btnUpdate.setBounds(245, 530, 160, 25);
                        btnUpdate.setBackground(Color.MAGENTA);
                        contentPane.add(btnUpdate);

                        btnUpdate.addActionListener(new ActionListener() {
                                    public void actionPerformed(ActionEvent evt) {
                                                // fungsi update
                                                // updatekaryawan();
                                                String IDKaryawan = txtIDKaryawan.getText();
                                                String Nama = txtNama.getText();
                                                String Alamat = txtAlamat.getText();
                                                String NoHP = txtNoHP.getText();
                                                if (!txtIDKaryawan.getText().isEmpty() && !txtNama.getText().isEmpty() && !txtAlamat.getText().isEmpty()
                                                                        && !txtNoHP.getText().isEmpty()) {
                                                            MdlK.updatekaryawan(IDKaryawan, Nama, Alamat, NoHP);
                                                            txtIDKaryawan.setText("");
                                                            txtNama.setText("");
                                                            txtAlamat.setText("");
                                                            txtNoHP.setText("");
                                                            tabel();
                                                } else {
                                                            JOptionPane.showMessageDialog(null, "Data belum lengkap", "Peringatan",
                                                                                    JOptionPane.WARNING_MESSAGE);
                                                }
                                    }
                        });

                        JButton btnHapus = new JButton("Hapus");
                        btnHapus.setBounds(420, 530, 160, 25);
                        btnHapus.setBackground(Color.MAGENTA);
                        contentPane.add(btnHapus);

                        btnHapus.addActionListener(new ActionListener() {
                                    public void actionPerformed(ActionEvent evt) {
                                                // fungsi hapus
                                                // hapuskaryawan();

                                                if (!txtIDKaryawan.getText().isEmpty()) {
                                                            String IDKaryawan = txtIDKaryawan.getText();
                                                            if (JOptionPane.showConfirmDialog(rootPane, "Yakin Menghapus?", "Hapus", 1) == 0) {
                                                                        MdlK.hapuskaryawan(IDKaryawan);
                                                                        txtIDKaryawan.setText("");
                                                                        txtNama.setText("");
                                                                        txtAlamat.setText("");
                                                                        txtNoHP.setText("");
                                                                        tabel();
                                                            }
                                                } else {
                                                            JOptionPane.showMessageDialog(null, "Data belum lengkap", "Peringatan",
                                                                                    JOptionPane.WARNING_MESSAGE);
                                                }
                                    }
                        });
                        tabel(); // tabel ini digunakan untuk memunculkan data secara satu per
                                                            // satu

            } // Akhir Konstruktor

            private void tabel() {
                        tabelModel.getDataVector().removeAllElements();
                        tabelModel.fireTableDataChanged();
                        try {
                                    java.sql.Connection con;
                                    con = new Koneksi().connect();
                                    String sql = "SELECT * FROM `karyawan`";
                                    Statement s = con.createStatement();
                                    ResultSet rs = s.executeQuery(sql);
                                    while (rs.next()) {
                                                tabelModel.addRow(new Object[] { rs.getString("id_karyawan"), rs.getString("nama"),
                                                                        rs.getString("alamat"), rs.getString("no_hp"), });
                                    }
                        } catch (Exception s) {
                        }

            }
}
ketika di run akan muncul tampilan seperti ini :

4. Buat class member pada package view dengan source code seperti ini :
package view;

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;

import koneksi.Koneksi;
import model.MdlMember;

public class Member extends JFrame {
            /**
             *
             */
            private JPanel contentPane;
            private JTextField txtIDMember;
            private JTextField txtNama;
            private JTextField txtAlamat;
            private JTextField txtNoHP;
            private JTable tabel;
            String fieldTabel[] = { "ID Member", "Nama", "Alamat", "Nomor HP" }; // membuat
                                                                                                                                                                                                                                    // field
                                                                                                                                                                                                                                    // tabel
                                                                                                                                                                                                                                    // dengan
                                                                                                                                                                                                                                    // array
                                                                                                                                                                                                                                    // 1
                                                                                                                                                                                                                                    // dimensi
            DefaultTableModel tabelModel;

            /**
             * create the frame.
             */
            /*
             * Konstruktor adalah method yang dipanggil pertama kali ketika program
             * dijalankan konstruktor namanya sama dengan nama class
             */

            public Member() {

                        MdlMember MdlM = new MdlMember();

                        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                        setBounds(100, 100, 685, 620);
                        contentPane = new JPanel();
                        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
                        setContentPane(contentPane);
                        contentPane.setLayout(null);
                        // Buat object tabelModel dengan syntax new
                        tabelModel = new DefaultTableModel(null, fieldTabel);
                        /*
                         * null akan diisi data yang diambil dari database fieldTabel adalah
                         * nama field tabel
                         */
                        JLabel lblData = new JLabel("DATA MEMBER");
                        lblData.setBounds(230, 25, 300, 25);
                        contentPane.add(lblData);
                        lblData.setFont(new java.awt.Font("elephant", 10, 25));

                        JLabel lblIDMember = new JLabel("ID Member : ");
                        lblIDMember.setBounds(15, 67, 70, 15);
                        contentPane.add(lblIDMember);

                        JLabel lblNama = new JLabel("Nama          : ");
                        lblNama.setBounds(15, 95, 70, 15);
                        contentPane.add(lblNama);

                        JLabel lblAlamat = new JLabel("Alamat        : ");
                        lblAlamat.setBounds(15, 123, 70, 15);
                        contentPane.add(lblAlamat);

                        JLabel lblNoHP = new JLabel("No HP          : ");
                        lblNoHP.setBounds(15, 151, 70, 15);
                        contentPane.add(lblNoHP);

                        txtIDMember = new JTextField();
                        txtIDMember.setBounds(100, 65, 150, 19);
                        contentPane.add(txtIDMember);
                        txtIDMember.setColumns(10);

                        txtNama = new JTextField();
                        txtNama.setBounds(100, 95, 150, 19);
                        contentPane.add(txtNama);
                        txtNama.setColumns(10);

                        txtAlamat = new JTextField();
                        txtAlamat.setBounds(100, 124, 150, 19);
                        contentPane.add(txtAlamat);
                        txtAlamat.setColumns(10);

                        txtNoHP = new JTextField();
                        txtNoHP.setBounds(100, 151, 150, 19);
                        contentPane.add(txtNoHP);
                        txtNoHP.setColumns(10);

                        setTitle("APOTEK SEJAHTERA");
                        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

                        JLabel lblDaftar = new JLabel("DAFTAR MEMBER");
                        lblDaftar.setBounds(280, 230, 500, 15);
                        contentPane.add(lblDaftar);

                        JScrollPane scrollPane = new JScrollPane();
                        scrollPane.setBounds(70, 250, 525, 260);
                        contentPane.add(scrollPane);

                        tabel = new JTable();
                        tabel.setModel(tabelModel);
                        scrollPane.setViewportView(tabel);
                        tabel.addMouseListener(new MouseAdapter() {
                                    public void mouseReleased(MouseEvent e) {
                                                int a = tabel.getSelectedRow();
                                                if (a < 0) {
                                                            return;
                                                }
                                                String id = (String) tabelModel.getValueAt(a, 0);
                                                txtIDMember.setText(id);
                                                String nama = (String) tabelModel.getValueAt(a, 1);
                                                txtNama.setText(nama);
                                                String alamat = (String) tabelModel.getValueAt(a, 2);
                                                txtAlamat.setText(alamat);
                                                String hp = (String) tabelModel.getValueAt(a, 3);
                                                txtNoHP.setText(hp);
                                    }
                        });

                        JButton btnSimpan = new JButton("Simpan");
                        btnSimpan.setBounds(70, 530, 160, 25);
                        btnSimpan.setBackground(Color.PINK);
                        contentPane.add(btnSimpan);

                        btnSimpan.addActionListener(new ActionListener() {
                                    public void actionPerformed(ActionEvent evt) {
                                                // fungsi simpan
                                                String IDMember = txtIDMember.getText();
                                                String Nama = txtNama.getText();
                                                String Alamat = txtAlamat.getText();
                                                String NoHP = txtNoHP.getText();
                                                if (!txtIDMember.getText().isEmpty() && !txtNama.getText().isEmpty() && !txtAlamat.getText().isEmpty()
                                                                        && !txtNoHP.getText().isEmpty()) {
                                                            MdlM.simpanmember(IDMember, Nama, Alamat, NoHP);
                                                            txtIDMember.setText("");
                                                            txtNama.setText("");
                                                            txtAlamat.setText("");
                                                            txtNoHP.setText("");
                                                            tabel();
                                                } else {
                                                            JOptionPane.showMessageDialog(null, "Data belum lengkap", "Peringatan",
                                                                                    JOptionPane.WARNING_MESSAGE);
                                                }
                                    }
                        });

                        JButton btnUpdate = new JButton("Update");
                        btnUpdate.setBounds(245, 530, 160, 25);
                        btnUpdate.setBackground(Color.PINK);
                        contentPane.add(btnUpdate);

                        btnUpdate.addActionListener(new ActionListener() {
                                    public void actionPerformed(ActionEvent evt) {
                                                // fungsi update
                                                String IDMember = txtIDMember.getText();
                                                String Nama = txtNama.getText();
                                                String Alamat = txtAlamat.getText();
                                                String NoHP = txtNoHP.getText();
                                                if (!txtIDMember.getText().isEmpty() && !txtNama.getText().isEmpty() && !txtAlamat.getText().isEmpty()
                                                                        && !txtNoHP.getText().isEmpty()) {
                                                            MdlM.updatemember(IDMember, Nama, Alamat, NoHP);
                                                            txtIDMember.setText("");
                                                            txtNama.setText("");
                                                            txtAlamat.setText("");
                                                            txtNoHP.setText("");
                                                            tabel();
                                                } else {
                                                            JOptionPane.showMessageDialog(null, "Data belum lengkap", "Peringatan",
                                                                                    JOptionPane.WARNING_MESSAGE);
                                                }
                                    }
                        });

                        JButton btnHapus = new JButton("Hapus");
                        btnHapus.setBounds(420, 530, 160, 25);
                        btnHapus.setBackground(Color.PINK);
                        contentPane.add(btnHapus);

                        btnHapus.addActionListener(new ActionListener() {
                                    public void actionPerformed(ActionEvent evt) {
                                                // fungsi hapus
                                                if (!txtIDMember.getText().isEmpty()) {
                                                            String IDMember = txtIDMember.getText();
                                                            if (JOptionPane.showConfirmDialog(rootPane, "Yakin Menghapus?", "Hapus", 1) == 0) {
                                                                        MdlM.hapusmember(IDMember);
                                                                        txtIDMember.setText("");
                                                                        txtNama.setText("");
                                                                        txtAlamat.setText("");
                                                                        txtNoHP.setText("");
                                                                        tabel();
                                                            }
                                                } else {
                                                            JOptionPane.showMessageDialog(null, "Data belum lengkap", "Peringatan",
                                                                                    JOptionPane.WARNING_MESSAGE);
                                                }
                                    }
                        });

                        tabel();

            } // Akhir Konstruktor

            private void tabel() {
                        tabelModel.getDataVector().removeAllElements();
                        tabelModel.fireTableDataChanged();
                        try {
                                    java.sql.Connection con; //METHOD MEMBUAT KONEKSI
                                    con = new Koneksi().connect();
                                    String sql = "SELECT * FROM `member`";
                                    Statement s = con.createStatement();
                                    ResultSet rs = s.executeQuery(sql);
                                    while (rs.next()) {
                                                tabelModel.addRow(new Object[] { rs.getString("id_member"), rs.getString("nama"),
                                                                        rs.getString("alamat"), rs.getString("no_hp"), });
                                    }
                        } catch (Exception s) {
                        }

            }
}
ketika di run akan muncul tampilan seperti ini :

5. Buat class Obat dengan source code seperti ini :
package view;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;

import koneksi.Koneksi;
import model.MdlObat;

public class Obat extends JFrame {
            /**
             *
             */
            private JPanel contentPane;
            private JTextField txtKode;
            private JTextField txtNama;
            private JTextField txtHarga;
            private JTextField txtJumlah;
            private JTable tabel;
            String fieldTabel[] = { "Kode Obat", "Nama", "Harga", "Jumlah" }; // membuat
                                                                                                                                                                                                                        // field
                                                                                                                                                                                                                        // tabel
                                                                                                                                                                                                                        // dengan
                                                                                                                                                                                                                        // array
                                                                                                                                                                                                                        // 1
                                                                                                                                                                                                                        // dimensi
            DefaultTableModel tabelModel;

            /**
             * create the frame.
             */
            /*
             * Konstruktor adalah method yang dipanggil pertama kali ketika program
             * dijalankan konstruktor namanya sama dengan nama class
             */

            public Obat() {
                        MdlObat MdlO = new MdlObat();

                        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                        setBounds(100, 100, 685, 620);
                        contentPane = new JPanel();
                        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
                        setContentPane(contentPane);
                        contentPane.setLayout(null);
                        // Buat object tabelModel dengan syntax new
                        tabelModel = new DefaultTableModel(null, fieldTabel);
                        /*
                         * null akan diisi data yang diambil dari database fieldTabel adalah
                         * nama field tabel
                         */
                        JLabel lblData = new JLabel("DATA OBAT");
                        lblData.setBounds(250, 25, 200, 20);
                        contentPane.add(lblData);
                        lblData.setFont(new java.awt.Font("Elephant", 10, 25));

                        JLabel lblKode = new JLabel("Kode Obat : ");
                        lblKode.setBounds(15, 67, 70, 15);
                        contentPane.add(lblKode);

                        JLabel lblNama = new JLabel("Nama          : ");
                        lblNama.setBounds(15, 95, 70, 15);
                        contentPane.add(lblNama);

                        JLabel lblHarga = new JLabel("Harga          : ");
                        lblHarga.setBounds(15, 123, 70, 15);
                        contentPane.add(lblHarga);

                        JLabel lblJumlah = new JLabel("Jumlah       : ");
                        lblJumlah.setBounds(15, 151, 70, 15);
                        contentPane.add(lblJumlah);

                        txtKode = new JTextField();
                        txtKode.setBounds(100, 65, 150, 19);
                        contentPane.add(txtKode);
                        txtKode.setColumns(10);

                        txtNama = new JTextField();
                        txtNama.setBounds(100, 95, 150, 19);
                        contentPane.add(txtNama);
                        txtNama.setColumns(10);

                        txtHarga = new JTextField();
                        txtHarga.setBounds(100, 124, 150, 19);
                        contentPane.add(txtHarga);
                        txtHarga.setColumns(10);

                        txtJumlah = new JTextField();
                        txtJumlah.setBounds(100, 151, 150, 19);
                        contentPane.add(txtJumlah);
                        txtJumlah.setColumns(10);

                        setTitle("APOTEK SEJAHTERA");
                        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

                        JLabel lblDaftar = new JLabel("DAFTAR OBAT");
                        lblDaftar.setBounds(280, 230, 500, 15);
                        contentPane.add(lblDaftar);

                        JScrollPane scrollPane = new JScrollPane();
                        scrollPane.setBounds(70, 250, 525, 260);
                        contentPane.add(scrollPane);

                        tabel = new JTable();
                        tabel.setModel(tabelModel);
                        // digunakan untuk mengatur tabelnya agar bagian di atas terisi seperti
                        // yang di tabel
                        scrollPane.setViewportView(tabel);
                        tabel.addMouseListener(new MouseAdapter() {
                                    public void mouseReleased(MouseEvent e) {
                                                int a = tabel.getSelectedRow();
                                                if (a < 0) {
                                                            return;
                                                }
                                                String id = (String) tabelModel.getValueAt(a, 0);
                                                txtKode.setText(id);
                                                String nama = (String) tabelModel.getValueAt(a, 1);
                                                txtNama.setText(nama);
                                                String harga = (String) tabelModel.getValueAt(a, 2);
                                                txtHarga.setText(harga);
                                                String jumlah = (String) tabelModel.getValueAt(a, 3);
                                                txtJumlah.setText(jumlah);
                                    }
                        });

                        JButton btnSimpan = new JButton("Simpan");
                        btnSimpan.setBounds(70, 530, 160, 25);
                        btnSimpan.setBackground(Color.YELLOW);
                        contentPane.add(btnSimpan);
                        btnSimpan.addActionListener(new ActionListener() {
                                    public void actionPerformed(ActionEvent evt) {
                                                // fungsi simpan
                                                // simpanobat();
                                                String Kode = txtKode.getText();
                                                String Nama = txtNama.getText();
                                                String Harga = txtHarga.getText();
                                                String Jumlah = txtJumlah.getText();
                                                if (!txtKode.getText().isEmpty() && !txtNama.getText().isEmpty() && !txtHarga.getText().isEmpty()
                                                                        && !txtJumlah.getText().isEmpty()) {
                                                            MdlO.simpanobat(Kode, Nama, Harga, Jumlah);
                                                            txtKode.setText("");
                                                            txtNama.setText("");
                                                            txtHarga.setText("");
                                                            txtJumlah.setText("");
                                                            tabel();
                                                } else {
                                                            JOptionPane.showMessageDialog(null, "Data belum lengkap", "Peringatan",
                                                                                    JOptionPane.WARNING_MESSAGE);
                                                }
                                    }
                        });

                        JButton btnUpdate = new JButton("Update");
                        btnUpdate.setBounds(245, 530, 160, 25);
                        btnUpdate.setBackground(Color.YELLOW);
                        contentPane.add(btnUpdate);

                        btnUpdate.addActionListener(new ActionListener() {
                                    public void actionPerformed(ActionEvent evt) {
                                                // fungsi update
                                                // updateobat();
                                                String Kode = txtKode.getText();
                                                String Nama = txtNama.getText();
                                                String Harga = txtHarga.getText();
                                                String Jumlah = txtJumlah.getText();
                                                if (!txtKode.getText().isEmpty() && !txtNama.getText().isEmpty() && !txtHarga.getText().isEmpty()
                                                                        && !txtJumlah.getText().isEmpty()) {
                                                            MdlO.updateobat(Kode, Nama, Harga, Jumlah);
                                                            txtKode.setText("");
                                                            txtNama.setText("");
                                                            txtHarga.setText("");
                                                            txtJumlah.setText("");
                                                            tabel();
                                                } else {
                                                            JOptionPane.showMessageDialog(null, "Data belum lengkap", "Peringatan",
                                                                                    JOptionPane.WARNING_MESSAGE);
                                                }
                                    }
                        });

                        JButton btnHapus = new JButton("Hapus");
                        btnHapus.setBounds(420, 530, 160, 25);
                        btnHapus.setBackground(Color.YELLOW);
                        contentPane.add(btnHapus);
                        btnHapus.addActionListener(new ActionListener() {
                                    public void actionPerformed(ActionEvent evt) {
                                                // fungsi hapus
                                                // hapusobat();
                                                if (!txtKode.getText().isEmpty()) {
                                                            String Kode = txtKode.getText();
                                                            if (JOptionPane.showConfirmDialog(rootPane, "Yakin Menghapus?", "Hapus", 1) == 0) {
                                                                        MdlO.hapusobat(Kode);
                                                                        txtKode.setText("");
                                                                        txtNama.setText("");
                                                                        txtHarga.setText("");
                                                                        txtJumlah.setText("");
                                                                        tabel();
                                                            }
                                                } else {
                                                            JOptionPane.showMessageDialog(null, "Data belum lengkap", "Peringatan",
                                                                                    JOptionPane.WARNING_MESSAGE);
                                                }
                                    }
                        });

                        tabel();

            } // Akhir Konstruktor

            /**
             * Launch the application.
             */
            private void tabel() {
                        tabelModel.getDataVector().removeAllElements();
                        tabelModel.fireTableDataChanged();
                        try {
                                    java.sql.Connection con;
                                    con = new Koneksi().connect();
                                    String sql = "SELECT * FROM `obat`";
                                    Statement s = con.createStatement();
                                    ResultSet rs = s.executeQuery(sql);
                                    while (rs.next()) {
                                                tabelModel.addRow(new Object[] { rs.getString("id_obat"), rs.getString("nama"), rs.getString("harga"),
                                                                        rs.getString("stok"), });
                                    }
                        } catch (Exception s) {
                        }

            }
}
ketika di run akan muncul tampilan seperti ini :

6. Buat class pembelian dengan source code seperti ini :
package view;

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;

import koneksi.Koneksi;
import model.MdlPembelian;

public class Pembelian extends JFrame {
            /**
             *
             */
            private JPanel contentPane;
            private JTextField txtIDMember;
            private JTextField txtIDKaryawan;
            private JTextField txtKode;
            private JTextField txtJumlah;
            private JTextField txtHarga;
            private JTable tabel;
            String fieldTabel[] = { "ID Member", "ID Karyawan", "Kode", "Jumlah", "Harga" }; // membuat
                                                                                                                                                                                                                                                                        // field
                                                                                                                                                                                                                                                                        // tabel
                                                                                                                                                                                                                                                                        // dengan
                                                                                                                                                                                                                                                                        // array
                                                                                                                                                                                                                                                                        // 1
                                                                                                                                                                                                                                                                        // dimensi
            DefaultTableModel tabelModel;

            /**
             * create the frame.
             */
            /*
             * Konstruktor adalah method yang dipanggil pertama kali ketika program
             * dijalankan konstruktor namanya sama dengan nama class
             */

            public Pembelian() {

                        MdlPembelian MdlP = new MdlPembelian();

                        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                        setBounds(100, 100, 685, 620);
                        contentPane = new JPanel();
                        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
                        setContentPane(contentPane);
                        contentPane.setLayout(null);
                        // Buat object tabelModel dengan syntax new
                        tabelModel = new DefaultTableModel(null, fieldTabel);
                        /*
                         * null akan diisi data yang diambil dari database fieldTabel adalah
                         * nama field tabel
                         */
                        JLabel lblData = new JLabel("DATA PEMBELIAN");
                        lblData.setBounds(200, 25, 300, 20);
                        contentPane.add(lblData);
                        lblData.setFont(new java.awt.Font("ELEPHANT", 10, 25));

                        JLabel lblIDMember = new JLabel("ID Member    : ");
                        lblIDMember.setBounds(15, 67, 90, 15);
                        contentPane.add(lblIDMember);

                        JLabel lblIDKaryawan = new JLabel("ID Karyawan : ");
                        lblIDKaryawan.setBounds(15, 95, 90, 15);
                        contentPane.add(lblIDKaryawan);

                        JLabel lblKode = new JLabel("Kode               : ");
                        lblKode.setBounds(15, 123, 90, 15);
                        contentPane.add(lblKode);

                        JLabel lblJumlah = new JLabel("Jumlah          : ");
                        lblJumlah.setBounds(15, 151, 90, 15);
                        contentPane.add(lblJumlah);

                        JLabel lblHarga = new JLabel("Harga             : ");
                        lblHarga.setBounds(15, 179, 90, 15);
                        contentPane.add(lblHarga);

                        txtIDMember = new JTextField();
                        txtIDMember.setBounds(100, 65, 150, 19);
                        contentPane.add(txtIDMember);
                        txtIDMember.setColumns(10);

                        txtIDKaryawan = new JTextField();
                        txtIDKaryawan.setBounds(100, 95, 150, 19);
                        contentPane.add(txtIDKaryawan);
                        txtIDKaryawan.setColumns(10);

                        txtKode = new JTextField();
                        txtKode.setBounds(100, 124, 150, 19);
                        contentPane.add(txtKode);
                        txtKode.setColumns(10);

                        txtJumlah = new JTextField();
                        txtJumlah.setBounds(100, 151, 150, 19);
                        contentPane.add(txtJumlah);
                        txtJumlah.setColumns(10);

                        txtHarga = new JTextField();
                        txtHarga.setBounds(100, 178, 150, 19);
                        contentPane.add(txtHarga);
                        txtHarga.setColumns(10);

                        setTitle("APOTEK SEJAHTERA");
                        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

                        JLabel lblDaftar = new JLabel("DAFTAR PEMBELIAN");
                        lblDaftar.setBounds(280, 230, 500, 15);
                        contentPane.add(lblDaftar);

                        JScrollPane scrollPane = new JScrollPane();
                        scrollPane.setBounds(70, 250, 525, 260);
                        contentPane.add(scrollPane);

                        tabel = new JTable();
                        tabel.setModel(tabelModel);
                        scrollPane.setViewportView(tabel);

                        tabel = new JTable();
                        tabel.setModel(tabelModel);
                        scrollPane.setViewportView(tabel);
                        tabel.addMouseListener(new MouseAdapter() {
                                    public void mouseReleased(MouseEvent e) {
                                                int a = tabel.getSelectedRow();
                                                if (a < 0) {
                                                            return;
                                                }
                                                String id1 = (String) tabelModel.getValueAt(a, 0);
                                                txtIDMember.setText(id1);
                                                String id2 = (String) tabelModel.getValueAt(a, 1);
                                                txtIDKaryawan.setText(id2);
                                                String kode = (String) tabelModel.getValueAt(a, 2);
                                                txtKode.setText(kode);
                                                String jumlah = (String) tabelModel.getValueAt(a, 3);
                                                txtJumlah.setText(jumlah);
                                                String harga = (String) tabelModel.getValueAt(a, 4);
                                                txtHarga.setText(harga);
                                    }
                        });

                        JButton btnSimpan = new JButton("Simpan");
                        btnSimpan.setBounds(70, 530, 160, 25);
                        btnSimpan.setBackground(Color.GREEN);
                        contentPane.add(btnSimpan);

                        btnSimpan.addActionListener(new ActionListener() {
                                    public void actionPerformed(ActionEvent evt) {
                                                // fungsi simpan
                                                // simpanpembelian(); jika digabung dan sampai ini saja bawahnya
                                                // tidak ada script lagi
                                                String IDMember = txtIDMember.getText();
                                                String IDKaryawan = txtIDKaryawan.getText();
                                                String Kode = txtKode.getText();
                                                String Jumlah = txtJumlah.getText();
                                                String Harga = txtHarga.getText();
                                                if (!txtIDMember.getText().isEmpty() && !txtIDKaryawan.getText().isEmpty()
                                                                        && !txtKode.getText().isEmpty() && !txtJumlah.getText().isEmpty()
                                                                        && !txtHarga.getText().isEmpty()) {
                                                            MdlP.simpanpembelian(IDMember, IDKaryawan, Kode, Jumlah, Harga);
                                                            txtIDMember.setText("");
                                                            txtIDKaryawan.setText("");
                                                            txtKode.setText("");
                                                            txtJumlah.setText("");
                                                            txtHarga.setText("");
                                                            tabel();
                                                } else {
                                                            JOptionPane.showMessageDialog(null, "Data belum lengkap", "Peringatan",
                                                                                    JOptionPane.WARNING_MESSAGE);
                                                }
                                    }
                        });

                        JButton btnUpdate = new JButton("Update");
                        btnUpdate.setBounds(245, 530, 160, 25);
                        btnUpdate.setBackground(Color.GREEN);
                        contentPane.add(btnUpdate);

                        btnUpdate.addActionListener(new ActionListener() {
                                    public void actionPerformed(ActionEvent evt) {
                                                // fungsi update
                                                // updatepembelian();
                                                String IDMember = txtIDMember.getText();
                                                String IDKaryawan = txtIDKaryawan.getText();
                                                String Kode = txtKode.getText();
                                                String Jumlah = txtJumlah.getText();
                                                String Harga = txtHarga.getText();
                                                if (!txtIDMember.getText().isEmpty() && !txtIDKaryawan.getText().isEmpty()
                                                                        && !txtKode.getText().isEmpty() && !txtJumlah.getText().isEmpty()
                                                                        && !txtHarga.getText().isEmpty()) {
                                                            MdlP.updatepembelian(IDMember, IDKaryawan, Kode, Jumlah, Harga);
                                                            txtIDMember.setText("");
                                                            txtIDKaryawan.setText("");
                                                            txtKode.setText("");
                                                            txtJumlah.setText("");
                                                            txtHarga.setText("");
                                                            tabel();
                                                } else {
                                                            JOptionPane.showMessageDialog(null, "Data belum lengkap", "Peringatan",
                                                                                    JOptionPane.WARNING_MESSAGE);
                                                }
                                    }
                        });

                        JButton btnHapus = new JButton("Hapus");
                        btnHapus.setBounds(420, 530, 160, 25);
                        btnHapus.setBackground(Color.GREEN);
                        contentPane.add(btnHapus);

                        btnHapus.addActionListener(new ActionListener() {
                                    public void actionPerformed(ActionEvent evt) {
                                                // fungsi hapus
                                                // hapuspembelian();
                                                if (!txtIDMember.getText().isEmpty()) {
                                                            String IDMember = txtIDMember.getText();
                                                            if (JOptionPane.showConfirmDialog(rootPane, "Yakin Menghapus?", "Hapus", 1) == 0) {
                                                                        MdlP.hapuspembelian(IDMember);
                                                                        txtIDMember.setText("");
                                                                        txtIDKaryawan.setText("");
                                                                        txtKode.setText("");
                                                                        txtJumlah.setText("");
                                                                        txtHarga.setText("");
                                                                        tabel();
                                                            }
                                                } else {
                                                            JOptionPane.showMessageDialog(null, "Data belum lengkap", "Peringatan",
                                                                                    JOptionPane.WARNING_MESSAGE);
                                                }
                                    }
                        });

                        tabel();

            } // Akhir Konstruktor

            private void tabel() {
                        // TODO Auto-generated method stub
                        tabelModel.getDataVector().removeAllElements();
                        tabelModel.fireTableDataChanged();

                        try {
                                    java.sql.Connection con;
                                    con = new Koneksi().connect();
                                    String sql = "SELECT * FROM `pembelian`";
                                    Statement s = con.createStatement();
                                    ResultSet rs = s.executeQuery(sql);
                                    while (rs.next()) {
                                                tabelModel.addRow(new Object[] { rs.getString("id_member"), rs.getString("id_karyawan"),
                                                                        rs.getString("kode"), rs.getString("jumlah"), rs.getString("harga"), });
                                    }
                        } catch (Exception s) {
                        }

            }
}
ketika di run maka akan muncul tampilan sepeti ini :

Sekarang buat package model
7. Buat class MdlMember dengan source code seperti ini :
package model;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;

import javax.swing.JOptionPane;

import koneksi.Koneksi;

public class MdlMember {

                        public void simpanmember(String IDMember, String Nama, String Alamat, String NoHP) {
                    java.sql.Connection con;
                    con = new Koneksi().connect();
                    String sql = "INSERT INTO `member`(`id_member`, `nama`, `alamat`, `no_hp`) VALUES (?,?,?,?)";
                    PreparedStatement pstmt;
                    try {
                            pstmt = con.prepareStatement(sql);
                            pstmt.setString(1, IDMember);
                            pstmt.setString(2, Nama);
                            pstmt.setString(3, Alamat);
                            pstmt.setString(4, NoHP);
                            pstmt.executeUpdate();
                            JOptionPane.showMessageDialog(null, "Berhasil menyimpan", "sukses", JOptionPane.INFORMATION_MESSAGE);
                    } catch (SQLException e) {
                            // TODO Auto-generated catch block
                            JOptionPane.showMessageDialog(null, "Gagal Menyimpan", "Peringatan", JOptionPane.WARNING_MESSAGE);
                    }
                }

                public void hapusmember(String IDMember) {
                    java.sql.Connection con;
                    con = new Koneksi().connect();
                    String sql = "DELETE FROM `member` WHERE `id_member`='" + IDMember + "' ";
                    Statement s;
                    try {
                            s = con.createStatement();
                            s.executeUpdate(sql);
                            JOptionPane.showMessageDialog(null, "Data Terhapus", "sukses", JOptionPane.INFORMATION_MESSAGE);                                              
                    } catch (SQLException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                    }
                }

                public void updatemember(String IDMember, String Nama, String Alamat, String NoHP) {
                    java.sql.Connection con;
                    con = new Koneksi().connect();
                    String sql = "UPDATE `member` SET `nama`='" + Nama + "',`alamat`='" + Alamat + "',`no_hp`='" + NoHP + "' WHERE `id_member`='" + IDMember + "' ";
                    Statement s;
                    try {
                            s = con.createStatement();
                            s.executeUpdate(sql);
                            JOptionPane.showMessageDialog(null, "Data Berhasil Diubah", "sukses", JOptionPane.INFORMATION_MESSAGE);
                    } catch (SQLException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                    }
                }
            }
2. Buat class MdlKaryawan dengan source code seperti ini :
package model;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;

import javax.swing.JOptionPane;

import koneksi.Koneksi;

publicclass MdlKaryawan {

           
            publicvoid simpankaryawan(String IDKaryawan, String Nama, String Alamat, String NoHP) {
                    java.sql.Connection con;
                    con = new Koneksi().connect();
                    String sql = "INSERT INTO `karyawan`(`id_karyawan`, `nama`, `alamat`, `no_hp`) VALUES (?,?,?,?)";
                    PreparedStatement pstmt;
            try {
                            pstmt = con.prepareStatement(sql);
                            pstmt.setString(1, IDKaryawan);
                            pstmt.setString(2, Nama);
                            pstmt.setString(3, Alamat);
                            pstmt.setString(4, NoHP);
                            pstmt.executeUpdate();
                            JOptionPane.showMessageDialog(null, "Berhasil menyimpan", "sukses", JOptionPane.INFORMATION_MESSAGE);
                    } catch (SQLException e) {
                            // TODO Auto-generated catch block
                            JOptionPane.showMessageDialog(null, "Gagal Menyimpan", "Peringatan", JOptionPane.WARNING_MESSAGE);
                    }
                }

            publicvoid hapuskaryawan(String IDKaryawan) {
                    java.sql.Connection con;
                    con = new Koneksi().connect();
                    String sql = "DELETE FROM `karyawan` WHERE `id_karyawan`='" + IDKaryawan + "' ";
                    Statement s;
            try {
                            s = con.createStatement();
                            s.executeUpdate(sql);
                            JOptionPane.showMessageDialog(null, "Data Terhapus", "sukses", JOptionPane.INFORMATION_MESSAGE);                                             
                    } catch (SQLException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                    }
                }

            publicvoid updatekaryawan(String IDKaryawan, String Nama, String Alamat, String NoHP) {
                    java.sql.Connection con;
                    con = new Koneksi().connect();
                    String sql = "UPDATE `karyawan` SET `nama`='" + Nama + "',`alamat`='" + Alamat + "',`no_hp`='" + NoHP + "' WHERE `id_karyawan`='" + IDKaryawan + "' ";
                    Statement s;
            try {
                            s = con.createStatement();
                            s.executeUpdate(sql);
                            JOptionPane.showMessageDialog(null, "Data Berhasil Diubah", "sukses", JOptionPane.INFORMATION_MESSAGE);
                    } catch (SQLException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                    }
                }

}
3. Buat class MdlObat dengan source code seperti ini :
package model;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;

import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import koneksi.Koneksi;

public class MdlObat {
            private JPanel contentPane;
            private JTextField txtKode;
            private JTextField txtNama;
            private JTextField txtHarga;
            private JTextField txtJumlah;

            public void simpanobat(String Kode, String Nama, String Harga, String Jumlah) {
                        java.sql.Connection con;
                        con = new Koneksi().connect();
                        String sql = "INSERT INTO `obat`(`id_obat`, `nama`, `harga`, `stok`) VALUES (?,?,?,?)";
                        PreparedStatement pstmt;
                        try {
                                    pstmt = con.prepareStatement(sql);
                                    pstmt.setString(1, Kode);
                                    pstmt.setString(2, Nama);
                                    pstmt.setString(3, Harga);
                                    pstmt.setString(4, Jumlah);
                                    pstmt.executeUpdate();
                                    JOptionPane.showMessageDialog(null, "Berhasil menyimpan", "sukses", JOptionPane.INFORMATION_MESSAGE);
                        } catch (SQLException e) {
                                    // TODO Auto-generated catch block
                                    JOptionPane.showMessageDialog(null, "Gagal Menyimpan", "Peringatan", JOptionPane.WARNING_MESSAGE);
                        }
            }

            public void hapusobat(String Kode) {
                        java.sql.Connection con;
                        con = new Koneksi().connect();
                        String sql = "DELETE FROM `obat` WHERE `id_obat`='" + Kode + "' ";
                        Statement s;
                        try {
                                    s = con.createStatement();
                                    s.executeUpdate(sql);
                                    JOptionPane.showMessageDialog(null, "Data Terhapus", "sukses", JOptionPane.INFORMATION_MESSAGE);
                        } catch (SQLException e) {
                                    // TODO Auto-generated catch block
                                    e.printStackTrace();
                        }
            }

            public void updateobat(String Kode, String Nama, String Harga, String Jumlah) {
        java.sql.Connection con;
        con = new Koneksi().connect();
        String sql = "UPDATE `obat` SET `id_obat`='" + Kode + "',`nama`='" + Nama + "',`harga`='" + Harga + "',`stok`='" + Jumlah + "' WHERE `id_obat`='" + Kode + "' ";
        Statement s;
        try {
                s = con.createStatement();
                s.executeUpdate(sql);
                JOptionPane.showMessageDialog(null, "Data Berhasil Diubah", "sukses", JOptionPane.INFORMATION_MESSAGE);
        } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
        }
    }
}
4. Buat class MdlPembelian dengan source code seperti ini :
package model;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;

import javax.swing.JOptionPane;

import koneksi.Koneksi;

public class MdlPembelian {

                public void simpanpembelian(String IDMember, String IDKaryawan, String Kode, String Jumlah, String Harga) {
                    java.sql.Connection con;
                    con = new Koneksi().connect();
                    String sql = "INSERT INTO `pembelian`(`id_member`, `id_karyawan`, `kode`, `jumlah`, `harga`) VALUES (?,?,?,?,?)";
                    PreparedStatement pstmt;
                    try {
                            pstmt = con.prepareStatement(sql);
                            pstmt.setString(1, IDMember);
                            pstmt.setString(2, IDKaryawan);
                            pstmt.setString(3, Kode);
                            pstmt.setString(4, Jumlah);
                            pstmt.setString(5, Harga);
                            pstmt.executeUpdate();
                            JOptionPane.showMessageDialog(null, "Berhasil menyimpan", "sukses", JOptionPane.INFORMATION_MESSAGE);
                    } catch (SQLException e) {
                            // TODO Auto-generated catch block
                            JOptionPane.showMessageDialog(null, "Gagal Menyimpan", "Peringatan", JOptionPane.WARNING_MESSAGE);
                    }
                }

                public void hapuspembelian(String IDMember) {
                    java.sql.Connection con;
                    con = new Koneksi().connect();
                    String sql = "DELETE FROM `pembelian` WHERE `id_member`='" + IDMember + "' ";
                    Statement s;
                    try {
                            s = con.createStatement();
                            s.executeUpdate(sql);
                            JOptionPane.showMessageDialog(null, "Data Terhapus", "sukses", JOptionPane.INFORMATION_MESSAGE);                                              
                    } catch (SQLException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                    }
                }

                public void updatepembelian(String IDMember, String IDKaryawan, String Kode, String Jumlah, String Harga) {
                    java.sql.Connection con;
                    con = new Koneksi().connect();
                    String sql = "UPDATE `pembelian` SET `id_member`='" + IDMember + "',`id_karyawan`='" + IDKaryawan + "',`kode`='" + Kode + "',`jumlah`='" + Jumlah + "',`harga`='" + Harga + "' WHERE `id_member`='" + IDMember + "' ";
                    Statement s;
                    try {
                            s = con.createStatement();
                            s.executeUpdate(sql);
                            JOptionPane.showMessageDialog(null, "Data Berhasil Diubah", "sukses", JOptionPane.INFORMATION_MESSAGE);
                    } catch (SQLException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                    }
                }

            }
Setelah itu buat package main
dengan class bernama main dan berikut ini source code nya :
package main;

import view.login;

publicclass main {

            publicstaticvoidmain(String[] args) {
                        // TODO Auto-generated method stub
                        new login().setVisible(true);
            }
}
Setelah itu buat package koneksi dengan source code berikut :
package koneksi;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import javax.swing.JOptionPane;

public class Koneksi {
            private Connection koneksi = null;

            public Connection connect() {
                        // untuk koneksi ke driver
                        try {
                                    // try mencari library
                                    Class.forName("com.mysql.jdbc.Driver"); //LOAD DRIVER
                                    System.out.println("berhasil load driver");
                        } catch (ClassNotFoundException cnfe) {
                                    System.out.println("Tidak ada Driver " + cnfe);
                        }

                        // untuk koneksi ke database
                        try {
                                    String url = "jdbc:mysql://localhost:3306/apotek"; //DRIVER MANA YG AKAN DIGUNAKAN
                                    koneksi = DriverManager.getConnection(url, "root", "");
                                    System.out.println("Berhasil koneksi");
                                    // JOptionPane.showMessageDialog(null,"Berhasil
                                    // koneksi","sukses",JOptionPane.INFORMATION_MESSAGE);
                        } catch (SQLException se) {
                                    System.out.println("Gagal koneksi " + se);
                                    JOptionPane.showMessageDialog(null, "Gagal Koneksi Database", "Peringatan", JOptionPane.WARNING_MESSAGE);
                        }
                        return koneksi;
            }
}
kemudian klik kanan pada nama project dengan pilih build path kemudian pilih libraries kemudian pilih mysql.jar pada laptop anda untuk menghubungkan ke database.






Tinggal di run xampp nya.. dan run project java dalam eclipse anda.. Demikian tutorial dari saya.. semoga bermanfaat..





1 komentar:

Vinny nd on 11 Maret 2022 pukul 13.45 mengatakan...

Makasih banyak kakak

Posting Komentar

 

Afinda Nofi Nurfiyana Copyright © 2011 Designed by Ipietoon Blogger Template Sponsored by web hosting