Cara koneksi java ke mysql

By On Monday, September 19th, 2022 Categories : Tanya Jawab

Cara koneksi java ke mysql – Pa kabar Agan semua, Met Datang di web TrendWisata ini. Sore ini, kita di website trendwisata.com akan memberikan Q dan A yang wokeh yang mengulas Cara koneksi java ke mysql. Langsung saja Agan melihat berikut ini:

/*

 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license

 * Click nbfs://nbhost/SystemFileSystem/Templates/GUIForms/JFrame.java to edit this template

 */

package Penjualan;

import java.awt.event.KeyEvent;

import javax.swing.JOptionPane;

import java.sql.*;

import javax.swing.JTable;

/**

 *

 * @author Inzaghi Posuma

 */

public class FormBarang extends javax.swing.JFrame {

String user = “root”;

String pwd = “”;

String url = “jdbc:mysql://localhost/penjualan”;

Boolean isi = true;

/**

     * Creates new form FormBarang

     */

public FormBarang()

initComponents();

setLocationRelativeTo(null);

void aktif()

txtnama.setEnabled(true);

txtharga.setEnabled(true);

txtstok.setEnabled(true);

void nonaktif()

txtkode.setEnabled(false);

txtnama.setEnabled(false);

txtharga.setEnabled(false);

txtstok.setEnabled(false);

btnkeluar.setEnabled(true);

btntambah.setEnabled(true);

btnsimpan.setEnabled(false);

btnhapus.setEnabled(false);

btnrubah.setEnabled(false);

btnbatal.setEnabled(true);

void bersih()

txtkode.setText(“”);

txtnama.setText(“”);

txtharga.setText(“”);

txtstok.setText(“”);

void otomatis()

try

Connection conn = DriverManager.getConnection(url,user,pwd);

Statement st = (Statement) conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);

String sql = “Select right (kd_brg,3)+1 from barang”;

ResultSet rs = st.executeQuery(sql);

if(rs.next())

rs.last();

String kode = rs.getString(1);

while(kode.length()<3)

kode = “0” + kode;

txtkode.setText(“BR”+kode);

else

txtkode.setText(“BR001”);

catch (SQLException e)

System.out.println(“Koneksi gagal otomatis”+e.toString());

void cari()

try

Connection conn = DriverManager.getConnection(url,user,pwd);

Statement st = (Statement) conn.createStatement();

ResultSet rs = st.executeQuery(“Select * from barang where kd_brg='”+txtcari.getText()+“‘”);

if (rs.next())

txtkode.setText(rs.getString(“kd_brg”));

txtnama.setText(rs.getString(“nm_brg”));

txtharga.setText(rs.getString(“harga”));

txtstok.setText(rs.getString(“stok”));

else

JOptionPane.showMessageDialog(this,“Data tidak ditemukan”,“info”,JOptionPane.INFORMATION_MESSAGE);

txtcari.setText(“”);

catch (SQLException e)

System.out.println(“Koneksi gagal cari”+e.toString());

void simpan()

try

Connection conn = DriverManager.getConnection(url,user,pwd);

Statement st = (Statement) conn.createStatement();

String sql = “insert into barang values(‘”+txtkode.getText()+“‘,'”+

txtnama.getText()+“‘,'”+txtharga.getText()+“‘,'”+txtstok.getText()+“‘)”;

st.executeUpdate(sql);

JOptionPane.showMessageDialog(this,“Data berhasil disimpan”

,“info”,JOptionPane.INFORMATION_MESSAGE);

catch(SQLException e)

System.out.println(“Koneksi gagal simpan”+e.toString());

formWindowActivated(null);

void hapus()

try

Connection conn = DriverManager.getConnection(url,user,pwd);

Statement st = (Statement) conn.createStatement();

String sql = “delete from barang where kd_brg ='”+txtkode.getText()+“‘”;

st.executeUpdate(sql);

JOptionPane.showMessageDialog(this,“Data berhasil dihapus”,“info”,JOptionPane.INFORMATION_MESSAGE);

catch(SQLException e)

System.out.println(“Koneksi gagal hapus”+e.toString());

formWindowActivated(null);

void update()

try

Connection conn = DriverManager.getConnection(url,user,pwd);

Statement st = (Statement) conn.createStatement();

String sql = “update barang set nm_brg ='”+txtnama.getText()+

“‘, harga=””+txtharga.getText()+“”,stok='”+txtstok.getText()+“‘”+“where kd_brg='”+txtkode.getText()+“‘”;

st.executeUpdate(sql);

JOptionPane.showMessageDialog(this,“Data berhasil diupdate”

,“info”,JOptionPane.INFORMATION_MESSAGE);

catch(SQLException e)

System.out.println(“Koneksi gagal”+e.toString());

formWindowActivated(null);

private Object [][] getData()

Object [][] data = null;

try

Connection conn = DriverManager.getConnection(url,user,pwd);

Statement st = (Statement) conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);

ResultSet rs = st.executeQuery(“Select * from barang”);

rs.last();

int rowCount = rs.getRow();

rs.beforeFirst();

data = new Object[rowCount] [4];

int no = –1;

while (rs.next())

no = no+1;

data[no] [0] = rs.getString(“kd_brg”);

data[no] [1] = rs.getString(“nm_brg”);

data[no] [2] = rs.getString(“harga”);

data[no] [3] = rs.getString(“stok”);

catch(SQLException e)

System.out.println(“Koneksi gagal tampil”+e.toString());

return data;

void tampil()

String[] columnNames = “Kode Barang”, “Nama Barang”, “Harga”, “Stok”;

JTable table = new JTable(getData(), columnNames);

table.setEnabled(false);

jScrollPane1.setViewportView(table);

jScrollPane2.setViewportView(table);

/**

     * This method is called from within the constructor to initialize the form.

     * WARNING: Do NOT modify this code. The content of this method is always

     * regenerated by the Form Editor.

     */

@SuppressWarnings(“unchecked”)

// <editor-fold defaultstate=”collapsed” desc=”Generated Code”>//GEN-BEGIN:initComponents

private void initComponents()

jScrollPane1 = new javax.swing.JScrollPane();

jTable1 = new javax.swing.JTable();

jLabel1 = new javax.swing.JLabel();

jLabel2 = new javax.swing.JLabel();

jLabel3 = new javax.swing.JLabel();

jLabel4 = new javax.swing.JLabel();

jLabel5 = new javax.swing.JLabel();

jLabel6 = new javax.swing.JLabel();

txtnama = new javax.swing.JTextField();

txtkode = new javax.swing.JTextField();

txtharga = new javax.swing.JTextField();

txtstok = new javax.swing.JTextField();

txtcari = new javax.swing.JTextField();

btnkeluar = new javax.swing.JButton();

btnhapus = new javax.swing.JButton();

btncari = new javax.swing.JButton();

btntambah = new javax.swing.JButton();

btnsimpan = new javax.swing.JButton();

btnrubah = new javax.swing.JButton();

btnbatal = new javax.swing.JButton();

jScrollPane2 = new javax.swing.JScrollPane();

tbbarang = new javax.swing.JTable();

jTable1.setModel(new javax.swing.table.DefaultTableModel(

new Object [][]

null, null, null, null,

null, null, null, null,

null, null, null, null,

null, null, null, null

,

new String []

Title 1″,Title 2″,Title 3″,Title 4″

));

jScrollPane1.setViewportView(jTable1);

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

addWindowListener(new java.awt.event.WindowAdapter()

public void windowActivated(java.awt.event.WindowEvent evt)

formWindowActivated(evt);

);

jLabel1.setText(“Kode Barang”);

jLabel2.setText(“Nama Barang”);

jLabel3.setText(“Harga Barang”);

jLabel4.setText(“Stok Barang”);

jLabel5.setText(“Pencarian Data Barang”);

jLabel6.setText(“Input Koda Barang Anda dari :”);

txtnama.addActionListener(new java.awt.event.ActionListener()

public void actionPerformed(java.awt.event.ActionEvent evt)

txtnamaActionPerformed(evt);

);

txtnama.addKeyListener(new java.awt.event.KeyAdapter()

public void keyPressed(java.awt.event.KeyEvent evt)

txtnamaKeyPressed(evt);

);

txtkode.addActionListener(new java.awt.event.ActionListener()

public void actionPerformed(java.awt.event.ActionEvent evt)

txtkodeActionPerformed(evt);

);

txtharga.addKeyListener(new java.awt.event.KeyAdapter()

public void keyPressed(java.awt.event.KeyEvent evt)

txthargaKeyPressed(evt);

);

txtstok.addKeyListener(new java.awt.event.KeyAdapter()

public void keyPressed(java.awt.event.KeyEvent evt)

txtstokKeyPressed(evt);

);

btnkeluar.setText(“Keluar”);

btnkeluar.addActionListener(new java.awt.event.ActionListener()

public void actionPerformed(java.awt.event.ActionEvent evt)

btnkeluarActionPerformed(evt);

);

btnhapus.setText(“Hapus”);

btnhapus.addActionListener(new java.awt.event.ActionListener()

public void actionPerformed(java.awt.event.ActionEvent evt)

btnhapusActionPerformed(evt);

);

btncari.setText(“Cari”);

btncari.addActionListener(new java.awt.event.ActionListener()

public void actionPerformed(java.awt.event.ActionEvent evt)

btncariActionPerformed(evt);

);

btncari.addKeyListener(new java.awt.event.KeyAdapter()

public void keyPressed(java.awt.event.KeyEvent evt)

btncariKeyPressed(evt);

);

btntambah.setText(“Tambah”);

btntambah.addActionListener(new java.awt.event.ActionListener()

public void actionPerformed(java.awt.event.ActionEvent evt)

btntambahActionPerformed(evt);

);

btnsimpan.setText(“Simpan”);

btnsimpan.addActionListener(new java.awt.event.ActionListener()

public void actionPerformed(java.awt.event.ActionEvent evt)

btnsimpanActionPerformed(evt);

);

btnsimpan.addKeyListener(new java.awt.event.KeyAdapter()

public void keyPressed(java.awt.event.KeyEvent evt)

btnsimpanKeyPressed(evt);

);

btnrubah.setText(“Rubah”);

btnrubah.addActionListener(new java.awt.event.ActionListener()

public void actionPerformed(java.awt.event.ActionEvent evt)

btnrubahActionPerformed(evt);

);

btnbatal.setText(“Batal”);

btnbatal.addActionListener(new java.awt.event.ActionListener()

public void actionPerformed(java.awt.event.ActionEvent evt)

btnbatalActionPerformed(evt);

);

tbbarang.setModel(new javax.swing.table.DefaultTableModel(

new Object [][]

null, null, null, null,

null, null, null, null,

null, null, null, null,

null, null, null, null

,

new String []

Title 1″,Title 2″,Title 3″,Title 4″

));

jScrollPane2.setViewportView(tbbarang);

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());

getContentPane().setLayout(layout);

layout.setHorizontalGroup(

layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addGroup(layout.createSequentialGroup()

.addGap(40, 40, 40)

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addGroup(layout.createSequentialGroup()

.addGap(34, 34, 34)

.addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)

.addGap(74, 74, 74))

.addGroup(layout.createSequentialGroup()

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)

.addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)

.addComponent(jLabel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 64, javax.swing.GroupLayout.PREFERRED_SIZE)

.addComponent(jLabel2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)

.addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 67, javax.swing.GroupLayout.PREFERRED_SIZE))

.addGap(18, 18, 18)

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addGroup(layout.createSequentialGroup()

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)

.addComponent(txtharga, javax.swing.GroupLayout.Alignment.LEADING)

.addComponent(txtkode, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)

.addComponent(txtnama, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 125, javax.swing.GroupLayout.PREFERRED_SIZE))

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)

.addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 133, javax.swing.GroupLayout.PREFERRED_SIZE)

.addComponent(jLabel6, javax.swing.GroupLayout.DEFAULT_SIZE, 165, Short.MAX_VALUE)

.addComponent(txtcari))

.addGap(116, 116, 116))

.addGroup(layout.createSequentialGroup()

.addComponent(txtstok, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)

.addComponent(btncari, javax.swing.GroupLayout.PREFERRED_SIZE, 73, javax.swing.GroupLayout.PREFERRED_SIZE)

.addGap(128, 128, 128))))))

.addGroup(layout.createSequentialGroup()

.addGap(28, 28, 28)

.addComponent(btntambah, javax.swing.GroupLayout.PREFERRED_SIZE, 84, javax.swing.GroupLayout.PREFERRED_SIZE)

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)

.addComponent(btnsimpan, javax.swing.GroupLayout.PREFERRED_SIZE, 84, javax.swing.GroupLayout.PREFERRED_SIZE)

.addGap(10, 10, 10)

.addComponent(btnrubah, javax.swing.GroupLayout.PREFERRED_SIZE, 84, javax.swing.GroupLayout.PREFERRED_SIZE)

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)

.addComponent(btnbatal, javax.swing.GroupLayout.PREFERRED_SIZE, 84, javax.swing.GroupLayout.PREFERRED_SIZE)

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)

.addComponent(btnhapus, javax.swing.GroupLayout.PREFERRED_SIZE, 84, javax.swing.GroupLayout.PREFERRED_SIZE)

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)

.addComponent(btnkeluar, javax.swing.GroupLayout.PREFERRED_SIZE, 84, javax.swing.GroupLayout.PREFERRED_SIZE)

.addGap(0, 0, Short.MAX_VALUE))

);

layout.setVerticalGroup(

layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addGroup(layout.createSequentialGroup()

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addGroup(layout.createSequentialGroup()

.addGap(41, 41, 41)

.addComponent(jLabel5)

.addGap(18, 18, 18)

.addComponent(jLabel6)

.addGap(12, 12, 12)

.addComponent(txtcari, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))

.addGroup(layout.createSequentialGroup()

.addGap(53, 53, 53)

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)

.addComponent(jLabel1)

.addComponent(txtkode, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addComponent(jLabel2)

.addComponent(txtnama, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))

.addGap(1, 1, 1)

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)

.addComponent(jLabel3)

.addComponent(txtharga, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)

.addComponent(jLabel4)

.addComponent(txtstok, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)

.addComponent(btncari))

.addGap(53, 53, 53)

.addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE)

.addGap(41, 41, 41)

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)

.addComponent(btnbatal)

.addComponent(btntambah)

.addComponent(btnsimpan)

.addComponent(btnrubah)

.addComponent(btnhapus)

.addComponent(btnkeluar))

.addContainerGap(58, Short.MAX_VALUE))

);

pack();

// </editor-fold>//GEN-END:initComponents

private void txtkodeActionPerformed(java.awt.event.ActionEvent evt) //GEN-FIRST:event_txtkodeActionPerformed

nonaktif();

//GEN-LAST:event_txtkodeActionPerformed

private void btntambahActionPerformed(java.awt.event.ActionEvent evt) //GEN-FIRST:event_btntambahActionPerformed

txtnama.setEnabled(true);

txtharga.setEnabled(true);

txtstok.setEnabled(true);

btntambah.setEnabled(false);

btnsimpan.setEnabled(true);

btnhapus.setEnabled(false);

btnrubah.setEnabled(false);

btnbatal.setEnabled(true);

otomatis();

//txtkode.setText(“BR001”);

txtnama.grabFocus();

//GEN-LAST:event_btntambahActionPerformed

private void formWindowActivated(java.awt.event.WindowEvent evt) //GEN-FIRST:event_formWindowActivated

nonaktif();

bersih();

//GEN-LAST:event_formWindowActivated

private void btnkeluarActionPerformed(java.awt.event.ActionEvent evt) //GEN-FIRST:event_btnkeluarActionPerformed

System.exit(0);

//GEN-LAST:event_btnkeluarActionPerformed

private void btncariActionPerformed(java.awt.event.ActionEvent evt) //GEN-FIRST:event_btncariActionPerformed

cari();

btnsimpan.setEnabled(true);

btnhapus.setEnabled(true);

btntambah.setEnabled(false);

btnbatal.setEnabled(true);

btnkeluar.setEnabled(true);

btnrubah.setEnabled(true);

//GEN-LAST:event_btncariActionPerformed

private void btnhapusActionPerformed(java.awt.event.ActionEvent evt) //GEN-FIRST:event_btnhapusActionPerformed

int ok = JOptionPane.showConfirmDialog(this, “Yakin akan dihapus?”, “konfirmasi”, JOptionPane.YES_NO_OPTION);

if (ok == 0);

hapus();

formWindowActivated(null);

//GEN-LAST:event_btnhapusActionPerformed

private void btnbatalActionPerformed(java.awt.event.ActionEvent evt) //GEN-FIRST:event_btnbatalActionPerformed

formWindowActivated(null);

//GEN-LAST:event_btnbatalActionPerformed

private void btnrubahActionPerformed(java.awt.event.ActionEvent evt) //GEN-FIRST:event_btnrubahActionPerformed

// TODO add your handling code here:

isi = false;

aktif();

btnrubah.setEnabled(false);

btntambah.setEnabled(false);

btnsimpan.setEnabled(true);

btnbatal.setEnabled(true);

//GEN-LAST:event_btnrubahActionPerformed

private void txtnamaKeyPressed(java.awt.event.KeyEvent evt) //GEN-FIRST:event_txtnamaKeyPressed

if (evt.getKeyChar() == KeyEvent.VK_ENTER)

txtharga.requestFocusInWindow();

//GEN-LAST:event_txtnamaKeyPressed

private void txthargaKeyPressed(java.awt.event.KeyEvent evt) //GEN-FIRST:event_txthargaKeyPressed

// TODO add your handling code here:

if (evt.getKeyChar() == KeyEvent.VK_ENTER)

txtstok.requestFocusInWindow();

//GEN-LAST:event_txthargaKeyPressed

private void btnsimpanActionPerformed(java.awt.event.ActionEvent evt) //GEN-FIRST:event_btnsimpanActionPerformed

// TODO add your handling code here:

//GEN-LAST:event_btnsimpanActionPerformed

private void btnsimpanKeyPressed(java.awt.event.KeyEvent evt) //GEN-FIRST:event_btnsimpanKeyPressed

if (evt.getKeyChar() == KeyEvent.VK_ENTER)

if (isi == true)

simpan();

else

update();

//GEN-LAST:event_btnsimpanKeyPressed

private void btncariKeyPressed(java.awt.event.KeyEvent evt) //GEN-FIRST:event_btncariKeyPressed

// TODO add your handling code here:

if (evt.getKeyChar() == KeyEvent.VK_ENTER)

cari();

btnsimpan.setEnabled(true);

btnhapus.setEnabled(true);

btntambah.setEnabled(false);

btnbatal.setEnabled(true);

btnkeluar.setEnabled(true);

btnrubah.setEnabled(true);

//GEN-LAST:event_btncariKeyPressed

private void txtnamaActionPerformed(java.awt.event.ActionEvent evt) //GEN-FIRST:event_txtnamaActionPerformed

// TODO add your handling code here:

//GEN-LAST:event_txtnamaActionPerformed

private void txtstokKeyPressed(java.awt.event.KeyEvent evt) //GEN-FIRST:event_txtstokKeyPressed

if (evt.getKeyChar() == KeyEvent.VK_ENTER)

btnsimpan.requestFocusInWindow();

//GEN-LAST:event_txtstokKeyPressed

/**

     * @param args the command line arguments

     */

public static void main(String args[])

/* Set the Nimbus look and feel */

//<editor-fold defaultstate=”collapsed” desc=” Look and feel setting code (optional) “>

/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.

         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html

         */

try

for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels())

if (“Nimbus”.equals(info.getName()))

javax.swing.UIManager.setLookAndFeel(info.getClassName());

break;

catch (ClassNotFoundException ex)

java.util.logging.Logger.getLogger(FormBarang.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);

catch (InstantiationException ex)

java.util.logging.Logger.getLogger(FormBarang.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);

catch (IllegalAccessException ex)

java.util.logging.Logger.getLogger(FormBarang.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);

catch (javax.swing.UnsupportedLookAndFeelException ex)

java.util.logging.Logger.getLogger(FormBarang.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);

//</editor-fold>

/* Create and display the form */

java.awt.EventQueue.invokeLater(new Runnable()

public void run()

new FormBarang().setVisible(true);

);

// Variables declaration – do not modify//GEN-BEGIN:variables

private javax.swing.JButton btnbatal;

private javax.swing.JButton btncari;

private javax.swing.JButton btnhapus;

private javax.swing.JButton btnkeluar;

private javax.swing.JButton btnrubah;

private javax.swing.JButton btnsimpan;

private javax.swing.JButton btntambah;

private javax.swing.JLabel jLabel1;

private javax.swing.JLabel jLabel2;

private javax.swing.JLabel jLabel3;

private javax.swing.JLabel jLabel4;

private javax.swing.JLabel jLabel5;

private javax.swing.JLabel jLabel6;

private javax.swing.JScrollPane jScrollPane1;

private javax.swing.JScrollPane jScrollPane2;

private javax.swing.JTable jTable1;

private javax.swing.JTable tbbarang;

private javax.swing.JTextField txtcari;

private javax.swing.JTextField txtharga;

private javax.swing.JTextField txtkode;

private javax.swing.JTextField txtnama;

private javax.swing.JTextField txtstok;

// End of variables declaration//GEN-END:variables

}

ResultSet adalah komponen yang menyimpan data hasil perintah ke database. SQLException adalah komponen untuk menangani kesalahan-kesalahan yang mungkin terjadi saat pengolahan database.

JDBC adalah Application Programming Interface (API) yang menyediakan fungsi-fungsi dasar untuk akses data. JDBC API terdiri atas sejumlah class dan interface yang dapat digunakan untuk menulis aplikasi database dengan menggunakan Java.

Cara koneksi java ke mysql | admin | 4.5