package Sistem_Informasi_Penjualan_Motor;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
public class Data_Kendaraan extends javax.swing.JFrame {
    private DefaultTableModel tabmode;
    public
Connection conn;
    public ResultSet
rs;
    public
PreparedStatement pst;
public Data_Kendaraan() throws SQLException {
       
initComponents();
        conn = null;
        conn =
DriverManager.getConnection("jdbc:mysql://localhost/penjualanmotor","root","");
        UpdateTabel();
        setLocationRelativeTo(null);
    }
private void UpdateTabel() {
        try {
            String sql
= "SELECT * FROM tb_kendaraan;";
            pst =
conn.prepareStatement(sql);
            rs =
pst.executeQuery();
           
DefaultTableModel dtm = (DefaultTableModel)tabel_data.getModel();
       
dtm.setRowCount(0);
        String [] data
= new String[5];
        int i = 1;
       
while(rs.next()) {
            data[0] =
rs.getString("kode_kendaraan");
            data[1] =
rs.getString("merek_motor");
            data[2] =
rs.getString("penyedia");
            data[3] =
rs.getString("harga");
            data[4] =
rs.getString("tgl_input");
           
dtm.addRow(data);
            i++;
        }
        }
        catch
(Exception e) {
            JOptionPane.showMessageDialog(null, e);
        }
    }
private void hapus_text() {
           
txtkode.setText("");
           
txtmerek.setText("");
           
txtpenyedia.setText("");
           
txtharga.setText("");
           
txttanggal.setText("");
        }
private void
btninputActionPerformed(java.awt.event.ActionEvent evt) {
if(txtkode.getText().trim().equals("")){
           
JOptionPane.showMessageDialog(null,"Maaf, KODE belum di
isi!");
           
txtkode.requestFocus();
        }
        else
if(txtmerek.getText().trim().equals("")){
            JOptionPane.showMessageDialog(null,"Maaf,
MEREK MOTOR belum di isi!");
           
txtmerek.requestFocus();
        }
        else
if(txtpenyedia.getText().trim().equals("")){
           
JOptionPane.showMessageDialog(null,"Maaf, PENYEDIA belum di
isi!");
           
txtpenyedia.requestFocus();
        }
        else
if(txtharga.getText().trim().equals("")){
           
JOptionPane.showMessageDialog(null,"Maaf, HARGA belum di
isi!");
           
txtharga.requestFocus();
        }
        else if(txttanggal.getText().trim().equals("")){
           
JOptionPane.showMessageDialog(null,"Maaf, Tanggal belum di
isi!");
           
txttanggal.requestFocus();
        }
        else{
            String
kode = "", merek = "", penyedia = "",
harga="", tanggal="";
            try {
               
Connection con = new koneksi().getConnection();
                kode =
txtkode.getText();
                merek
= txtmerek.getText();
               
penyedia = txtpenyedia.getText();
                harga
= txtharga.getText();
               
tanggal = txttanggal.getText();
                String
sql = "insert into tb_kendaraan
values('"+kode+"','"+merek+"','"+penyedia+"','"+harga+"','"+tanggal+"')";
               
Statement st = con.createStatement();
                st.executeUpdate(sql);
               
JOptionPane.showMessageDialog(null, "Data BERHASIL tersimpan"
, "Informasi", JOptionPane.INFORMATION_MESSAGE);
               
UpdateTabel();
            } catch
(Exception e) {
               
JOptionPane.showMessageDialog(null, "Data GAGAL tersimpan" ,
"Informasi", JOptionPane.INFORMATION_MESSAGE);
            }
        }
}
private void btndeleteActionPerformed(java.awt.event.ActionEvent
evt) {                                         
        // TODO add
your handling code here:
        String kodek =
txtkode.getText();
         try {
               
Connection con = new koneksi().getConnection();
                String
sql = ("delete from tb_kendaraan where
kode_kendaraan=('"+kodek+"');");
               
Statement st = con.createStatement();
               
st.executeUpdate(sql);
               
JOptionPane.showMessageDialog(null, "Data BERHASIL DiHapus" ,
"Informasi", JOptionPane.INFORMATION_MESSAGE);
               
hapus_text();
               
UpdateTabel();
            } catch
(Exception e) {
               
JOptionPane.showMessageDialog(null, "Data GAGAL DiHapus" ,
"Informasi", JOptionPane.INFORMATION_MESSAGE);
            }
    }
private void
tabel_dataMouseClicked(java.awt.event.MouseEvent evt) {                                        
        // TODO add
your handling code here:
        int row =
tabel_data.getSelectedRow();
           
txtkode.setText((String)tabel_data.getValueAt(row, 0));
           
txtmerek.setText((String)tabel_data.getValueAt(row, 1));
           
txtpenyedia.setText((String)tabel_data.getValueAt(row, 2));
           
txtharga.setText((String)tabel_data.getValueAt(row, 3));
           
txttanggal.setText((String)tabel_data.getValueAt(row, 4));
    }  
public static void main(String args[]) {
        /* Set the
Nimbus look and feel */
       
//<editor-fold defaultstate="collapsed" desc=" Look
and feel setting code (optional) ">
        /* If Nimbus
(introduced in Java SE 6) is not available, stay with the default look and
feel.
         * For details
see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for
(javax.swing.UIManager.LookAndFeelInfo info :
javax.swing.UIManager.getInstalledLookAndFeels()) {
                if
("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                   
break;
                }
            }
        } catch
(ClassNotFoundException ex) {
           
java.util.logging.Logger.getLogger(Data_Kendaraan.class.getName()).log(java.util.logging.Level.SEVERE,
null, ex);
        } catch
(InstantiationException ex) {
           
java.util.logging.Logger.getLogger(Data_Kendaraan.class.getName()).log(java.util.logging.Level.SEVERE,
null, ex);
        } catch
(IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Data_Kendaraan.class.getName()).log(java.util.logging.Level.SEVERE,
null, ex);
        } catch
(javax.swing.UnsupportedLookAndFeelException ex) {
           
java.util.logging.Logger.getLogger(Data_Kendaraan.class.getName()).log(java.util.logging.Level.SEVERE,
null, ex);
        }
       
//</editor-fold>
        /* Create and
display the form */
       
java.awt.EventQueue.invokeLater(new Runnable() {
            public
void run() {
                try {
                   
new Data_Kendaraan().setVisible(true);
                }
catch (SQLException ex) {
                   
Logger.getLogger(Data_Kendaraan.class.getName()).log(Level.SEVERE, null,
ex);
                }
            }
        });
    }
    // Variables
declaration - do not modify                    
    private
javax.swing.JButton btndelete;
    private
javax.swing.JButton btnedit;
    private
javax.swing.JButton btninput;
    private
javax.swing.JButton btnreset;
    private javax.swing.JLabel
jLabel1;
    private
javax.swing.JLabel jLabel2;
    private
javax.swing.JLabel jLabel3;
    private
javax.swing.JLabel jLabel4;
    private
javax.swing.JLabel jLabel5;
    private
javax.swing.JLabel jLabel6;
    private
javax.swing.JPanel jPanel1;
    private
javax.swing.JPanel jPanel2;
    private
javax.swing.JScrollPane jScrollPane1;
    private
javax.swing.JTable tabel_data;
    private
javax.swing.JTextField txtharga;
    private
javax.swing.JTextField txtkode;
    private
javax.swing.JTextField txtmerek;
    private
javax.swing.JTextField txtpenyedia;
    private
javax.swing.JTextField txttanggal;
    // End of
variables declaration                   
}
No comments:
Post a Comment