Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package form;
- import com.mysql.jdbc.Statement;
- import java.awt.HeadlessException;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import javax.swing.JOptionPane;
- import koneksi.Koneksi;
- import javax.swing.table.DefaultTableModel;
- /**
- *
- * @author RIDHWAN
- */
- public class FormMahasiswa extends javax.swing.JFrame {
- /**
- * Creates new form FormMahasiswa
- */
- public FormMahasiswa() {
- initComponents();
- datatable();
- }
- public void datatable() {
- DefaultTableModel tbl = new DefaultTableModel();
- tbl.addColumn("NPM");
- tbl.addColumn("Nama");
- tbl.addColumn("Jurusan");
- tbl.addColumn("Jenis Kelamin");
- tbl.addColumn("Alamat");
- tabel.setModel(tbl);
- try {
- Statement statement = (Statement) Koneksi.GetConnection().createStatement();
- ResultSet res = statement.executeQuery("select * from tb_mhswa");
- while(res.next()) {
- tbl.addRow(new Object[] {
- res.getString("npm"),
- res.getString("nama"),
- res.getString("jurusan"),
- res.getString("jeniskelamin"),
- res.getString("alamat")
- });
- tabel.setModel(tbl);
- }
- } catch (Exception ex) {
- JOptionPane.showMessageDialog(rootPane, "Salah");
- }
- }
- /**
- * 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() {
- jeniskelaminGB = new javax.swing.ButtonGroup();
- jLabel1 = new javax.swing.JLabel();
- jLabel2 = new javax.swing.JLabel();
- jLabel3 = new javax.swing.JLabel();
- jLabel4 = new javax.swing.JLabel();
- jLabel5 = new javax.swing.JLabel();
- jLabel6 = new javax.swing.JLabel();
- jLabel7 = new javax.swing.JLabel();
- npmTF = new javax.swing.JTextField();
- namamhsTF = new javax.swing.JTextField();
- jurusanCB = new javax.swing.JComboBox<>();
- alamatmhsTF = new javax.swing.JTextField();
- jRadioButton1 = new javax.swing.JRadioButton();
- jRadioButton2 = new javax.swing.JRadioButton();
- tambahBT = new javax.swing.JButton();
- simpanBT = new javax.swing.JButton();
- cariBT = new javax.swing.JButton();
- hapusBT = new javax.swing.JButton();
- keluarBT = new javax.swing.JButton();
- jScrollPane1 = new javax.swing.JScrollPane();
- tabel = new javax.swing.JTable();
- setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
- jLabel1.setFont(new java.awt.Font("Arial", 1, 18)); // NOI18N
- jLabel1.setText("DATA MAHASISWA");
- jLabel2.setFont(new java.awt.Font("Arial", 1, 12)); // NOI18N
- jLabel2.setText("NPM");
- jLabel3.setFont(new java.awt.Font("Arial", 1, 12)); // NOI18N
- jLabel3.setText("Nama Mahasiswa");
- jLabel4.setFont(new java.awt.Font("Arial", 1, 12)); // NOI18N
- jLabel4.setText("Jurusan");
- jLabel5.setFont(new java.awt.Font("Arial", 1, 12)); // NOI18N
- jLabel5.setText("Jenis Kelamin");
- jLabel6.setFont(new java.awt.Font("Arial", 1, 12)); // NOI18N
- jLabel6.setText("Alamat");
- jLabel7.setFont(new java.awt.Font("Arial", 1, 12)); // NOI18N
- jLabel7.setText("Proses");
- jurusanCB.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "Sistem Informasi", "Teknik Informatika" }));
- jeniskelaminGB.add(jRadioButton1);
- jRadioButton1.setText("Laki - Laki");
- jeniskelaminGB.add(jRadioButton2);
- jRadioButton2.setText("Wanita");
- tambahBT.setText("TAMBAH");
- tambahBT.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- tambahBTActionPerformed(evt);
- }
- });
- simpanBT.setText("SIMPAN");
- simpanBT.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- simpanBTActionPerformed(evt);
- }
- });
- cariBT.setText("CARI");
- cariBT.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- cariBTActionPerformed(evt);
- }
- });
- hapusBT.setText("HAPUS");
- hapusBT.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- hapusBTActionPerformed(evt);
- }
- });
- keluarBT.setText("KELUAR");
- keluarBT.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- keluarBTActionPerformed(evt);
- }
- });
- tabel.setModel(new javax.swing.table.DefaultTableModel(
- new Object [][] {
- },
- new String [] {
- "NPM", "Nama", "Jurusan", "Jenis Kelamin", "Alamant"
- }
- ));
- jScrollPane1.setViewportView(tabel);
- javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
- getContentPane().setLayout(layout);
- layout.setHorizontalGroup(
- layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addGap(38, 38, 38)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addComponent(jLabel2)
- .addComponent(jLabel4)
- .addComponent(jLabel3)
- .addComponent(jLabel5)
- .addComponent(jLabel6))
- .addGap(39, 39, 39)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addComponent(jLabel1)
- .addComponent(npmTF, javax.swing.GroupLayout.PREFERRED_SIZE, 65, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addComponent(namamhsTF, javax.swing.GroupLayout.PREFERRED_SIZE, 200, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addComponent(jurusanCB, javax.swing.GroupLayout.PREFERRED_SIZE, 132, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addComponent(jRadioButton1)
- .addComponent(jRadioButton2)
- .addComponent(alamatmhsTF, javax.swing.GroupLayout.PREFERRED_SIZE, 200, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addComponent(jLabel7)))
- .addGroup(layout.createSequentialGroup()
- .addComponent(tambahBT)
- .addGap(18, 18, 18)
- .addComponent(simpanBT)
- .addGap(18, 18, 18)
- .addComponent(cariBT)
- .addGap(18, 18, 18)
- .addComponent(hapusBT)
- .addGap(18, 18, 18)
- .addComponent(keluarBT))))
- .addGroup(layout.createSequentialGroup()
- .addGap(13, 13, 13)
- .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
- .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
- );
- layout.setVerticalGroup(
- layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addContainerGap()
- .addComponent(jLabel1)
- .addGap(26, 26, 26)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
- .addComponent(jLabel2)
- .addComponent(npmTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
- .addGap(18, 18, 18)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
- .addComponent(jLabel3)
- .addComponent(namamhsTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
- .addGap(18, 18, 18)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
- .addComponent(jLabel4)
- .addComponent(jurusanCB, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
- .addGap(18, 18, 18)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
- .addComponent(jLabel5)
- .addComponent(jRadioButton1))
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
- .addComponent(jRadioButton2)
- .addGap(18, 18, 18)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
- .addComponent(alamatmhsTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addComponent(jLabel6))
- .addGap(18, 18, 18)
- .addComponent(jLabel7)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
- .addComponent(tambahBT)
- .addComponent(simpanBT)
- .addComponent(cariBT)
- .addComponent(hapusBT)
- .addComponent(keluarBT))
- .addGap(18, 18, 18)
- .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 145, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
- );
- pack();
- }// </editor-fold>
- private void tambahBTActionPerformed(java.awt.event.ActionEvent evt) {
- // TODO add your handling code here:
- npmTF.setText("");
- namamhsTF.setText("");
- jurusanCB.setSelectedItem("");
- if(jRadioButton1.isSelected()) {
- JOptionPane.showMessageDialog(rootPane, "laki - laki");
- } else if (jRadioButton2.isSelected()) {
- JOptionPane.showMessageDialog(rootPane, "wanita");
- alamatmhsTF.setText("");
- }
- }
- private void simpanBTActionPerformed(java.awt.event.ActionEvent evt) {
- // TODO add your handling code here:
- String npm = npmTF.getText();
- String nama = namamhsTF.getText();
- String jurusan = (String) jurusanCB.getSelectedItem();
- String alamat = alamatmhsTF.getText();
- String jeniskelamin = null;
- if(jRadioButton1.isSelected()) {
- jeniskelamin = "Laki - Laki";
- } else if(jRadioButton2.isSelected()) {
- jeniskelamin = "Wanita";
- }
- try {
- Statement statement = (Statement) Koneksi.GetConnection().createStatement();
- statement.executeUpdate("insert into tb_mhswa VALUES ('"+npm+"','"+nama+"','"+jurusan+"', '"+jeniskelamin+"', '"+alamat+"');");
- statement.close();
- JOptionPane.showMessageDialog(null, "Data Berhasil Disimpan");
- } catch (Exception ex) {
- JOptionPane.showMessageDialog(null, "Data Gagal Disimpan, "+ ex);
- }
- datatable();
- }
- private void hapusBTActionPerformed(java.awt.event.ActionEvent evt) {
- // TODO add your handling code here:
- String npm = npmTF.getText();
- try {
- Statement statement = (Statement) Koneksi.GetConnection().createStatement();
- statement.executeUpdate("DELETE from tb_mhswa where npm = ('"+npm+"');");
- JOptionPane.showMessageDialog(null, "Data Berhasil Dihapus");
- npmTF.setText("");
- namamhsTF.setText("");
- jurusanCB.setSelectedItem("");
- String jeniskelamin = null;
- alamatmhsTF.setText("");
- npmTF.requestFocus();
- } catch (Exception ex) {
- JOptionPane.showMessageDialog(null, "Data Gagal Dihapus, " + ex);
- }
- }
- private void keluarBTActionPerformed(java.awt.event.ActionEvent evt) {
- // TODO add your handling code here:
- dispose();
- }
- private void cariBTActionPerformed(java.awt.event.ActionEvent evt) {
- // TODO add your handling code here:
- try {
- Statement statement = (Statement) Koneksi.GetConnection().createStatement();
- ResultSet res = statement.executeQuery("select * from tb_mhswa where " + "npm='" + npmTF.getText() + "'");
- DefaultTableModel tbl = new DefaultTableModel();
- tbl.addColumn("NPM");
- tbl.addColumn("Nama");
- tbl.addColumn("Jurusan");
- tabel.setModel(tbl);
- while (res.next()) {
- tbl.addRow(new Object[] {
- res.getString("npm"),
- res.getString("nama"),
- res.getString("jurusan")
- });
- tabel.setModel(tbl);
- }
- } catch (Exception ex) {
- JOptionPane.showMessageDialog(rootPane, "Salah");
- }
- }
- /**
- * @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(FormMahasiswa.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (InstantiationException ex) {
- java.util.logging.Logger.getLogger(FormMahasiswa.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (IllegalAccessException ex) {
- java.util.logging.Logger.getLogger(FormMahasiswa.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (javax.swing.UnsupportedLookAndFeelException ex) {
- java.util.logging.Logger.getLogger(FormMahasiswa.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() {
- new FormMahasiswa().setVisible(true);
- }
- });
- }
- // Variables declaration - do not modify
- private javax.swing.JTextField alamatmhsTF;
- private javax.swing.JButton cariBT;
- private javax.swing.JButton hapusBT;
- 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.JLabel jLabel7;
- private javax.swing.JRadioButton jRadioButton1;
- private javax.swing.JRadioButton jRadioButton2;
- private javax.swing.JScrollPane jScrollPane1;
- private javax.swing.ButtonGroup jeniskelaminGB;
- private javax.swing.JComboBox<String> jurusanCB;
- private javax.swing.JButton keluarBT;
- private javax.swing.JTextField namamhsTF;
- private javax.swing.JTextField npmTF;
- private javax.swing.JButton simpanBT;
- private javax.swing.JTable tabel;
- private javax.swing.JButton tambahBT;
- // End of variables declaration
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement