Salam NewGen, kali ini
saya akan melanjutkan share belajar java 2 sekarang kita akan coba membuat
pencarian dan edit / update data pada Netbeans dengan menggunakan database
MySQL, Ok langsung saja...
Pertama kita buat
terlebih dahalu database dan tabelnya pada Xampp/ MySQL, disini saya
menggunakan database BelajarJava2 dan Tabel barang
Buat tabel dengan 3
field (kode, nama dan jenis) seperti gambar dibawah, kemudian Simpan
Kemudian kita isi tabel
tersebut
Kode Nama Jenis
S001 Software A SW
S002 Software B SW
H001 Hardware A HW
H002 Hardware B HW
Setelah selesai dengan
database dan tabelnya selanjutnya kita buka NetBeansnya dan buat project baru,
pilih File >> New Project >> Java >> Java Application
kemudian Next
Berinama Project Java2
pilih lokasi penyimpanan kemudian hilangkan ceklis pada Create Main Class karna
kita nanti hanya menggunakan 1 Jframe saja >> Finish
Setelah pembuatan
project baru selesai kita tambah Jframe pada project, Klik kanan default
package >> New >> Jframe berinama Cari_Edit 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
JTextField3 text = Variable Name = txtjenis
JButton1 text = Cari Variable Name = bcari
JButton12 text = Update Variable Name = bupdate
Setelah selesai design
Jframenya selanjutnya ke source dan ketik listing dibawah ini diatas public
class Cari_Edit
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_Edit 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"));
txtjenis.setText(rs.getString("jenis"));
javax.swing.JOptionPane.showMessageDialog(null, "Data
Ditemukan");
}
} catch (Exception e)
{
}
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 ='" + txtjenis.getText() + "' "
+
"where kode ='" + txtkode.getText() + "'";
stat =
kon.createStatement();
stat.execute(sql);
txtkode.setText("");
txtnama.setText("");
txtjenis.setText("");
txtkode.requestFocus();
{
javax.swing.JOptionPane.showMessageDialog(null, "Update Data
Sukses");
}
} catch (Exception e)
{
}
Setelah listing sudah
di ketikkan semua selanjutnya kita tambahkan Library koneksi database MySQL
dengan cara klik kanan Libraries >> Add Library
Cari dan Pilih MySQL
JDBC Driver >> Add Library
Selesai deh share kali
ini sekarang tinggal running
Semoga dapat bermanfaat
ya para NewGen, saya hanya sedikit berbagi apa yang saya dapatkan dalam
pelajaran mata kuliah Java2 kalian bisa melakukan pengembangan dan
mengexplorasi sendiri ya...
Salam NewGen...
Your Affiliate Money Printing Machine is waiting -
ReplyDeleteAnd making money with it is as simple as 1...2...3!
Follow the steps below to make money...
STEP 1. Input into the system what affiliate products you intend to promote
STEP 2. Add some push button traffic (this ONLY takes 2 minutes)
STEP 3. See how the affiliate products system grow your list and up-sell your affiliate products all by itself!
So, do you want to start making money??
Click here to activate the system
If you're looking to lose pounds then you need to jump on this brand new tailor-made keto meal plan diet.
ReplyDeleteTo design this keto diet, certified nutritionists, fitness couches, and professional chefs united to produce keto meal plans that are powerful, convenient, cost-efficient, and delicious.
From their grand opening in early 2019, hundreds of people have already remodeled their figure and health with the benefits a certified keto meal plan diet can offer.
Speaking of benefits; clicking this link, you'll discover 8 scientifically-certified ones given by the keto meal plan diet.