Download Event Mouse Klik di JTable

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
BAHASA PEMROGRAMAN BERORIENTASI OBJEK
Abdul Rahim, S.kom
TUJUAN

JAVA DATABASE, INPUT, DELETE, UPDATE
JAVA DATABASE
JMySQL.java
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.JOptionPane;
public class JMySQL {
public static Connection getKoneksi(){
String hostDbDriver = "jdbc:mysql://localhost/db_java";
Connection cn = null;
try {
cn = DriverManager.getConnection(hostDbDriver,"root","12123");
} catch (SQLException ex) {
JOptionPane.showMessageDialog(null, "Database error" +ex);
}
return cn;
}
public static void closeKoneksi(Connection cn, Statement st, ResultSet rs){
try {
if(st != null){st.close();}
if(rs != null){rs.close();}
if(cn != null){cn.close();}
}catch(SQLException ex) {
JOptionPane.showMessageDialog(null, ex);
}
}
}
FORM DATA USER
Gunakan frame_data_user pada latihan sebelumnya seperti gambar berikut :
Pada program ini, kita akan menggunakan event double click pada jtable untuk menampilkan
data yang hendak diubah pada jtextfield.
Dan one click untuk menghapus data.
Membuat method dataTable() pada frame_data_user
DefaultTableModel tblMode = null;
public void dataTabel(){
//menghapus semua baris pada tabel
tblMode = (DefaultTableModel) tblData.getModel();
tblMode.getDataVector().removeAllElements();
tblMode.fireTableDataChanged();
//end
Connection cn = JMySQL.getKoneksi();
Statement st = null;
ResultSet rs = null;
try {
st = cn.createStatement();
String select = "select *from tuser order by id desc";
st.executeQuery(select);
rs = st.getResultSet();
int row = 0;
while (rs.next()){
//tambah baris data kosong di tabel
tblMode.addRow(new Object[]{"","",""});
//mengisi data pada cell tabel
tblData.setValueAt(rs.getString("id"), row, 0);
tblData.setValueAt(rs.getString("username"), row, 1);
tblData.setValueAt(rs.getString("password"), row, 2);
row++;
}
JMySQL.closeKoneksi(cn, st, rs);
} catch (SQLException ex) {
JOptionPane.showMessageDialog(this, "Error database " +ex);
}
}
FORM DATA USER
Kode di Konstruktor
txtUser.requestFocus();
//disable tombol ubah
btnUbah.setEnabled(false);
this.dataTabel();
FORM DATA USER
Event Mouse Klik di JTable
if(evt.getClickCount() == 2){
btnUbah.setEnabled(true);
btnSimpan.setEnabled(false);
btnHapus.setEnabled(false);
int barisKlik = tblData.getSelectedRow();
String
id = String.valueOf(tblData.getValueAt(barisKlik, 0));
String username = String.valueOf(tblData.getValueAt(barisKlik, 1));
String password = String.valueOf(tblData.getValueAt(barisKlik, 2));
txtUser.setText(username);
txtIdUser.setText(id);
txtPassword.setText(password);
txtKpassword.setText(password);
}
FORM DATA USER
Event btnUbah Action
String user = txtUser.getText();
String pass = String.valueOf(txtPassword.getPassword());
String idUser = txtIdUser.getText();
String queryUpdate = "update tuser set password='"+pass+"', username='"+user+"'
where id='"+idUser+"'";
try {
Connection cn = JMySQL.getKoneksi();
Statement st = cn.createStatement();
st.executeUpdate(queryUpdate);
JOptionPane.showMessageDialog(this, "Data sudah diubah");
JMySQL.closeKoneksi(cn, st, null);
} catch (SQLException ex) {
JOptionPane.showMessageDialog(this, "Data gagal diubah, error "+ex);
}
FORM DATA USER
Event btnUbah Action
this.dataTabel();
btnUbah.setEnabled(false);
btnSimpan.setEnabled(true);
btnHapus.setEnabled(true);
txtIdUser.setText("");
txtUser.setText("");
txtPassword.setText("");
txtKpassword.setText("");
txtUser.requestFocus();
FORM DATA USER
Event btnHapus
int confirm = JOptionPane.showConfirmDialog(this, "Data akan dihapus");
if(confirm == JOptionPane.OK_OPTION){
int baris = tblData.getSelectedRow();
String id = String.valueOf(tblData.getValueAt(baris, 0));
String queryUpdate = "delete from tuser where id='"+id+"'";
try {
Connection cn = JMySQL.getKoneksi();
Statement st = cn.createStatement();
st.executeUpdate(queryUpdate);
JOptionPane.showMessageDialog(this, "Data sudah dihapus");
JMySQL.closeKoneksi(cn, st, null);
} catch (SQLException ex) {
JOptionPane.showMessageDialog(this, "Data gagal hapus, error "+ex);
}
this.dataTabel();
}