1. USer Interface
2. Design
3. Coding
/*
* 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 db1;
import Koneksi.db_koneksi;
import javax.swing.table.DefaultTableModel;
import com.mysql.jdbc.Statement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.swing.JOptionPane;
/**
*
* @author SMKMoedikal
*/
public final class TampilSiswa extends javax.swing.JFrame {
//membuat variable model dengan untuk DefaultTableModel
private DefaultTableModel model;
/**
* Creates new form TampilSiswa
*/
public void dataSelect(){
int i = tblSiswa.getSelectedRow();
if (i==-1){
return;
}
txtnis.setText(""+model.getValueAt(i, 0));
txtnama.setText(""+model.getValueAt(i, 1));
}
public void saveData(){
try{
//membuat statemen pemanggilan data pada table tblSiswa dari database
Statement stat = (Statement) db_koneksi.getKoneksi().createStatement();
String sql = "insert into siswa values('"+txtnis.getText()+"','"+txtnama.getText()+"')";
stat.executeUpdate(sql);
getData();
}catch(SQLException err){
JOptionPane.showMessageDialog(null, err.getMessage() );
}
}
public void getData( ){
//menghapus isi table tblGaji
model.getDataVector( ).removeAllElements( );
model.fireTableDataChanged( );
try{
//membuat statemen pemanggilan data pada table tblSiswa dari database
Statement stat = (Statement) db_koneksi.getKoneksi().createStatement( );
String sql = "Select * from siswa";
ResultSet res = stat.executeQuery(sql);
//penelusuran baris pada tabel tblGaji dari database
while(res.next ()){
Object[ ] obj = new Object[2];
obj[0] = res.getString("nis");
obj[1] = res.getString("Nama");
model.addRow(obj);
}
}catch(SQLException err){
JOptionPane.showMessageDialog(null, err.getMessage() );
}
}
public TampilSiswa() {
initComponents();
//memberi penamaan pada judul kolom tblSiswa;
model = new DefaultTableModel ( );
tblSiswa.setModel(model);
model.addColumn("NIS");
model.addColumn("Nama");
getData();
}
/**
* 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() {
jScrollPane2 = new javax.swing.JScrollPane();
tblSiswa = new javax.swing.JTable();
jLabel1 = new javax.swing.JLabel();
txtnis = new javax.swing.JTextField();
jLabel2 = new javax.swing.JLabel();
txtnama = new javax.swing.JTextField();
btnsave = new javax.swing.JButton();
btnupdate = new javax.swing.JButton();
btnDelete = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
tblSiswa.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"
}
));
tblSiswa.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
tblSiswaMouseClicked(evt);
}
});
jScrollPane2.setViewportView(tblSiswa);
jLabel1.setText("NIS");
jLabel2.setText("NAMA");
btnsave.setText("Save");
btnsave.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnsaveActionPerformed(evt);
}
});
btnupdate.setText("Update");
btnupdate.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnupdateActionPerformed(evt);
}
});
btnDelete.setText("Delete");
btnDelete.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnDeleteActionPerformed(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()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 511, Short.MAX_VALUE)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel1)
.addComponent(jLabel2))
.addGap(69, 69, 69)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(txtnis, javax.swing.GroupLayout.PREFERRED_SIZE, 95, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(txtnama, javax.swing.GroupLayout.PREFERRED_SIZE, 220, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(layout.createSequentialGroup()
.addComponent(btnsave)
.addGap(26, 26, 26)
.addComponent(btnupdate)
.addGap(36, 36, 36)
.addComponent(btnDelete)))
.addGap(0, 0, Short.MAX_VALUE)))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 128, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(32, 32, 32)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1)
.addComponent(txtnis, 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(jLabel2)
.addComponent(txtnama, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(41, 41, 41)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(btnsave)
.addComponent(btnupdate)
.addComponent(btnDelete))
.addContainerGap(79, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void btnsaveActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code her
saveData();
}
private void btnupdateActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
try{
//membuat statemen pemanggilan data pada table tblSiswa dari database
Statement stat = (Statement) db_koneksi.getKoneksi().createStatement();
String sql = "UPDATE siswa set Nama = '"+ txtnama.getText() +"' WHERE nis = '"+txtnis.getText()+"'";
stat.executeUpdate(sql);
getData();
txtnis.setText("");
txtnama.setText("");
JOptionPane.showMessageDialog(null, "Update Berhasil ...");
}catch(SQLException err){
JOptionPane.showMessageDialog(null, err.getMessage() );
}
}
private void tblSiswaMouseClicked(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
dataSelect();
}
private void btnDeleteActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
int pesan = JOptionPane.showConfirmDialog(null, "Anda Yakin Akan HApus "+txtnis.getText()+"?","Konfirmasi",JOptionPane.OK_CANCEL_OPTION);
if (pesan == JOptionPane.OK_OPTION){
try{
Statement stat = (Statement) db_koneksi.getKoneksi().createStatement();
String sql = "DELETE from siswa WHERE nis = '"+txtnis.getText()+"'";
stat.executeUpdate(sql);
getData();
txtnis.setText("");
txtnama.setText("");
JOptionPane.showMessageDialog(null, "Delete Berhasil ...");
}catch(SQLException err){
JOptionPane.showMessageDialog(null, err.getMessage() );
}
}
}
/**
* @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(TampilSiswa.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(TampilSiswa.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(TampilSiswa.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(TampilSiswa.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 TampilSiswa().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton btnDelete;
private javax.swing.JButton btnsave;
private javax.swing.JButton btnupdate;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JScrollPane jScrollPane2;
private javax.swing.JTable tblSiswa;
private javax.swing.JTextField txtnama;
private javax.swing.JTextField txtnis;
// End of variables declaration
}
Tidak ada komentar:
Posting Komentar