Feb
20
Tutorial membuat Aplikasi Kasir atau Penjualan dengan Java Netbeans (part IV)
IV. Script Aplikasi KasirPada Source code tambahkan code berikut dibawah package app_kasir;
import java.sql.*;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
Selanjut nya tambahkan code berikut dibawah Public class Frame extends javax.swing.JFrame{
Connection c;
ResultSet r;
Statement s;
private Object[][] table_data = null;
private String[] label = {“ID”,”Name”, “Price”, “Amount”, “Total”};
Selanjutnya Tambahkan Code berikut di bawah Public Frame(){
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
|
public Frame(){
initComponents();
OpenConnection();
SetTable();
ClearField();
}
private void OpenConnection(){ // Koneksi ke Database
try {
Class.forName("com.mysql.jdbc.Driver");
c = DriverManager.getConnection("jdbc:mysql://localhost/kasir", "root", ""); // username dan password xampp anda
System.out.println("Connected");
} catch (Exception e) {
System.out.println(e);
}
}
private void SetTable(){ // Set isi tabel
try {
s = c.createStatement();
String sql = "select * from transaksi ";
r = s.executeQuery(sql);
ResultSetMetaData m = r.getMetaData();
int column = m.getColumnCount();
int row = 0;
while(r.next()){
row = r.getRow();
}
table_data = new Object[row][column];
int x = 0;
r.beforeFirst();
while(r.next()){
table_data[x][0] = r.getString("id");
table_data[x][1] = r.getString("namabrg");
table_data[x][2] = r.getString("hargabrg");
table_data[x][3] = r.getString("jumlahbrg");
table_data[x][4] = r.getString("total");
x++;
}
table.setModel(new DefaultTableModel(table_data, label));
} catch (Exception e) {
JOptionPane.showMessageDialog(null, e);
}
}
private void ReadData(){ // Mengambil data dari tabel
int row = table.getSelectedRow();
id.setText((String)table.getValueAt(row, 0));
nama.setText((String)table.getValueAt(row, 1));
harga.setText((String)table.getValueAt(row, 2));
jumlah.setText((String)table.getValueAt(row, 3));
}
private void ClearField(){ // Fungsi untuk mengosongkan semua field dan tabel
nama.setText("");
harga.setText("");
jumlah.setText("1");
id.setText("");
kembali.setText("0");
bayar.setText("0");
total_harga.setText("0");
hitung.setEnabled(false);
update.setEnabled(false);
delete.setEnabled(false);
}
private void SaveData(){ // Menyimpan data ke Database
try {
int a = Integer.parseInt(harga.getText());
int b = Integer.parseInt(jumlah.getText());
String sql = "insert into transaksi set namabrg='"+nama.getText()+"', "
+ "hargabrg='"+harga.getText()+"', jumlahbrg='"+jumlah.getText()+"', "
+ "total='"+a*b+"'";
s.executeUpdate(sql);
s.close();
ClearField();
SetTable();
} catch (SQLException e) {
}
}
private void UpdateData(){ // Update data
try {
int a = Integer.parseInt(harga.getText());
int b = Integer.parseInt(jumlah.getText());
String sql = "update transaksi set namabrg='"+nama.getText()+"', "
+ "hargabrg='"+harga.getText()+"', jumlahbrg='"+jumlah.getText()+"', "
+ "total='"+a*b+"' where id='"+id.getText()+"'";
s.executeUpdate(sql);
s.close();
ClearField();
SetTable();
} catch (SQLException e) {
}
}
private void DeleteData(){ // hapus data dari Database
try {
String sql = "delete from transaksi where id='"+id.getText()+"'";
s.executeUpdate(sql);
s.close();
ClearField();
SetTable();
} catch (SQLException e) {
}
}
public String conversion(int value) // Konversi angka ke huruf
{
String [] bilangan ={"","satu","dua","tiga","empat","lima","enam","tujuh","delapan","sembilan","sepuluh","sebelas"};
String temp=" ";
if (value<12){
temp = " " + bilangan[value];
}
else if(value<20){
temp = conversion(value-10) + " belas";
}
else if(value<100){
temp = conversion(value/10) + " puluh" + conversion(value%10);
}
else if(value<200){
temp = " seratus" + conversion(value-100);
}
else if(value<1000){
temp = conversion(value/100) + " ratus" + conversion(value%100);
}
else if(value<2000){
temp = "seribu"+ conversion(value-1000);
}
else if(value<1000000){
temp = conversion(value/1000) + " ribu" + conversion (value%1000);
}
return temp;
}
/** 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")
|
Selanjutnya kita akan menambahkan action
pada setiap button,table ketika diklik,lihat gambar dibawah
Tambahkan action code pada setiap button, dan table seperti dibawah ini
Selesai !!..
Silahkan run class frame.java tersebut ( klik kanan pada frame.java – run file)
Selamat belajar !!..
Kalau masih bingung, silahkan Download Project dan source code nya disini …
Comments
Post a Comment
terima kasih telah berpartisipasi pada blog kami