Tabelspp Nova

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 14

Nama : Nova gracella

Kelas : 3 PSI

package project_nova;

import java.sql.Connection;

import javax.swing.JOptionPane;

import javax.swing.table.DefaultTableModel;

public class frmspp extends javax.swing.JFrame {

public frmspp() {

initComponents();

load_table();

kosong();

private void load_table() {

// TODO add your handling code here:

// membuat tampilan model tabel

DefaultTableModel model = new DefaultTableModel();

model.addColumn("tahun masuk");

model.addColumn("prodi");

model.addColumn("spp");

model.addColumn("bpp");

model.addColumn("npm");

// menampilkan data database kedalam tabel

try {

String sql = "select * from tbspp";

java.sql.Connection conn=(Connection)koneksiDB.configDB();

java.sql.Statement stm=conn.createStatement();

java.sql.ResultSet res=stm.executeQuery(sql);
while(res.next()) {

model.addRow(new Object[]{res.getString(1),res.getString(2),res.getString(3),res.getString(4),res.getString(5)});

datagridspp.setModel(model);

} catch (Exception e) {

private void kosong()

cmbthnmasuk.setSelectedItem(this);

cmbprodi.setSelectedItem(this);

txtspp.setText(null);

txtbpp.setText(null);

txtnpm.setText(null);

@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();

cmbthnmasuk = new javax.swing.JComboBox<>();

cmbprodi = new javax.swing.JComboBox<>();

jLabel4 = new javax.swing.JLabel();

txtspp = new javax.swing.JTextField();

jLabel5 = new javax.swing.JLabel();

txtbpp = new javax.swing.JTextField();

jLabel6 = new javax.swing.JLabel();


txtnpm = new javax.swing.JTextField();

jScrollPane1 = new javax.swing.JScrollPane();

datagridspp = new javax.swing.JTable();

btntambah = new javax.swing.JButton();

btnhapus = new javax.swing.JButton();

btnubah = new javax.swing.JButton();

btncari = new javax.swing.JButton();

btnkeluar = new javax.swing.JButton();

btnsimpan = new javax.swing.JButton();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

jLabel1.setFont(new java.awt.Font("Segoe UI", 0, 14)); // NOI18N

jLabel1.setText("LAPORAN SPP MAHASISWA");

jLabel2.setText("Tahun Masuk");

jLabel3.setText("prodi");

cmbthnmasuk.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "2017", "2018", "2019", "2020",


"2021", "2022", "2023", "2024" }));

cmbprodi.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "Teknik Informatika", "Manajemen",


"Akuntansi", "Teknik Sipil" }));

jLabel4.setText("SPP");

jLabel5.setText("BPP");

jLabel6.setText("NPM");

datagridspp.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"

));

datagridspp.addMouseListener(new java.awt.event.MouseAdapter() {

public void mouseClicked(java.awt.event.MouseEvent evt) {

datagridsppMouseClicked(evt);

});

jScrollPane1.setViewportView(datagridspp);

btntambah.setText("Tambah");

btntambah.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

btntambahActionPerformed(evt);

});

btnhapus.setText("Hapus");

btnhapus.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

btnhapusActionPerformed(evt);

});

btnubah.setText("Ubah");

btnubah.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

btnubahActionPerformed(evt);
}

});

btncari.setText("Cari");

btncari.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

btncariActionPerformed(evt);

});

btnkeluar.setText("Keluar");

btnkeluar.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

btnkeluarActionPerformed(evt);

});

btnsimpan.setText("Simpan");

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());

getContentPane().setLayout(layout);

layout.setHorizontalGroup(

layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addGroup(layout.createSequentialGroup()

.addGap(10, 10, 10)

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)

.addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,


Short.MAX_VALUE)

.addComponent(jLabel3)

.addComponent(jLabel4)

.addComponent(txtspp)

.addComponent(jLabel5)

.addComponent(txtbpp)

.addComponent(jLabel6)
.addComponent(txtnpm)

.addComponent(cmbprodi, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,


javax.swing.GroupLayout.PREFERRED_SIZE)

.addComponent(cmbthnmasuk, 0, 0, Short.MAX_VALUE)

.addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,


Short.MAX_VALUE))

.addGap(18, 18, 18)

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)

.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 452,


javax.swing.GroupLayout.PREFERRED_SIZE)

.addGroup(layout.createSequentialGroup()

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addComponent(btnhapus)

.addComponent(btnsimpan))

.addGap(108, 108, 108)

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addGroup(layout.createSequentialGroup()

.addComponent(btntambah)

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)

.addComponent(btncari))

.addGroup(layout.createSequentialGroup()

.addComponent(btnubah)

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)

.addComponent(btnkeluar)))))

.addContainerGap(10, Short.MAX_VALUE))

);

layout.setVerticalGroup(

layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addGroup(layout.createSequentialGroup()

.addGap(30, 30, 30)

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)

.addGroup(layout.createSequentialGroup()

.addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)

.addComponent(jLabel2)

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)

.addComponent(cmbthnmasuk, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)

.addComponent(jLabel3)

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)

.addComponent(cmbprodi, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)

.addComponent(jLabel4)

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)

.addComponent(txtspp, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,


javax.swing.GroupLayout.PREFERRED_SIZE)

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)

.addComponent(jLabel5)

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)

.addComponent(txtbpp, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,


javax.swing.GroupLayout.PREFERRED_SIZE))

.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE))

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)

.addComponent(jLabel6)

.addComponent(btntambah)

.addComponent(btncari)

.addComponent(btnsimpan))

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)

.addComponent(txtnpm, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,


javax.swing.GroupLayout.PREFERRED_SIZE)

.addComponent(btnhapus)

.addComponent(btnkeluar)

.addComponent(btnubah))

.addContainerGap(30, Short.MAX_VALUE))
);

pack();

setLocationRelativeTo(null);

}// </editor-fold>

private void btncariActionPerformed(java.awt.event.ActionEvent evt) {

String cari = JOptionPane.showInputDialog(this, "Masukkan npm yang dicari:");

if (cari != null && !cari.trim().isEmpty()) {

try {

String sql = "SELECT * FROM tbspp WHERE npm = ?";

java.sql.Connection conn = (Connection) koneksiDB.configDB();

java.sql.PreparedStatement pst = conn.prepareStatement(sql);

pst.setString(1, cari);

java.sql.ResultSet res = pst.executeQuery();

DefaultTableModel model = new DefaultTableModel();

model.addColumn("thnmasuk");

model.addColumn("prodi");

model.addColumn("spp");

model.addColumn("bpp");

model.addColumn("npm");

boolean found = false;

while (res.next()) {

model.addRow(new Object[]{

res.getString("thnmasuk"),

res.getString("prodi"),

res.getString("spp"),

res.getString("bpp"),

res.getString("npm")

});
found = true;

datagridspp.setModel(model);

if (!found) {

JOptionPane.showMessageDialog(this, "Data tidak ditemukan", "Informasi",


JOptionPane.INFORMATION_MESSAGE);

res.close();

pst.close();

conn.close();

} catch (Exception e) {

JOptionPane.showMessageDialog(this, "Error: " + e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);

} else {

JOptionPane.showMessageDialog(this, "Masukkan notransaksi yang valid", "Informasi",


JOptionPane.INFORMATION_MESSAGE);

private void btntambahActionPerformed(java.awt.event.ActionEvent evt) {

// Perintah tambah data mahasiswa

try {

String sql = "INSERT INTO tbspp VALUES ('"+cmbthnmasuk.getSelectedItem()+"','"+cmbprodi.getSelectedItem()


+"','"+txtspp.getText()+"','"+txtbpp.getText()+"','"+txtnpm.getText()+"')";

java.sql.Connection conn=(Connection)koneksiDB.configDB();

java.sql.PreparedStatement pst = conn.prepareStatement(sql);

pst.execute();

JOptionPane.showMessageDialog(null, "Penyimpanan Data Berhasil");

} catch (Exception e) {

JOptionPane.showMessageDialog(this, e.getMessage());

load_table();
kosong();

private void btnhapusActionPerformed(java.awt.event.ActionEvent evt) {

//hapus data

try {

String sql = "DELETE FROM tbspp where npm='"+txtnpm.getText()+"'";

java.sql.Connection conn=(Connection)koneksiDB.configDB();

java.sql.PreparedStatement pst = conn.prepareStatement(sql);

pst.execute();

JOptionPane.showMessageDialog(this, "Berhasil dihapus");

} catch (Exception e) {

JOptionPane.showMessageDialog(this, e.getMessage());

load_table();

kosong();

private void btnubahActionPerformed(java.awt.event.ActionEvent evt) {

// Edit Data

try {

String sql ="UPDATE tbspp SET thnmasuk = '"+cmbthnmasuk.getSelectedItem()+"', prodi =


'"+cmbprodi.getSelectedItem()+"', spp = '"+txtspp.getText()+"',bpp= '"+txtbpp.getText()+"',npm= '"+txtnpm.getText()+"'
WHERE npm = '"+txtnpm.getText()+"'";

java.sql.Connection conn=(Connection)koneksiDB.configDB();

java.sql.PreparedStatement pst=conn.prepareStatement(sql);

pst.execute();

JOptionPane.showMessageDialog(null, "data berhasil di edit");

} catch (Exception e) {

JOptionPane.showMessageDialog(null, "Perubahan Data Gagal"+e.getMessage());

load_table();

kosong();

}
private void btnkeluarActionPerformed(java.awt.event.ActionEvent evt) {

System.exit(0);

private void datagridsppMouseClicked(java.awt.event.MouseEvent evt) {

// menampilkan data ke dalam form pengisian

int baris = datagridspp.rowAtPoint(evt.getPoint());

String thnmasuk =datagridspp.getValueAt(baris, 0).toString();

cmbthnmasuk.setSelectedItem(thnmasuk);

String prodi = datagridspp.getValueAt(baris,1).toString();

cmbprodi.setSelectedItem(prodi);

String spp = datagridspp.getValueAt(baris, 2).toString();

txtspp.setText(spp);

String bpp = datagridspp.getValueAt(baris, 3).toString();

txtbpp.setText(bpp);

String npm = datagridspp.getValueAt(baris, 4).toString();

txtnpm.setText(npm);

/**

* @param args the command line arguments

*/

public static void main(String args[]) {

java.awt.EventQueue.invokeLater(new Runnable() {

public void run() {

new frmspp().setVisible(true);

}
});

// Variables declaration - do not modify

private javax.swing.JButton btncari;

private javax.swing.JButton btnhapus;

private javax.swing.JButton btnkeluar;

private javax.swing.JButton btnsimpan;

private javax.swing.JButton btntambah;

private javax.swing.JButton btnubah;

private javax.swing.JComboBox<String> cmbprodi;

private javax.swing.JComboBox<String> cmbthnmasuk;

private javax.swing.JTable datagridspp;

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.JScrollPane jScrollPane1;

private javax.swing.JTextField txtbpp;

private javax.swing.JTextField txtnpm;

private javax.swing.JTextField txtspp;

// End of variables declaration

}
Hasil :
1. Menampilkan Data

2. Menambahkan Data ke Dalam Tabel

3. Mengubah Data di Dalam Tabel

4. Menghapus Data di Dalam Tabel


5. Mencari Data di Dalam Tabel

You might also like