Tempat Berbagi Ilmu dan Pengalaman

Blog ini menampilkan berbagai artikel artikel yang dapat menambah pengetahuan dan membantu para mahasiswa untuk meningkatkan pengetahuan dan membantu tugas kuliah

Pemrograman CRUD

 Pemrograman Visual CRUD dengan Koneksi JDBC:sqlite3

import javax.swing.*;
import javax.swing.table.*;
import java.awt.event.*;
import java.awt.*;
import java.awt.Font;
import java.awt.Color;
import java.sql.*;

    public class sewalosmen1 extends JFrame implements ActionListener
    {
            JLabel lbljudul = new JLabel("PEMESANAN KAMAR");
            JLabel lblIDPelanggan = new JLabel("ID Pelanggan       :");
            JLabel lblNama = new JLabel("Nama                    :");
            JLabel lblTgl = new JLabel("Tanggal Cek In    :");
            JLabel lblAlamat = new JLabel("Alamat                  :");
            JLabel lblJenisKamar = new JLabel("JenisKamar        :");
            JLabel lblMember = new JLabel("Member                :");
            JLabel lbllama = new JLabel("Lama Menginap :");
            JLabel lblTelp = new JLabel("Telp                       :");

            JTextField tfIDPelanggan = new JTextField();
            JTextField tfNama = new JTextField();
            JTextField tfTgl = new JTextField();
            JTextField tfAlamat = new JTextField();

            ButtonGroup btnGrup=new ButtonGroup();
            JRadioButton rDeluxeRoom = new JRadioButton("1. Deluxe Room Rp 275.000/hari");
            JRadioButton rSuiteRoom = new JRadioButton("2. Suite Room Rp 300.000/hari");
            JRadioButton rPresidentRoom = new JRadioButton("3. President Room Rp 370.000/hari");
                  
            ButtonGroup btnGrup1=new ButtonGroup();
            JRadioButton rMember = new JRadioButton("Member");
            JRadioButton rNonMember = new JRadioButton("Non Member");
       
            JTextField tflama = new JTextField();
            JTextField tfTelp = new JTextField();

            JButton btsimpan = new JButton("SIMPAN");
            JButton bthapus = new JButton("HAPUS");
            JButton btexit = new JButton("KELUAR");
            JButton btedit = new JButton("EDIT");
            JButton btupdate = new JButton("UPDATE");
     
            DefaultTableModel model = new DefaultTableModel();
            JTable tampil = new JTable(model);
            JScrollPane pane = new JScrollPane(tampil);
          
            DefaultTableModel model1 = new DefaultTableModel();
            JTable tampil1 = new JTable(model1);
            JScrollPane pane1 = new JScrollPane(tampil1);
               
  public sewalosmen1()
            {
                super("sewalosmen1");
              
                setVisible(true);
                setSize(850,800);
                setLocationRelativeTo(null);
                setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                setLayout(null);
              
                lbljudul.setFont(new Font("Monotype Corsiva",Font.BOLD,25));
 lbljudul.setForeground(Color.blue);                       lbljudul.setBounds(250,10,300,25);
 lblIDPelanggan.setBounds(20,50,100,25);            tfIDPelanggan.setBounds(150,50,200,25);    btsimpan.setBounds(500,50,100,25);                    lblNama.setBounds(20,80,100,25);                   tfNama.setBounds(150,80,200,25);                      bthapus.setBounds(500,80,100,25                    lbllama.setBounds(20,110,120,25);                       tflama.setBounds(150,110,200,25);                   btupdate.setBounds(500,110,100,25);                  lblJenisKamar.setBounds(20,140,100,25);         tfTgl.setBounds(150,270,200,25);                        btedit.setBounds(500,140,100,25);                    lblMember.setBounds(20,210,100,25);                 tfAlamat.setBounds(150,300,200,25);                btexit.setBounds(500,170,100,25);                       lblTgl.setBounds(20,270,100,25);                      tfTelp.setBounds(150,330,200,25);                       lblTelp.setBounds(20,330,100,25);     
lblAlamat.setBounds(20,300,100,25);                   btsimpan.setBackground(new Color(102,255,102));  
btupdate.setBackground(new Color(102,255,102));
btsimpan.setForeground(Color.red);                        btupdate.setForeground(Color.red); pane.setBounds(20,380,650,500);                          btsimpan.setFont(new java.awt.Font("Chili",1,12));       btupdate.setFont(new java.awt.Font("Chili",1,12));  pane1.setBounds(670,380,170,500);           bthapus.setBackground(new Color(102,255,102));  btexit.setBackground(new Color(102,255,102));
bthapus.setForeground(Color.red);                           btexit.setForeground(Color.red);
bthapus.setFont(new java.awt.Font("Chili",1,12));    btexit.setFont(new java.awt.Font("Chili",1,12));
rDeluxeRoom.setBounds(150,140,220,25);             btedit.setBackground(new Color(102,255,102));
rSuiteRoom.setBounds(150,160,220,25);                btedit.setForeground(Color.red);
rPresidentRoom.setBounds(150,180,220,25);         btedit.setFont(new java.awt.Font("Chili",1,12));
rMember.setBounds(150,210,190,25);                   pane.setBackground(new Color(98,115,70));
rNonMember.setBounds(150,230,190,25);            pane1.setBackground(new Color(98,115,70));
                                                         
 add(lbljudul);                        add(pane);                   add(rDeluxeRoom);          add(pane1);  add(lblIDPelanggan);             add(tfIDPelanggan);     add(rSuiteRoom);             add(rNonMember);  add(lblNama);                       add(tfNama);               add(rPresidentRoom);       add(btexit);   
add(lbllama);                         add(tflama);                 add(rMember);                  add(btedit);
add(lblJenisKamar);              add(tfTgl);                   add(btsimpan);                   add(lblTelp);   
add(lblMember);                  add(tfAlamat);             add(bthapus);                     add(lblAlamat);      
add(lblTgl);                          add(tfTelp);                  add(btupdate);
                                                                                                              
                btsimpan.addActionListener(this);
                bthapus.addActionListener(this);
                btexit.addActionListener(this);
                btedit.addActionListener(this);
                btupdate.addActionListener(this);
    
                btnGrup.add(rDeluxeRoom);
                btnGrup.add(rSuiteRoom);
                btnGrup.add(rPresidentRoom);
                rDeluxeRoom.setSelected(true);

                btnGrup1.add(rMember);
                btnGrup1.add(rNonMember);
                rMember.setSelected(true);                 
     
                String header [] = {"ID_Pel", "Nama","JenKam","Lama", "Member","TglCekIn","Alamat","Telp"};
                for(int kolom =0; kolom



                {
                     model.addColumn(header[kolom]);
                }
                 String header1 [] = {"Tarif","Diskon","Bayar"};
                for(int kolom =0; kolom                {
                     model1.addColumn(header1[kolom]);
                }
              
            }
                @Override
            public void actionPerformed(ActionEvent act)
            {                          
            
            double lama=Double.parseDouble(tflama.getText());
            double tarif=0,diskon=0,bayar=0;
            Object obj = act.getSource();
          
        String dataID_Pel = tfIDPelanggan.getText();
        String dataNama= tfNama.getText();
        String dataLama= tflama.getText();
        String dataTglCekin =tfTgl.getText();
        String dataAlamat=tfAlamat.getText();
        String dataTelp=tfTelp.getText();
        String []dataJenKam={"rDeluxeRoom","rSuiteRoom","rPresidentRoom"};
        String []dataMember={"rMember","rNonMember"};
       
            if (obj==btsimpan)
            {
                String JenKam="";
                String Member="";
                {

           if (rDeluxeRoom.isSelected())
                    {
                        tarif = lama*275000;
                        JenKam="Deluxe Room";
                    } 
                    else if (rSuiteRoom.isSelected())
                    {
                        tarif = lama*300000;
                        JenKam="Suite Room";
                    }
                        else
                        {
                        tarif = lama*375000;
                        JenKam="President Room";
                    }

           if (rMember.isSelected())
           {
                    Member = " Member";
                    if(lama>=4 && lama<=7)
                    {
                      diskon = 0.20 * tarif;
                    }
                    else if (lama>7)
                    {
                        diskon = 0.35 * tarif;
                    }
                }
           else if
                (rNonMember.isSelected())
                    {
                        Member = "Non Member";
                            if (lama>=4 && lama<=7)
                            {
                                diskon = 0.10 * tarif;
                            }
                            else if (lama>7)
                            {
                               diskon = 0.15 * tarif;
                            }
           }

               bayar = tarif-diskon;
                String [][] data = {{dataID_Pel,dataNama,JenKam,dataLama,Member,dataTglCekin,dataAlamat,dataTelp}};
                Double [][] isSelected ={{tarif,diskon,bayar}};
               for(int isi=0; isi            {
                model.addRow(data[isi]);
            }
        
            for(int isi=0; isi            {
                model1.addRow(isSelected[isi]);
            }
              
             PreparedStatement ps = null;
             PreparedStatement ps1 = null;
            try
            {
                 Class.forName("org.sqlite.JDBC");
            Connection koneksi = DriverManager.getConnection("JDBC:sqlite:produk.db");
            ps = koneksi.prepareStatement("insert into sewalos(ID_Pel,Nama,JenKam,Lama,Member,TglCekIn,Alamat,Telp) values (?,?,?,?,?,?,?,?)");
            ps.setString(1,dataID_Pel);
            ps.setString(2,dataNama);
            ps.setString(3,JenKam);
            ps.setString(4,dataLama);
            ps.setString(5,Member);
            ps.setString(6,dataTglCekin);
            ps.setString(7,dataAlamat);
            ps.setString(8,dataTelp);
         
            ps.executeUpdate();
          
            ps1 = koneksi.prepareStatement("insert into sewalosmen(Tarif,Diskon,Bayar) values (?,?,?)");
            ps1.setDouble(1,tarif);
            ps1.setDouble(2,diskon);
            ps1.setDouble(3,bayar);
                     
            ps1.executeUpdate();
          
            JOptionPane.showMessageDialog(null,"Data Telah Tersimpan");
          
            int row=model.getRowCount ();
                for (int i = 0; i < row; i++)
                {
                    model.removeRow(0);
                }
            int row1=model1.getRowCount ();
                for (int i = 0; i < row; i++)
                {
                    model1.removeRow(0);
                }
              
            Statement stat = koneksi.createStatement ();
            ResultSet set = stat.executeQuery("select * from sewalos");
                     
          
                while(set.next())
                { model.addRow(new Object [] {set.getString("ID_Pel"),set.getString("Nama"),set.getString("JenKam"),set.getString("Lama"),
                    set.getString("Member"),set.getString("TglCekIn"),set.getString("Alamat"),set.getString("Telp")});
                    tampil.setModel(model);
                }
               tfIDPelanggan.setText("");
               tfNama.setText("");
               tflama.setText("");
               tfTgl.setText("");
               tfAlamat.setText("");
               tfTelp.setText("");
             
               ResultSet set1 = stat.executeQuery("select * from sewalosmen");
          
                while(set1.next())
                { model1.addRow(new Object [] {set1.getDouble("Tarif"),set1.getDouble("Diskon"),set1.getDouble("Bayar")});
                    tampil1.setModel(model1);
                }
             
             
            }catch(Exception ex)
            {
                System.err.println("Error...!SIMPAN!..");
            }
        }
              
            if(obj == bthapus)
        {
            tfIDPelanggan.setText("");
            tfNama.setText("");
            tflama.setText("");
            tfTgl.setText("");
            tfAlamat.setText("");
            tfTelp.setText("");
            tfIDPelanggan.requestFocus();
        }
      
        else if (obj == btedit)
        {
            int baris = tampil.getSelectedRow();
          
            String kolomID_Pelanggan = model.getValueAt(baris, 0).toString();
            tfIDPelanggan.setText(kolomID_Pelanggan);
          
            String kolomNama = model.getValueAt(baris, 1).toString();
            tfNama.setText(kolomNama);
          
            String kolomLama_Menginap = model.getValueAt(baris, 2).toString();
            tflama.setText(kolomLama_Menginap);
          
            String kolomAlamat = model.getValueAt(baris, 3).toString();
            tfAlamat.setText(kolomAlamat); 
            String kolomTelp = model.getValueAt(baris, 4).toString();
            tfTelp.setText(kolomTelp); 
        }
        else if(obj == bthapus)
        {
            tfIDPelanggan.setText("");
            tfNama.setText("");
            tflama.setText("");
            tfTgl.setText("");
            tfAlamat.setText("");
            tfTelp.setText("");
            tfIDPelanggan.requestFocus();
        }
       
        else if (obj == btupdate)
        {
            //model.insertRow(0, new Object[] {dataNAMA,dataNPM,dataALAMAT,dataKET});
         
        }
       
        else if (obj == btexit)
        {
            System.exit(0);
        }
      
     
     }
  }

  public static void main (String [] form)
    {
            sewalosmen1 sewalosmen1 = new sewalosmen1();
    }
}

output:






0 komentar :