package moddesktop3;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
/**
*
* @author trj3je
*/
public class FUpdateData extends javax.swing.JFrame {
Connection koneksi;
Statement perintah;
ResultSet hasil;
/** Creates new form FUpdateData */
public FUpdateData() {
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() {
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();
txtNama = new javax.swing.JTextField();
cbNim = new javax.swing.JComboBox();
txtAlamat = new javax.swing.JTextField();
txtJurusan = new javax.swing.JTextField();
btnTutup = new javax.swing.JButton();
btnOke = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
addWindowListener(new java.awt.event.WindowAdapter() {
public void windowActivated(java.awt.event.WindowEvent evt) {
formWindowActivated(evt);
}
});
jLabel1.setFont(new java.awt.Font("Monotype Corsiva", 1, 24)); // NOI18N
jLabel1.setText("UPDATE DATA MAHASISWA");
jLabel2.setText("NIM");
jLabel3.setText("NAMA");
jLabel4.setText("ALAMAT");
jLabel5.setText("JURUSAN");
cbNim.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
cbNimMouseClicked(evt);
}
});
btnTutup.setText("TUTUP");
btnTutup.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnTutupActionPerformed(evt);
}
});
btnOke.setText("UPDATE");
btnOke.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnOkeActionPerformed(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)
.addComponent(jLabel1)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel2)
.addComponent(jLabel4)
.addComponent(jLabel3)
.addComponent(jLabel5)
.addComponent(btnTutup))
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(layout.createSequentialGroup()
.addGap(54, 54, 54)
.addComponent(btnOke))
.addGroup(layout.createSequentialGroup()
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(cbNim, 0, 248, Short.MAX_VALUE))
.addGroup(layout.createSequentialGroup()
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(txtNama, javax.swing.GroupLayout.DEFAULT_SIZE, 248, Short.MAX_VALUE))
.addGroup(layout.createSequentialGroup()
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(txtAlamat, javax.swing.GroupLayout.DEFAULT_SIZE, 248, Short.MAX_VALUE))
.addGroup(layout.createSequentialGroup()
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(txtJurusan, javax.swing.GroupLayout.DEFAULT_SIZE, 248, Short.MAX_VALUE)))))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(24, 24, 24)
.addComponent(jLabel1)
.addGap(44, 44, 44)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(cbNim, 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.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(44, 44, 44)
.addComponent(jLabel4))
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel3)
.addComponent(txtNama, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addComponent(txtAlamat, 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()
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 28, Short.MAX_VALUE)
.addComponent(jLabel5)
.addGap(32, 32, 32))
.addGroup(layout.createSequentialGroup()
.addGap(18, 18, 18)
.addComponent(txtJurusan, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(btnTutup)
.addComponent(btnOke))
.addContainerGap())
);
pack();
}// </editor-fold>
private void btnOkeActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
try {
String konek = "jdbc:mysql://localhost:3306/mahasiswa";
String user = "root";
String pass = "root";
Class.forName("com.mysql.jdbc.Driver");
koneksi = DriverManager.getConnection(konek, user, pass);
String nim, nama, alamat, jurusan;
nim = cbNim.getItemAt(cbNim.getSelectedIndex()).toString().trim();
nama = txtNama.getText().trim();
alamat = txtAlamat.getText().trim();
jurusan = txtJurusan.getText().trim();
perintah = koneksi.createStatement();
String sql = "UPDATE dtmhs SET nama = '"+nama+"',"+" alamat = '"+alamat+"',"+" jurusan = '"+jurusan+"'"+" WHERE nim = '"+nim+"';";
perintah.execute(sql);
perintah.close();
JOptionPane.showMessageDialog(null, "Berhasil Disimpan");
}catch(ClassNotFoundException ex){
Logger.getLogger(FUpdateData.class.getName()).log(Level.SEVERE,null, ex);
JOptionPane.showMessageDialog(null, "GAGAL MENYIMPAN DATA MAHASISWA");
}catch(SQLException ex){
Logger.getLogger(FUpdateData.class.getName()).log(Level.SEVERE,null, ex);
JOptionPane.showMessageDialog(null, "GAGAL.......!! COBA LAGI");
}
}
private void btnTutupActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
System.exit(0);
}
private void formWindowActivated(java.awt.event.WindowEvent evt) {
// TODO add your handling code here:
try
{
String konek = "jdbc:mysql://localhost:3306/mahasiswa";
String user = "root";
String pass = "root";
Class.forName("com.mysql.jdbc.Driver");
koneksi = DriverManager.getConnection(konek, user, pass);
String sql = "SELECT nim FROM dtmhs ORDER BY nim";
cbNim.removeAllItems();
try
{
perintah = koneksi.createStatement();
hasil = perintah.executeQuery(sql);
}
catch (SQLException ex)
{
return;
}
try
{
while (hasil.next())
{
cbNim.addItem(hasil.getString("nim").toString());
}
hasil.close();
perintah.close();
}
catch (SQLException ex)
{
return;
}
}
catch(ClassNotFoundException ex)
{
Logger.getLogger(FUpdateData.class.getName()).log(Level.SEVERE,null, ex);
}
catch(SQLException ex)
{
Logger.getLogger(FUpdateData.class.getName()).log(Level.SEVERE,null, ex);
}
}
private void cbNimMouseClicked(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
String nim = cbNim.getItemAt(cbNim.getSelectedIndex()).toString().trim();
String sql = "SELECT * FROM dtmhs WHERE nim = '"+nim+"';";
try{
perintah = koneksi.createStatement();
hasil = perintah.executeQuery(sql);
}
catch (SQLException ex) {
return;
}
try {
while (hasil.next()) {
txtNama.setText(hasil.getString("nama").toString());
txtAlamat.setText(hasil.getString("alamat").toString());
txtJurusan.setText(hasil.getString("jurusan").toString());
}
hasil.close();
perintah.close();
}
catch (SQLException ex) {
return;
}
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new FUpdateData().setVisible(true);
}
});
}
//dibawah ini merupakan database yang saya gunakan
mysql> desc dtmhs;
+---------+------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+------------+------+-----+---------+-------+
| nim | varchar(4) | NO | | | |
| nama | char(14) | YES | | NULL | |
| alamat | char(14) | YES | | NULL | |
| jurusan | char(2) | YES | | NULL | |
+---------+------------+------+-----+---------+-------+
4 rows in set (0.06 sec)
mysql> insert into dtmhs (nim, nama, alamat, jurusan)
-> values
-> ('0123','Tri Julians','Yogyakarta','SI');
Query OK, 1 row affected (0.01 sec)
mysql> select * from dtmhs;
+------+----------+------------+---------+
| nim | nama | alamat | jurusan |
+------+----------+------------+---------+
| 0123 | Tri Juls | Yogyakarta | SI |
+------+----------+------------+---------+
1 row in set (0.00 sec)
//dan yang ada dibawah ini adalah hasilnya :)
Tidak ada komentar:
Posting Komentar