Sie sind auf Seite 1von 16

TUGAS II

BAHASA PEMROGRAMAN
Menampilkan Database ke dalam Jtable

YUDI NUGRAHA

2018091022

PRODI SISTEM INFORMASI

FAKULTAS ILMU KOMPUTER

UNIVERSITAS KUNINGAN
2020

Pembuatan Database Menggunakan MYSQL

Dengan nama database “db_barang”

-- phpMyAdmin SQL Dump


-- version 4.8.4
-- https://www.phpmyadmin.net/
--
-- Host: localhost
-- Waktu pembuatan: 11 Apr 2020 pada 16.55
-- Versi server: 10.1.37-MariaDB
-- Versi PHP: 7.0.33

SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";


SET AUTOCOMMIT = 0;
START TRANSACTION;
SET time_zone = "+00:00";

/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;


/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;

--
-- Database: `db_barang`
--
-- --------------------------------------------------------

--
-- Struktur dari tabel `barang`
--

CREATE TABLE `barang` (


`kode_barang` int(100) NOT NULL,
`nama_barang` varchar(100) NOT NULL,
`satuan` int(100) NOT NULL,
`stock` int(100) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

--
-- Dumping data untuk tabel `barang`
--

INSERT INTO `barang` (`kode_barang`, `nama_barang`, `satuan`, `stock`) VALUES


(1, 'Mentega', 4000, 22),
(2, 'Sikat Gigi', 2000, 30);

--
-- Indexes for dumped tables
--

--
-- Indeks untuk tabel `barang`
--
ALTER TABLE `barang`
ADD PRIMARY KEY (`kode_barang`);

--
-- AUTO_INCREMENT untuk tabel yang dibuang
--

--
-- AUTO_INCREMENT untuk tabel `barang`
--
ALTER TABLE `barang`
MODIFY `kode_barang` int(100) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=3;
COMMIT;

/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;


/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;

Pembuatan Aplikasi Menggunakan Netbean IDE 8.2


Membuat Package dengan NIM 2018091022
Membuat class koneksidb.java untuk interface koneksi dengan database
/*

* To change this license header, choose License Headers in Project Properties.

* To change this template file, choose Tools | Templates

* and open the template in the editor.

*/

package pkg2018091022;

/**
*

* @author macbook

*/

import com.mysql.jdbc.Driver;

import java.sql.DriverManager;

import java.sql.Connection;

import java.sql.SQLException;

/**

* @author Faw

*/

public class koneksidb {

public static Connection koneksi;

public static Connection getConnection()throws SQLException {

if (koneksi == null) {

new Driver();

koneksi = DriverManager.getConnection("jdbc:mysql://localhost:3306/db_barang","root","");

return koneksi;

Membuat class data_barang untuk menampilkan hasil pengambilan data


barang
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package pkg2018091022;

/**
*
* @author macbook
*/
import pkg2018091022.koneksidb;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.sql.Connection;
import java.sql.Statement;
import javax.swing.table.DefaultTableColumnModel;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import java.sql.ResultSet;
import java.sql.SQLDataException;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import javax.swing.JOptionPane;

/**
*
* @author Faw
*/
public class data_barang extends javax.swing.JFrame {
private Dimension layar;
private DefaultTableModel Model;
/**
*
*/
public data_barang() {
initComponents();
setTitle("Form Barang");
//Agar posisi form diTengah :
layar = Toolkit.getDefaultToolkit().getScreenSize();
setLocation((layar.width / 2) - (getSize().width / 2),
(layar.height / 2) - (getSize().height / 2));

//Untuk Atur Header Table :


Model=new DefaultTableModel();
tabel_barang.setModel(Model);

Model.addColumn("Kode Barang");
Model.addColumn("Nama Barang");
Model.addColumn("Satuan");
Model.addColumn("Stok");

//Mengambil nilai dari Method :


AmbilDataBarang();
}
public void AmbilDataBarang() {
//Menghapus Seluruh Data
Model.getDataVector().removeAllElements();
//Memberitahu bahwa data kosong
Model.fireTableDataChanged();

try {
//Memanggil koneksi :
Connection c=koneksidb.getConnection();
//Membaca perintah SQL static di JAVA :
Statement st=c.createStatement();
//Perintah QUERY :
String sql = "SELECT * FROM barang";
//Menjalankan perintah Query :
ResultSet rs=st.executeQuery(sql);

while (rs.next()) {
Object[] o=new Object[4];
o[0]=rs.getString("kode_barang");
o[1]=rs.getString("nama_barang");
o[2]=rs.getString("satuan");
o[3]=rs.getInt("stock");
Model.addRow(o);
}
rs.close();
st.close();
} catch (SQLException e) {
System.out.println("Terjadi Error dalam pengambilan data"+e);
}

/**
* 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">
private void initComponents() {

jScrollPane1 = new javax.swing.JScrollPane();


tabel_barang = new javax.swing.JTable();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

tabel_barang.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(tabel_barang);

jLabel1.setText("Yudi Nugraha");

jLabel2.setText("2018091022");

jLabel3.setText("Tugas 2");

javax.swing.GroupLayout layout = new


javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(

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

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LE
ADING)
.addGroup(layout.createSequentialGroup()
.addGap(64, 64, 64)

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LE
ADING)
.addComponent(jLabel1)
.addComponent(jLabel2)))
.addGroup(layout.createSequentialGroup()
.addGap(77, 77, 77)
.addComponent(jLabel3)))

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED,
129, Short.MAX_VALUE)
.addComponent(jScrollPane1,
javax.swing.GroupLayout.PREFERRED_SIZE, 375,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap())
);
layout.setVerticalGroup(

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

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LE
ADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jScrollPane1,
javax.swing.GroupLayout.PREFERRED_SIZE, 275,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addGap(25, 25, 25)
.addComponent(jLabel1)

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jLabel2)
.addGap(58, 58, 58)
.addComponent(jLabel3)))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE,
Short.MAX_VALUE))
);

pack();
}// </editor-fold>

/**
* @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(data_barang.class.getName()).log(java.util.lo
gging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {

java.util.logging.Logger.getLogger(data_barang.class.getName()).log(java.util.lo
gging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {

java.util.logging.Logger.getLogger(data_barang.class.getName()).log(java.util.lo
gging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {

java.util.logging.Logger.getLogger(data_barang.class.getName()).log(java.util.lo
gging.Level.SEVERE, null, ex);
}
//</editor-fold>

/* Create and display the form */


java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new data_barang().setVisible(true);
}
});
}

// Variables declaration - do not modify


private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTable tabel_barang;
// End of variables declaration
}

Screenshoot Tampilan
*design form
*Hasil Running

*interface database mysql