0% found this document useful (0 votes)
32 views10 pages

Public Class Petugas Extends Javax - Swing.Jframe (

This Java code defines a class called Petugas that extends JFrame and contains methods for connecting to a MySQL database, displaying data in a table, and performing CRUD operations on a table called tb_petugas. It imports SQL and Swing classes, defines instance variables to store connection and result set objects, and includes methods for connecting to the database, displaying data, clearing fields, and handling button clicks to save, edit, delete and search for records.

Uploaded by

Iqbal Agistany
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views10 pages

Public Class Petugas Extends Javax - Swing.Jframe (

This Java code defines a class called Petugas that extends JFrame and contains methods for connecting to a MySQL database, displaying data in a table, and performing CRUD operations on a table called tb_petugas. It imports SQL and Swing classes, defines instance variables to store connection and result set objects, and includes methods for connecting to the database, displaying data, clearing fields, and handling button clicks to save, edit, delete and search for records.

Uploaded by

Iqbal Agistany
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

import java.sql.

Connection;

import java.sql.DriverManager;

import java.sql.ResultSet;

import java.sql.Statement;

import java.text.SimpleDateFormat;

import javax.swing.JOptionPane;

import javax.swing.table.DefaultTableModel;

public class Petugas extends javax.swing.JFrame {

private Statement st;

private Connection Con;

private ResultSet Rs;

private String Tanggal="";

private String Sql="";

private String host;

public String kodepetugas,namapetugas,tanggallahir,tempatlahir,jeniskelamin,jabatan,alamat,nohp;

public Petugas() {

initComponents();

KoneksiPetugas();

TampilPetugas("SELECT*FROM tb_petugas");

kosongkan();

}
private void KoneksiPetugas(){

try {

Class.forName("com.mysql.jdbc.Driver"); //Jika Tidak Terjadi Error

Con=DriverManager.getConnection("jdbc:mysql://localhost:3306/db_perpustakaan","root","");

System.out.println("Connection Succes");

JOptionPane.showMessageDialog(null, "Connection Succes");

} catch (Exception e) {

System.out.println("Connection Failed"+e);

JOptionPane.showMessageDialog(null, "Connection failed");

private void TampilPetugas(String Sql){

DefaultTableModel grid=new DefaultTableModel();

grid.addColumn("No");

grid.addColumn("Kode Petugas");

grid.addColumn("Nama Petugas");

grid.addColumn("Tanggal Lahir");

grid.addColumn("Tempat Lahir");

grid.addColumn("Jenis Kelamin");

grid.addColumn("Jabatan");

grid.addColumn("Alamat");

grid.addColumn("No.Hp");

try {

int i=1;

st=Con.createStatement();
Rs=st.executeQuery("SELECT*FROM tb_petugas");

while (Rs.next()){

grid.addRow(new Object[]{

(""+i++),Rs.getString(1),

Rs.getString(2),

Rs.getString(3),

Rs.getString(4),

Rs.getString(5),

Rs.getString(6),

Rs.getString(7),

Rs.getString(8)

});

TPETUGAS.setModel(grid);

TPETUGAS.enable(false);

BTNTAMBAH.requestFocus();

} catch (Exception e) {

JOptionPane.showMessageDialog(null,"Gagal Tampil: "+e);

}
private void kosongkan (){

TXTKODEPETUGAS.setText("");

TXTNAMAPETUGAS.setText("");

JTANGGAL.setDate(null);

TXTTEMPATLAHIR.setText("");

CBJENISKELAMIN.setSelectedItem(null);

CBJABATAN.setSelectedItem(null);

TXTALAMAT.setText("");

TXTNOHP.setText("");

TXTKODEPETUGAS.requestFocus();

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

// TODO add your handling code here:

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

// TODO add your handling code here:

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

// TODO add your handling code here:

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

// TODO add your handling code here:

kodepetugas=TXTKODEPETUGAS.getText();

namapetugas=TXTNAMAPETUGAS.getText();

tempatlahir=TXTTEMPATLAHIR.getText();

jeniskelamin=CBJENISKELAMIN.getItemAt(CBJENISKELAMIN.getSelectedIndex());

jabatan=CBJABATAN.getItemAt(CBJABATAN.getSelectedIndex());

alamat=TXTALAMAT.getText();

nohp=TXTNOHP.getText();

try { Sql="UPDATE tb_petugas SET nama_petugas='"+namapetugas+"',"

+ "tanggal_lahir='"+Tanggal+"',"

+ "tempat_lahir='"+tempatlahir+"',"

+ "jenis_kelamin='"+jeniskelamin+"',"

+ "jabatan='"+jabatan+"',"

+ "alamat='"+alamat+"',"

+ "nohp='"+nohp+"'"

+ "WHERE Kode_petugas='"+kodepetugas+"'";

st=Con.createStatement();

st.execute(Sql);

kosongkan();

TampilPetugas("SELECT*FROM tb_petugas");

JOptionPane.showMessageDialog(null, "Edit Succses");

BTNTAMBAH.show();

BTNSIMPAN.show();
} catch (Exception e) {

JOptionPane.showConfirmDialog(null,"Data Tidak Di Perbaharui\n"+e.getMessage());

private void JTANGGALPropertyChange(java.beans.PropertyChangeEvent evt) {

// TODO add your handling code here:

if(JTANGGAL.getDate()!=null){

SimpleDateFormat format =new SimpleDateFormat ("yyyy-MM-dd");

Tanggal=format.format(JTANGGAL.getDate());

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

// TODO add your handling code here:

kodepetugas=TXTKODEPETUGAS.getText();

namapetugas=TXTNAMAPETUGAS.getText();

tempatlahir=TXTTEMPATLAHIR.getText();

jeniskelamin=CBJENISKELAMIN.getItemAt(CBJENISKELAMIN.getSelectedIndex());

jabatan=CBJABATAN.getItemAt(CBJABATAN.getSelectedIndex());

alamat=TXTALAMAT.getText();

nohp=TXTNOHP.getText();

try {

Sql="INSERT INTO tb_petugas"

+
"(kode_petugas,nama_petugas,tanggal_lahir,tempat_lahir,jenis_kelamin,jabatan,alamat,nohp)"
+ "VALUES('"+kodepetugas+"',"

+ "'"+namapetugas+"',"

+ "'"+Tanggal+"',"

+ "'"+tempatlahir+"',"

+ "'"+jeniskelamin+"',"

+ "'"+jabatan+"',"

+ "'"+alamat+"',"

+ "'"+nohp+"')";

st=Con.createStatement();

st.execute(Sql);

kosongkan();

TampilPetugas("SELECT*FROM tb_petugas");

JOptionPane.showMessageDialog(null, "Saving Succses");

BTNTAMBAH.show();

BTNSIMPAN.enable(true);

} catch (Exception e) {

JOptionPane.showConfirmDialog(null,"Data Tidak Tersimpan\n"+e.getMessage());

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

// TODO add your handling code here:

kodepetugas=TXTKODEPETUGAS.getText();
try { Sql="DELETE FROM tb_petugas "

+ "WHERE Kode_petugas='" +kodepetugas+ "'";

st=Con.createStatement();

st.execute(Sql);

kosongkan();

TampilPetugas("SELECT*FROM tb_petugas");

JOptionPane.showMessageDialog(null, "Delete Succses");

BTNTAMBAH.show();

BTNSIMPAN.show();

} catch (Exception e) {

JOptionPane.showConfirmDialog(null,"Data Tidak Di Hapus\n"+e.getMessage());

private void TXTKODEPETUGASKeyPressed(java.awt.event.KeyEvent evt) {

// TODO add your handling code here:

kodepetugas=TXTKODEPETUGAS.getText();

int A = evt.getKeyCode();

if (A==10){

try {

Sql="SELECT * FROM tb_petugas WHERE Kode_petugas='"+kodepetugas+"'";

st=Con.createStatement();

Rs=st.executeQuery(Sql);

while(Rs.next()){
TXTNAMAPETUGAS.setText(Rs.getString(2));

JTANGGAL.setDate(Rs.getDate(3));

TXTTEMPATLAHIR.setText(Rs.getString(4));

CBJENISKELAMIN.setSelectedItem(Rs.getString(5));

CBJABATAN.setSelectedItem(Rs.getString(6));

TXTALAMAT.setText(Rs.getString(7));

TXTNOHP.setText(Rs.getString(8));

BTNSIMPAN.enable(false);

BTNEDIT.enable(true);

BTNHAPUS.enable(true);

} catch (Exception e) {

JOptionPane.showConfirmDialog(null, "Data Not Found\n"+e.getMessage());

TXTNAMAPETUGAS.requestFocus();

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

// TODO add your handling code here:

kosongkan();

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

// TODO add your handling code here:

new MenuUtama().setVisible(true);

this.dispose();

You might also like