0% found this document useful (0 votes)
46 views3 pages

Copykan Ini

This document contains code for connecting to a MySQL database in Java, retrieving data from a database table, and inserting new data into that table. It defines a method to get a database connection, retrieves all records from a table called "t_barang" and displays them in a JTable, and includes a button click handler that inserts new record data into that table using a prepared statement.

Uploaded by

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

Copykan Ini

This document contains code for connecting to a MySQL database in Java, retrieving data from a database table, and inserting new data into that table. It defines a method to get a database connection, retrieves all records from a table called "t_barang" and displays them in a JTable, and includes a button click handler that inserts new record data into that table using a prepared statement.

Uploaded by

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

COPYKAN DI CLASS KONEKSI

public static Connection getConnection(){


Connection conn = null;
String url = "jdbc:mysql://localhost/dbcoba";
String user = "root";
String password = "";
try{
conn = DriverManager.getConnection(url, user, password);
}catch (SQLException e){
System.out.println(e);
}
return conn;
}
public static void main (String []args){
try{
Connection c = koneksi.getConnection();
System.out.println(String.format("Terkoneksi kedata base %s "+" Sukses !",c.getCatalog()));
}catch(SQLException e){
System.out.println(e);
}
}

COPYKAN DI BAWAH public class formBarang extends javax.swing.JFrame {

DefaultTableModel mod;
private String SQL;
/**
* Creates new form formBarang
*/
public formBarang() {
initComponents();
bersih();
tampilData();
setLocationRelativeTo(this);
}

private void bersih(){


txtKodeBarang.setText(null);
txtNamaBarang.setText(null);
txtHarga.setText(null);
txtSatuan.setText(null);
txtStok.setText(null);
}
//inner clas tampil data
private void tampilData(){
mod = new DefaultTableModel();
mod.addColumn("Kode Barang");
mod.addColumn("Nama Barang");
mod.addColumn("Harga");
mod.addColumn("Satuan");
mod.addColumn("Stok");
tabelData.setModel(mod);
Connection conn = koneksi.getConnection();
try{
java.sql.Statement stmt = conn.createStatement();
SQL = "select * from t_barang";
java.sql.ResultSet res = stmt.executeQuery(SQL);
while(res.next()){
mod.addRow(new Object[]{
res.getString("kode_barang"),
res.getString("nama_barang"),
res.getString("harga"),
res.getString("satuan"),
res.getString("stok")
});
}
}catch (SQLException e){
System.out.println(e);
}
}

COPYKAN DI BUTTON SIMPAN

try{
Connection conn = koneksi.getConnection();
PreparedStatement stmt = conn.prepareStatement
("insert into t_barang (kode_barang, nama_barang, harga, satuan, stok) value (?,?,?,?,?)");
stmt.setString(1, txtKodeBarang.getText());
stmt.setString(2, txtNamaBarang.getText());
stmt.setString(3, txtHarga.getText());
stmt.setString(4, txtSatuan.getText());
stmt.setString(5, txtStok.getText());
stmt.executeUpdate();
JOptionPane.showMessageDialog(null, "Data berhasil disimpan","Pesan",
JOptionPane.INFORMATION_MESSAGE);
tampilData();
bersih();
}catch (SQLException e){
System.out.println(e);
}

You might also like