Rabu, 30 Oktober 2013

tugas ku yang ke-2 pbo

program entry siswa dengan menggunakan java"

sebelum mengetikan program di netbeans terlebih dahulu kita buat databasenya dulu di xampp,dengan nama database mahasiswa dan nama tabelnya siswa.
Isi dari nama tabel siswa yaitu;
-Npm(int)(9)(primery key)
-Nama(varchar)(30)
-Jurusan(varchar)(20)
-Telpon(varchar)(13)
-Kelas(varchar)(3)
-Mata Kuliah(varchar)(20)

Dalam pembuatan data di netbeans yaitu;
-untuk mengisi tombol button klik 2x isi kan syintaknya ada bagian dibawah sesuai dengan petunjuk button(simpan,edit,hapus dan keluar)
-untuk mengisi label,textfield,tabel dan combobox sudah ada di sediakan di netbeans sebelah kanan layar.

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package prjakademik;
import java.awt.HeadlessException;
import java.sql.*;
import javax.swing.*;
import javax.swing.table.*;
/**
 *
 * @author Rui
 */
public final class mahasiswa extends javax.swing.JFrame {
   public Connection conn;
   public Statement cn;
    /**
     * Creates new form mahasiswa
     */
    public mahasiswa() {
        initComponents();
        bersih();
        tampildata();
        jComboBox1.addItem("-- PILIH --");
        jComboBox1.addItem("Sistem Informasi");
        jComboBox1.addItem("Teknik Informatika");
        jComboBox1.addItem("Manajemen Informatika");
       
    }

    public void koneksi(){
        try{
            Class.forName("com.mysql.jdbc.Driver");
            conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/dbmahasiswa","root","");
            cn=conn.createStatement();
        } catch(ClassNotFoundException | SQLException e) {
            JOptionPane.showMessageDialog(null, "koneksi gagal....");
            System.out.println(e.getMessage());
        }
    }
   
    public void bersih(){
        jTextField1.setText("");
        jTextField2.setText("");
        jTextField3.setText("");
        jComboBox1.setSelectedItem("-- PILIH --");
    }
   
    public void tampildata(){
        DefaultTableModel tblmhs = new DefaultTableModel();
        tblmhs.addColumn("Npm");
        tblmhs.addColumn("Nama");
        tblmhs.addColumn("Kelas");
        tblmhs.addColumn("Jurusan");
        tblmhs.addColumn("Telp");
       
        try{
            koneksi();
            String sql="Select * from siswa order by npm asc";
            ResultSet rs=cn.executeQuery(sql);
            while (rs.next())
            {
                tblmhs.addRow(new Object[]{
                rs.getString(1),
                rs.getString(2),
                rs.getString(3),
                rs.getString(4),
                rs.getString(5)
            });
            }
            jTable1.setModel(tblmhs);
        } catch (Exception e) {
          JOptionPane.showMessageDialog(null,"Ada Kesalahan...."); 
        }
    }
   
    public void carimhs(){
        try{
            koneksi();
            String sql="Select * from siswa where npm='"+jTextField1.getText()+"'";
            ResultSet rs=cn.executeQuery(sql);
           
            if(rs.next())
            {
                jTextField1.setText(rs.getString(1));
                jTextField2.setText(rs.getString(2));
                jComboBox1.setSelectedItem(rs.getString(3));
                jTextField3.setText(rs.getString(4));
                JOptionPane.showMessageDialog(null,"Data Mahasiswa: "+jTextField1.getText()+"sudah ada..!!!");
                bersih();
            }
        } catch(Exception e){}
            jTextField2.requestFocus();
       
    }
   
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jTextField2 = new javax.swing.JTextField();
        jComboBox1 = new javax.swing.JComboBox();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();
        jTextField3 = new javax.swing.JTextField();
        jButton3 = new javax.swing.JButton();
        jLabel6 = new javax.swing.JLabel();
        jButton5 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setText("NPM");

        jLabel2.setText("Nama");

        jLabel3.setText("Jurusan");

        jTextField1.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jTextField1KeyPressed(evt);
            }
        });

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                true, true, true, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jTable1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTable1MouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(jTable1);

        jButton1.setText("Simpan");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("Edit");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jLabel4.setText("Telp");

        jButton3.setText("Hapus");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });



        jLabel6.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        jLabel6.setText("Entry Data Siswa");

        jButton5.setText("Keluar");
        jButton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton5ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(145, 145, 145)
                                .addComponent(jLabel6))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(32, 32, 32)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(jLabel1)
                                            .addComponent(jLabel2)
                                            .addComponent(jLabel3)
                                            .addComponent(jLabel4))
                                        .addGap(36, 36, 36)
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, 126, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, 179, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, 179, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 127, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(jButton1)
                                        .addGap(29, 29, 29)
                                        .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 67, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGap(31, 31, 31)
                                        .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 67, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGap(32, 32, 32)
                                        .addComponent(jButton5)))))
                        .addGap(0, 38, Short.MAX_VALUE))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel6)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(11, 11, 11)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addGap(10, 10, 10)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel4)
                    .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(16, 16, 16)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jButton1)
                            .addComponent(jButton5)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jButton2)
                            .addComponent(jButton3))))
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 101, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(22, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                        
        // TODO add your handling code here:
        // button simpan
        try{
            koneksi();
            String sql="Insert into siswa values('"+jTextField1.getText()+"','"+jTextField2.getText()+"','"+jComboBox1.getSelectedItem()+"','"+jTextField3.getText()+"')";
            cn.executeUpdate(sql);
            conn.close();
            tampildata();
            bersih();
            JOptionPane.showMessageDialog(null, "Data berhasil di simpan");
        } catch (SQLException | HeadlessException e) {
            JOptionPane.showMessageDialog(null, "Proses penyimpanan gagal/koneksi gagal..");
            System.out.println(e.getMessage());
            JOptionPane.showMessageDialog(null,"No Npm : "+jTextField1.getText()+" sudah ada...!!!");
            System.out.println(e.getMessage());
            bersih();
        }
        jComboBox1.setSelectedItem("-- PILIH --");
    }                                       

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                        
        // TODO add your handling code here:
        // button edit
        try{
           
            koneksi();
            String sql="Update siswa set nama='"+jTextField2.getText()+"',"+"jurusan='"+jComboBox1.getSelectedItem()+"',"+"Kelas='"+"',"+"telp='"+jTextField3.getText()+"'"+"where npm='"+jTextField1.getText()+"'";
            cn.executeUpdate(sql);
            conn.close();
            tampildata();
            bersih();
            JOptionPane.showMessageDialog(null,"Data berhasil di rubah");
           
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null,"Proses update gagal/koneksi gagal...");
            System.out.println(e.getMessage());
        }
        jTextField1.setEnabled(true);
        jButton1.setEnabled(true);
        jComboBox1.setSelectedItem("-- PILIH --");
    }                                       

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {                                        
        // TODO add your handling code here:
        // button hapus
        try{
           
            koneksi();
            String sql="delete from siswa where npm='"+jTextField1.getText()+"'";
            cn.executeUpdate(sql);
            conn.close();
            tampildata();
            bersih();
            JOptionPane.showMessageDialog(null,"Data berhasil di hapus");
           
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null,"Proses hapus gagal/koneksi gagal...");
            System.out.println(e.getMessage());
           
        }
        jTextField1.setEnabled(true);
        jButton1.setEnabled(true);
        jComboBox1.setSelectedItem("-- PILIH --");
    }                                       

    private void jTable1MouseClicked(java.awt.event.MouseEvent evt) {                                    
        // TODO add your handling code here:
        int table=jTable1.getSelectedRow();
        //mengambil data dari table
        String a=jTable1.getValueAt(table, 0).toString();
        String b=jTable1.getValueAt(table,1).toString();
        String c=jTable1.getValueAt(table,2).toString();
        String d=jTable1.getValueAt(table, 3).toString();
       
        jTextField1.setText(a);
        jTextField2.setText(b);
        jComboBox1.setSelectedItem(c);
        jTextField3.setText(d);
        jTextField1.setEnabled(false);
        jTextField2.requestFocus();
        jButton1.setEnabled(false);
    }                                   

    private void jTextField1KeyPressed(java.awt.event.KeyEvent evt) {                                      
        // TODO add your handling code here:
      
    }                                     

    private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {                                        
        // TODO add your handling code here:
        // button keluar
        System.exit(0);
    }                                       

           
    /**
     * @param args the command line arguments
     */
    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(mahasiswa.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(mahasiswa.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(mahasiswa.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(mahasiswa.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new mahasiswa().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton5;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JTextField jTextField3;
    // End of variables declaration

    private void clear(int i) {
        throw new UnsupportedOperationException("Not yet implemented");
    }

    private static class condisi {

        public condisi() {
        }
    }
}

Tidak ada komentar:

Posting Komentar