coding untuk tabel operator menggunakan java
package
projec_penjualan;
import
javax.swing.JTable.*;
import
javax.swing.table.DefaultTableModel;
import
javax.swing.JOptionPane;
import
java.text.*;
import
java.sql.*;
import
java.util.Date.*;
/**
*
*
@author yuli
*/
public
class operator extends javax.swing.JFrame {
String[]
judul={"Kode Operator","Nama
Operator","Kelamin","Alamat","Username"};
DefaultTableModel
DTM;
/**
Creates new form operator */
public
operator() {
DTM=new
DefaultTableModel(null,judul){
public boolean
iscellEditable(int now,int coloum){
return false;
}
};
initComponents();
kosong();
filltable();
}
Kode Tombol Keluar :
private
void bkeluarActionPerformed(java.awt.event.ActionEvent evt) {
//
TODO add your handling code here:
if(bkeluar.getText().equals("Keluar")){
dispose();
setVisible(false);
}
else
kosong();
}
Kode Kode Operator :
private
void txtkoActionPerformed(java.awt.event.ActionEvent evt) {
//
TODO add your handling code here:
try{
conekdb getCn=new conekdb();
Connection Cn=getCn.konek();
Statement
st=Cn.createStatement();
String SQL="SELECT *FROM
operator WHERE kdoperator= '"+txtko.getText().trim()+"'";
ResultSet
rs=st.executeQuery(SQL);
if(rs.next()){
JOptionPane.showMessageDialog(null,"Data
Sudah ada");
txtno.setText(""+rs.getString("nmoperator"));
if(rs.getString("Kelamin").equals("L"))
rdpria.setSelected(true);
else
rdwanita.setSelected(true);
txtal.setText(""+rs.getString("alamat"));
txtun.setText(""+rs.getString("userid"));
txtpass.setText(""+rs.getString("passid"));
bsimpan.setText("Update");
bhapus.setEnabled(true);
}else{
bsimpan.setText("Simpan");
JOptionPane.showMessageDialog(null,"Data
Baru");
}
bsimpan.setEnabled(true);
bkeluar.setText("Batal");
st.close();
Cn.close();
rs.close();
}catch(Exception exc){
System.err.println(exc.getMessage());
}
txtno.requestFocus();
}
Kode Tombol Simpan :
private
void bsimpanActionPerformed(java.awt.event.ActionEvent evt) {
//
TODO add your handling code here:
if(txtko.getText().equals(""))
JOptionPane.showMessageDialog(null,"Isi
Data Operator dengan lengkap");
else{
String kel="";
if(rdpria.isSelected()==true)
kel="L";
else
kel="P";
Data
clsData=new Data();
String SQL="";
if(bsimpan.getText().equals("Simpan")){
SQL=" INSERT INTO
operator
VALUES('"+txtko.getText().trim()+"','"+txtno.getText().trim()+"','"+kel+"','"+txtal.getText().trim()+"','"+txtun.getText().trim()+"','"+txtpass.getText().trim()+"')";
clsData.ExecuteSQL(SQL);
}
else{
SQL="UPDATE operator SET
nmoperator='"+txtno.getText().trim()+"',kelamin='"+kel+"',alamat='"+txtal.getText().trim()+"',
userid='"+txtun.getText().trim()+"',passid='"+txtpass.getText().trim()+"'
WHERE kdoperator = '"+txtko.getText().trim()+"'";
clsData.ExecuteSQL(SQL);
}
clsData=null;
kosong();
filltable();
}
}
Kode Tombol Hapus :
private
void bhapusActionPerformed(java.awt.event.ActionEvent evt) {
//
TODO add your handling code here:
try{
conekdb getCn= new conekdb();
Connection Cn=getCn.konek();
Statement
st=Cn.createStatement();
String SQL="DELETE FROM
operator WHERE kdoperator='"+txtko.getText().trim()+"'";
st.executeUpdate(SQL);
st.close();
Cn.close();
kosong();
JOptionPane.showMessageDialog(null,
"Delete berhasil");
}catch(Exception e){
JOptionPane.showMessageDialog(null,"Proses
hapus Gagal !!");
System.err.println(e.getMessage());
}
filltable();
}
Kode
untuk Prosedur kosong sbb :
void
kosong(){
txtko.setText("");
txtno.setText("");
rdpria.setSelected(true);
rdwanita.setSelected(false);
txtal.setText("");
txtun.setText("");
txtpass.setText("");
bsimpan.setEnabled(false);
bhapus.setEnabled(false);
bkeluar.setEnabled(true);
bsimpan.setText("Simpan");
bkeluar.setText("Keluar");
txtko.requestFocus();
}
Kode Prosedur filltable :
void
filltable(){
String
SQL="SELECT *FROM operator";
DTM.setRowCount(0);
try{
conekdb getCn=new conekdb();
Connection Cn = getCn.konek();
Statement
st=Cn.createStatement();
ResultSet
rs=st.executeQuery(SQL);
while(rs.next())
{
String
vkdoperator=rs.getString("kdoperator");
String
vnmoperator=rs.getString("nmoperator");
String
valamat=rs.getString("alamat");
String
vkelamin=rs.getString("kelamin");
String kel="";
if (vkelamin.equals("L"))
kel="Laki-laki";
else
kel="Wanita";
String
vusername=rs.getString("userid");
String[]data={vkdoperator,vnmoperator,kel,valamat,vusername};
DTM.addRow(data);
}
st.close();
Cn.close();
rs.close();
TBoperator.setModel(DTM);
}catch(SQLException e){
}
}
Tags:
knowledge
0 comments