2.4. Membuat Form Anggota dengan Menggunakan NetBeans

1. Membuat Tabel Anggota pada Database Perpustakaan

C:\Documents and Settings\Toshiba>cd\
C:\>cd wamp\mysql\bin\
C:\wamp\mysql\bin>mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3 to server version: 5.0.21-community-nt

Type ‘help;’ or ‘\h’ for help. Type ‘\c’ to clear the buffer.
Membuat database
mysql> use perpustakaan;
Database changed
Membuat table
mysql> create table anggota(Noang varchar(5), namang varchar(25),
-> tmplhr varchar(15), tgllhr varchar(8), jenisk varchar(10),
-> agama varchar(10), alamat varchar(25), kota varchar(25));
Query OK, 0 rows affected (0.55 sec)

mysql> insert into anggota values (“00001″,”Ahmad”,”Padang”,”12/12/2010″,”Laki-L
aki”,”Islam”,”Pegambiran”,”Padang”);
Query OK, 1 row affected, 1 warning (0.41 sec)
2. Membuat Form Anggota
Nama Class : fromAnggota
Edit sehingga membentuk form berikut ini :

3. Menambah koding pada kelas PrjMhsView, untuk mengatur apabila diklik menu Input dan Klik ANggota maka formAnggota akan ditampilkan.
Caranya :
Masuk pada tab Design , klik Input, Klik kanan pada Anggota, klik Events, klik Action dan klik actionPerformance. Lalu ketikkan koding berikut yang cetak tebal
private void anggotaMenuItemActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
formAnggota FAng = new formAnggota();
jDesktopPane1.add(FAng, javax.swing.JLayeredPane.DEFAULT_LAYER);
FAng.show();
}

4. Menambah Skrip Program pada FormAnggota
a. Import file
package prjtrans;

import javax.swing.JFrame;
import javax.swing.JTable;
import javax.swing.table.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;

b. Mendefinsikan Variabel
public class LoginForm extends javax.swing.JDialog {
String data[] = new String[4];
Database dbsetting;
//Query DBQ;
String driver, dtbase, user,pass,userLogin;

c. Membuat pemodelan table dan membuat koding untuuk membaca file Database.ini, dibawah iniComponents seperti berikut :
public LoginForm(java.awt.Frame parent, boolean modal) {
super(parent, modal);
initComponents();
dbsetting = new Database();
driver = dbsetting.SettingPanel(“DBDriver”);
dtbase = dbsetting.SettingPanel(“DBDatabase”);
user = dbsetting.SettingPanel(“DBUsername”);
pass = dbsetting.SettingPanel(“DBPassword”);
this.setSize(600,400);
this.setSize(450,480);

jTable1.setModel(tblModel);
Tabel(jTable1, new int[] {100,130,120});
setDefaultTable();
}
d. Membuat object
private javax.swing.table.DefaultTableModel tblModel= getDefaultTabelModel();

e. Membuat function Tabel
private void Tabel(javax.swing.JTable tb, int lebar[]){
tb.setAutoResizeMode(jTable2.AUTO_RESIZE_OFF);
int kolom = tb.getColumnCount();
for (int i=0; i<kolom; i++){
javax.swing.table.TableColumn tbc = tb.getColumnModel().getColumn(i);
tbc.setPreferredWidth(lebar[i]);
tb.setRowHeight(17);
}
}

f. Membuat method getDefaultTableModel()
private javax.swing.table.DefaultTableModel getDefaultTabelModel(){
return new javax.swing.table.DefaultTableModel(
new Object[][]{},
new String[] {“No Ang”,”Nama”,”Jenis”}
){
boolean[] canEdit = new boolean[]{
false,false,false
};
public boolean isCellEditable(int rowIndext, int columnIndex){
return canEdit[columnIndex];
}
};
}
g. Membuat function setDefaultTable
public void setDefaultTable(){
try{
Class.forName(driver);
Connection kon = DriverManager.getConnection(Databs,user,””);
Statement stt = kon.createStatement();
String SQL = “Select * from Anggota”;
ResultSet res= stt.executeQuery(SQL);
while (res.next()){
data[0] = res.getString(1);
data[1] = res.getString(2);
data[2] = res.getString(3);
tblModel.addRow(data);
}
res.close();
stt.close();
kon.close();
}
catch(Exception exc){
System.err.println(exc.getMessage());
}
}
h. Menampilkan data pada table
private DefaultTableModel tableModel(String[] colNames, ResultSet rs) throws SQLException {
int row = 0;
while (rs.next()) {
row = rs.getRow();
}
rs.beforeFirst();
String[][] data1 = new String[row][colNames.length];
for (int i = 0; i < row; i++) {
rs.next();
for (int j = 0; j < colNames.length; j++) {
data1[i][j] = rs.getString(j + 1);
}
}
return new DefaultTableModel(data1, colNames);
}

i. Jalankan program dengan cara menekan tombol F6.

j. Membuat koding pada table sehingga pada saat di double klik data akan dipindahkan dari table ke form adapun langkah-langkahnya adalah : klik Kanan pada table di form, lalu klik events, mouse, mousecliked dan ketikkan listing berikut :
private void jTable1MouseClicked(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
if (evt.getClickCount()==1)
Tampil();
}
k. Membuat function Tampil() yang akand igunakan untuk memindahkan data dari table ke form. Adapun kodingnya adalah :
public void Tampil(){
row = jTable1.getSelectedRow();
jTextField1.setText(dataModel.getValueAt(row, 0).toString());
jTextField2.setText(dataModel.getValueAt(row, 1).toString());
jTextField3.setText(dataModel.getValueAt(row, 2).toString());
jFormattedTextField1.setText(dataModel.getValueAt(row, 3).toString());
if (dataModel.getValueAt(row, 4).toString().equals(“Laki-Laki”))
jRadioButton1.setSelected(true);
else
jRadioButton1.setSelected(false);
jComboBox1.setSelectedItem(dataModel.getValueAt(row, 5).toString());
}
l. Membuat koding pada tombol Add untuk menyimpan data
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
String jenisk=””;
if (jRadioButton1.isSelected())
jenisk=”Laki-Laki”;
else
jenisk=”Perempuan”;
try{
Class.forName(driver);
Connection kon = DriverManager.getConnection(Databs,user,””);
Statement stt = kon.createStatement();

String SQL = “insert into anggota values(‘”+ jTextField1.getText()+”‘,'”+jTextField2.getText()+”‘,'”+jTextField3.getText()+”‘,'”+jFormattedTextField1.getText()+”‘,'”+ jenisk +”‘,'”+jComboBox1.getSelectedItem().toString() +”‘,'”+jTextField4.getText()+”‘,'” +jTextField5.getText()+”‘)”;
stt.executeUpdate(SQL);
data[0] =jTextField1.getText();
data[1]=jTextField2.getText();
data[2]=jTextField3.getText();
tblModel.insertRow(0,data);
stt.close();
kon.close();
}catch(Exception exc){
System.err.println(exc.getMessage());
}
}
m. Membuat koding pada tombol Edit
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
String jenisk=””;
if (jRadioButton1.isSelected())
jenisk=”Laki-Laki”;
else
jenisk=”Perempuan”;
try{
Class.forName(driver);
Connection kon = DriverManager.getConnection(Databs,user,””);
Statement stt = kon.createStatement();

String SQL = “update anggota set namang =’”+ jTextField1.getText() +”’, ….”;
stt.executeUpdate(SQL);

data[0] =jTextField1.getText();
data[1]=jTextField2.getText();
data[2]=jTextField3.getText();
tblModel.removeRow(row);
tblModel.insertRow(0,data);
stt.close();
kon.close();
}catch(Exception exc){
System.err.println(exc.getMessage());
}
}
n. Membuat koding pada tombol hapus

try{
Class.forName(driver);
Connection kon = DriverManager.getConnection(Databs,user,””);
Statement stt = kon.createStatement();

String SQL = “delete from anggota wher noang =’”+ jTextField1.getText() +”’”;
stt.executeUpdate(SQL);

tblModel.removeRow(row);
stt.close();
kon.close();
}catch(Exception exc){
System.err.println(exc.getMessage());
}
}

o. Membuat koding pada tombol Exit.
this.dispose();

p. Uji Program….

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: