Aplikasi Input Data Siswa SMA (Sederhana)

Assalamualaikum..
Hallo guys,kali ini gua akan upload hasil karya gua tentang Aplikasi Input Data Siswa SMA yang masih sederhana banget. Kenapa masih sederhana?karena disini hanya input data umum yang biasanya dilakukan oleh guru/staff TU (kurang lebih seperti itu gambaran tentang aplikasi ini hehe)
Oke langsung aja ke deskripsi nya guys..

Pertama disini gua buat form login yang biasanya dipakai untuk masuk ke suatu aplikasi,bisa dicek aja desain login nya seperti dibawah ini 



JFrame login ini membutuhkan database guys,jadi disini gua tidak membahas tentang database (pembaca dianggap sudah mengerti hehe)

Ini source code untuk JFrame Login nya

=============================================
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.JOptionPane;

public class Login extends javax.swing.JFrame {
    private Connection koneksi;
    private KoneksiDB db = new KoneksiDB();

    /**
     * Creates new form Login
     */
    public Login() {
        initComponents();
    }

    /**
     * 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() {

        jPanel1 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        txtUsername = new javax.swing.JTextField();
        txtPassword = new javax.swing.JPasswordField();
        btnLogin = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jPanel1.setBackground(new java.awt.Color(0, 204, 204));

        jLabel2.setText("USERNAME");

        jLabel3.setText("PASSWORD");

        btnLogin.setText("Login");
        btnLogin.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnLoginActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap(199, Short.MAX_VALUE)
                        .addComponent(btnLogin, javax.swing.GroupLayout.PREFERRED_SIZE, 67, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel2)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(txtUsername, javax.swing.GroupLayout.PREFERRED_SIZE, 138, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel3)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(txtPassword, javax.swing.GroupLayout.PREFERRED_SIZE, 138, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap(29, Short.MAX_VALUE)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txtUsername, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel2))
                        .addGap(27, 27, 27)
                        .addComponent(txtPassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(jLabel3)))
                .addGap(18, 18, 18)
                .addComponent(btnLogin, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(23, 23, 23))
        );

        jLabel1.setFont(new java.awt.Font("Perpetua Titling MT", 1, 18)); // NOI18N
        jLabel1.setText("SELAMAT DATANG");

        jLabel4.setText("Made by Mohammad Ramdan");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jLabel1)
                .addGap(99, 99, 99))
            .addGroup(layout.createSequentialGroup()
                .addGap(83, 83, 83)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(41, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addComponent(jLabel4)
                .addGap(0, 0, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 65, Short.MAX_VALUE)
                .addComponent(jLabel4))
        );

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

    private void btnLoginActionPerformed(java.awt.event.ActionEvent evt) {                                        
        // TODO add your handling code here:
        db.KoneksiData();
        try {
            String sql = "SELECT * FROM users WHERE username = '" + txtUsername.getText() + "' AND password = '" + txtPassword.getText() + "'";
            Statement st = db.getKoneksi().createStatement();
            ResultSet rsLogin = st.executeQuery(sql);

            rsLogin.next();
            rsLogin.last(); //mengecek jumlah baris pada hasil query
            if (rsLogin.getRow()==1){
                JOptionPane.showMessageDialog(null, "Login Berhasil!");
                // new FrmMenuUtama().setVisible(true); //<-- BILA MAU DIARAHKAN KE Frame Menu Utama
                this.dispose();
            } else {
                JOptionPane.showMessageDialog(null, "Maaf, Username / Password salah!");
                txtPassword.setText("");
                txtPassword.requestFocus();
            }
        } catch (SQLException e) {
        }
        FrmSiswa n = new FrmSiswa();
        n.setVisible(true);
        this.setVisible(false);

 ==============================================================

Nah JFrame Login ini membutuhkan koneksi,silahkan dibuat  classnya dengan nama koneksiDB dan ikuti source code dibawah ini :

=======================================================================
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import javax.swing.JOptionPane;

public class KoneksiDB {
    private Connection koneksi;
   
    public Connection getKoneksi(){
        return koneksi;
    }
   
    public void KoneksiData() { //<-- untuk koneksi ke database
        // Cek Driver
        try {
            Class.forName("com.mysql.jdbc.Driver"); //<-- nama driver untuk koneksi ke MySQL

            // Cek Database
            try {
                String url, user, password;
                url = "jdbc:mysql://localhost:3306/db_login"; //alamat DB
                user = "root";
                password = "";
                koneksi = DriverManager.getConnection(url, user, password);

                System.out.println("Koneksi Sukses");
            } catch (SQLException se) {
                JOptionPane.showMessageDialog(null, "Koneksi Gagal! " + se);
                System.exit(0);
            }
        } catch (ClassNotFoundException cnfe) {
            JOptionPane.showMessageDialog(null, "Driver Tidak Ditemukan!" +cnfe);
            System.exit(0);
        }
    }
    public static void main(String[] kon) {
        new KoneksiDB().KoneksiData();
    }
   
}

=========================================================================

Jikas sudah,buat lah JFrame baru dengan nama FrmSiswa lalu desain seperti  contoh dibawah ini



Kemudian input source codenya seperti dibawah ini :

=========================================================================
import java.sql.*;
import javax.swing.*;;
import javax.swing.table.DefaultTableModel;

public class FrmSiswa extends javax.swing.JFrame {


    private Connection koneksi;
   
    public FrmSiswa() {
        initComponents();
        KoneksiDatabase();
        TampilData();
    }

private void btnSimpanActionPerformed(java.awt.event.ActionEvent evt) {                                          
        // TODO add your handling code here:
        String nisn = txtNISN.getText();
        String nama = txtNama.getText();
        String jeniskelamin = null;
        if (rblaki.isSelected()) {
            jeniskelamin = "Laki-Laki";
        } else if (rbperempuan.isSelected()) {
            jeniskelamin = "Perempuan";
        }
        String jurusan = (String) cmbJur.getSelectedItem();
        String alamat = txtAlamat.getText();

        if (txtNISN.getText().trim().equals("")) {
            JOptionPane.showMessageDialog(null, "Mohon Masukkan Data untuk di Input", "PERHATIAN", JOptionPane.WARNING_MESSAGE);
        } else {
            try {
                java.sql.Statement stat = koneksi.createStatement();
                ResultSet data = stat.executeQuery("SELECT * FROM tablesiswa WHERE nisn ='" + txtNISN.getText() + "'");
                if (data.next()) {
                    JOptionPane.showMessageDialog(null, "NISN Sudah Ada", "PERHATIAN", JOptionPane.WARNING_MESSAGE);
                    txtNISN.requestFocus();
                } else {                  
                    String sql = "INSERT INTO tablesiswa VALUES('" + nisn + "'"
                    + ",'"+nama+"'"
                    + ",'"+jurusan+"'"
                    + ",'"+jeniskelamin+"'"
                    + ",'"+alamat+"')";
                    stat.executeUpdate(sql);
                    txtNISN.setText("");
                    txtNama.setText("");
                    cmbJur.setSelectedIndex(-0);
                    txtAlamat.setText("");
                    txtNISN.requestFocus();
                    JOptionPane.showMessageDialog(null, "Data Berhasil Di Simpan", "SUKSES", JOptionPane.INFORMATION_MESSAGE);
                    TampilData();
                   
                }
                stat.close();
               
            } catch (Exception exc) {
                System.err.println("Terjadi Kesalahan :" + exc);
            }
           
        }
    }                   

private void btnHapusActionPerformed(java.awt.event.ActionEvent evt) {                                        
        // TODO add your handling code here:
        txtNISN.setText("");
        txtNama.setText("");
        cmbJur.setSelectedIndex(-0);
        txtAlamat.setText("");
        txtNISN.requestFocus();

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

private void tabsiswaMouseClicked(java.awt.event.MouseEvent evt) {                                     
        // TODO add your handling code here:
        int baris = tabsiswa.getSelectedRow();
        if (baris != -1) {
        txtNISN.setText(tabsiswa.getValueAt(baris, 0).toString());
        txtNama.setText(tabsiswa.getValueAt(baris, 1).toString());
    if ("Laki-Laki".equals(tabsiswa.getValueAt(baris, 2).toString())) {
        rblaki.setSelected(true);
    } else {
        rbperempuan.setSelected(true);
        }
    }
    cmbJur.setSelectedItem(tabsiswa.getValueAt(baris, 3).toString());
    txtAlamat.setText(tabsiswa.getValueAt(baris, 4).toString());
    }                        

 private void KoneksiDatabase() {
        try {
        Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException cnf) {
        }
        try {
        koneksi = DriverManager.getConnection("jdbc:mysql://localhost:3306/dbsiswa", "root", "");
        JOptionPane.showMessageDialog(null, "Koneksi Database Berhasil");
        } catch (SQLException se) {
        JOptionPane.showMessageDialog(null, "Koneksi Database Tidak Berhasil");
        } catch (Exception e) {
        } //To change body of generated methods, choose Tools | Templates.
    }

    private void TampilData() {
        //membuat tampilan model tabel
        DefaultTableModel model = new DefaultTableModel();
        model.addColumn("NISN");
        model.addColumn("Nama");
        model.addColumn("Jenis Kelamin");
        model.addColumn("Jurusan");
        model.addColumn("Alamat");
        tabsiswa.setModel(model);

        //menampilkan data database kedalam tabel
        try {
        java.sql.Statement stat = koneksi.createStatement();
        ResultSet data = stat.executeQuery("SELECT * FROM tablesiswa");
        while (data.next()) {
         model.addRow(new Object[]{
            data.getString("nisn"),
            data.getString("nama"),
            data.getString("jenis kelamin"),
            data.getString("jurusan"),
            data.getString("alamat")
         });
         tabsiswa.setModel(model);
        }
            } catch (Exception e) {
            System.err.println("Terjadi Kesalahan :" + e);
            }
        //To change body of generated methods, choose Tools | Templates.
    }
}

=========================================================================

Nah seperti itu kira kira untuk codingnya,berikut gua tampilkan hasil dari aplikasi yang sudah selesai dijalanka beserta flowchartnya





Kira kira segitu aja dulu yang dapat gua post kali ini,mohon maaf apabila banyak kesalahan sana sini. Silahkan tinggalkan komentar untuk kritik dan saran
Terimakasih,wassalamulaikum
See u guys ;D











Komentar