Salam NewGen, sebelumnya
kita sudah belajar tentang pencarian & Edit / Update data dengan NetBeans
kali ini saya akan share tentang pencarian & Delete Data dengan NetBeans.
Seperti biasa terlebih dahulu
kita buat Database MySQL dan tabelnya disini saya menggunakan database
sebelumnya “BelajarJava2” dan tabel “barang”, kalian bisa juga membuat database
dan tabel yang baru.
Setelah selesai dengan
database dan tabelnya selanjutnya kita buka NetBeansnya dan buat project baru, jika
sudah buat Project pada posting sebelumnya kita tidak perlu membuatnya kembali
tapi jika belum membuatnya pilih File >> New Project >> Java
>> Java Application kemudian Next
Berinama Project Java2
pilih lokasi penyimpanan kemudian hilangkan ceklis pada Create Main Class karena
kita nanti hanya menggunakan 1 JJrame saja >> Finish
Setelah pembuatan
project baru selesai kita tambah Jframe pada project, Klik kanan default
package >> New >> JFrame berinama Cari_Delete kemudian Finish
Design JFrame seperti
dibawah ini
Nama Properties Code
JLabel1 text = Kode -
JLabel2 text = Nama -
JLabel3 text = Jenis -
JTextField1 text = Variable Name = txtkode
JTextField2 text = Variable Name = txtnama
JComboBox1 model = SW, HW Variable Name = combojenis
JButton1 text = Cari Variable Name = bcari
JButton2 text = Delete Variable Name = bdelete
Setelah selesai design
JFramenya selanjutnya ke source dan ketik listing dibawah ini diatas public
class Cari_Delete
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.table.DefaultTableModel;
Ketikan listing dibawah
setelah public class Cari_Delete yang digunakan untuk koneksi ke database
public Connection kon;
public String sql = "";
public Statement stat ;
public ResultSet rs;
Listing
Button Cari ( bcari )
try {
Class.forName("com.mysql.jdbc.Driver");
kon =
DriverManager.getConnection("jdbc:mysql://localhost:3306/BelajarJava2",
"root", "");
sql = "select
* from barang where kode='" + txtkode.getText() + "'";
stat =
kon.createStatement();
rs =
stat.executeQuery(sql);
while (rs.next())
{
txtnama.setText(rs.getString("nama"));
combojenis.setSelectedItem(rs.getString("jenis"));
javax.swing.JOptionPane.showMessageDialog(null,
"Data Ditemukan");
}
} catch (Exception e)
{
}
Listing
Button Delete ( bdelete )
try {
Class.forName("com.mysql.jdbc.Driver");
kon = DriverManager.getConnection("jdbc:mysql://localhost:3306/BelajarJava2",
"root", "");
sql = "delete
from barang where kode ='" + txtkode.getText() + "'";
stat =
kon.createStatement();
stat.execute(sql);
txtkode.setText("");
txtnama.setText("");
combojenis.setSelectedIndex(0);
txtkode.requestFocus();
{
javax.swing.JOptionPane.showMessageDialog(null, "Delete Data
Sukses");
}
} catch (Exception e)
{
}
Jangan lupa tambahkan
Library koneksi database MySQL dengan cara klik kanan Libraries >> Add
Library
Cari dan Pilih MySQL
JDBC Driver >> Add Library
Jika sudah ditambahkan
kita tidak perlu lagi menambahkannya
Setelah itu Running
Jframe Cari_Deletenya
Selesai sudah Belajar
Cari dan Delete Datanya, kita juga bisa tambahkan Edit/Update di Jframe tadi...
seperti design dibawah ini
Kemudian tambahkan
Listing
Button Update ( bupdate )
try {
Class.forName("com.mysql.jdbc.Driver");
kon =
DriverManager.getConnection("jdbc:mysql://localhost:3306/BelajarJava2",
"root", "");
sql = "update
barang set nama='" + txtnama.getText() + "',"
+
"jenis ='" + combojenis.getSelectedItem() + "' "
+
"where kode ='" + txtkode.getText() + "'";
stat =
kon.createStatement();
stat.execute(sql);
txtkode.setText("");
txtnama.setText("");
combojenis.setSelectedIndex(0);
txtkode.requestFocus();
{
javax.swing.JOptionPane.showMessageDialog(null, "Update Data
Sukses");
}
} catch (Exception e)
{
}
Salam NewGen...
Semangat terus kawan...
Did you hear there is a 12 word phrase you can speak to your man... that will induce intense emotions of love and impulsive appeal to you deep inside his chest?
ReplyDeleteThat's because deep inside these 12 words is a "secret signal" that fuels a man's impulse to love, adore and guard you with all his heart...
===> 12 Words That Trigger A Man's Desire Response
This impulse is so built-in to a man's mind that it will make him try harder than ever before to take care of you.
Matter-of-fact, triggering this influential impulse is absolutely mandatory to achieving the best ever relationship with your man that the moment you send your man a "Secret Signal"...
...You'll soon notice him expose his mind and soul for you in such a way he's never experienced before and he'll distinguish you as the only woman in the world who has ever truly attracted him.