0% found this document useful (0 votes)
42 views

PBO

This document contains Java code for connecting to a MySQL database and performing CRUD (create, read, update, delete) operations on a table called "karyawan". It imports necessary Java and SQL libraries, defines a class called "koneksi" to establish the database connection, and includes code handlers for button clicks to save, edit, delete, and display records from the "karyawan" table. SQL queries are constructed and executed to manipulate the data in the table.
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)
42 views

PBO

This document contains Java code for connecting to a MySQL database and performing CRUD (create, read, update, delete) operations on a table called "karyawan". It imports necessary Java and SQL libraries, defines a class called "koneksi" to establish the database connection, and includes code handlers for button clicks to save, edit, delete, and display records from the "karyawan" table. SQL queries are constructed and executed to manipulate the data in the table.
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/ 3

form koneksi Java.

class

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import javax.swing.JOptionPane;

public class koneksi { // koneksi ke database


private static Connection mysqlkonek;
public static Connection koneksiDB() throws SQLException {
if(mysqlkonek==null){
try {
String DB="jdbc:mysql://localhost:3306/delta_db"; // delta_db database
String user="root"; // user database
String pass=""; // password database
DriverManager.registerDriver(new com.mysql.jdbc.Driver());
mysqlkonek = (Connection) DriverManager.getConnection(DB,user,pass);
} catch (Exception e) {
JOptionPane.showMessageDialog(null,"gagal koneksi");
}
}
return mysqlkonek;
}
}

Java.form
import java.awt.HeadlessException;
import java.sql.Connection;
import java.sql.SQLException;
import javax.swing.JOptionPane;
import net.proteanit.sql.DbUtils;
dan deklarasikan variable "databaru" dibawah class FrmUtama.java
public boolean databaru;
Selanjutnya langsung saja tuliskan code berikut pada tombol new
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
databaru=true;
// mengosongkan textbox
jTextField1.setText("");
jTextField2.setText("");
jTextField3.setText("");
jTextField4.setText("");
jTextField5.setText("");
jTextField6.setText("");
}
private void GetData(){ // menampilkan data dari database
try {
Connection conn =(Connection)delta.koneksi.koneksiDB();
java.sql.Statement stm = conn.createStatement();
java.sql.ResultSet sql = stm.executeQuery("select * from karyawan");
jTable1.setModel(DbUtils.resultSetToTableModel(sql));
}
catch (SQLException | HeadlessException e) {
}
}
if (databaru == true) { // prosess simpan atau edit
try {mu
String sql = "insert into karyawan values('"+jTextField1.getText()
+"','"+jTextField2.getText()+"','"+jTextField3.getText()+"','"+jTextField4.getText()
+"','"+jTextField5.getText()+"','"+jTextField6.getText()+"')";
java.sql.Connection conn = (java.sql.Connection)delta.koneksi.koneksiDB();
java.sql.PreparedStatement pst = conn.prepareStatement(sql);
pst.execute();
JOptionPane.showMessageDialog(null, "berhasil disimpan");
} catch (SQLException | HeadlessException e) {
JOptionPane.showMessageDialog(null, e);
}
} else {
try {
String sql = "update karyawan SET nama_karyawan='"+jTextField2.getText()
+"',nik='"+jTextField3.getText()+"',jabatan='"+jTextField4.getText()
+"',no_telphone='"+jTextField5.getText()+"',alamat='"+jTextField6.getText()+"' where
id_karyawan='"+jTextField1.getText()+"'";
java.sql.Connection conn = (java.sql.Connection)delta.koneksi.koneksiDB();
java.sql.PreparedStatement pst = conn.prepareStatement(sql);
pst.execute();
JOptionPane.showMessageDialog(null, "berhasil disimpan");
} catch (SQLException | HeadlessException e) {
JOptionPane.showMessageDialog(null, e);
}
}
GetData();
}
try { // hapus data
String sql ="delete from karyawan where id_karyawan='"+jTextField1.getText()+"'";
java.sql.Connection conn = (java.sql.Connection)delta.koneksi.koneksiDB();
java.sql.PreparedStatement pst = conn.prepareStatement(sql);
pst.execute();
JOptionPane.showMessageDialog(null, "Data akan dihapus?");
databaru=true;
jTextField1.setText("");
jTextField2.setText("");
jTextField3.setText("");
jTextField4.setText("");
jTextField5.setText("");
jTextField6.setText("");
} catch (SQLException | HeadlessException e) {}

databaru = false; // menampilkan data ke textboxt


try {
int row =jTable1.getSelectedRow();
String tabel_klik=(jTable1.getModel().getValueAt(row, 0).toString());
java.sql.Connection conn =(java.sql.Connection)delta.koneksi.koneksiDB();
java.sql.Statement stm = conn.createStatement();
java.sql.ResultSet sql = stm.executeQuery("select * from karyawan where
id_karyawan='"+tabel_klik+"'");
if(sql.next()){
String id = sql.getString("id_karyawan");
jTextField1.setText(id);
String nama = sql.getString("nama_karyawan");
jTextField2.setText(nama);
String nik = sql.getString("nik");
jTextField3.setText(nik);
String jabatan = sql.getString("jabatan");
jTextField4.setText(jabatan);
String no_telp = sql.getString("no_telphone");
jTextField5.setText(no_telp);
String alamat = sql.getString("alamat");
jTextField6.setText(alamat);
}
} catch (Exception e) {}

You might also like