LAPORAN PRAKTIKUM PEMOGRAMAN BERBASIS JAVA
Oleh : SHOLAHUDDIN AL-AYUBI 13111019
PROGRAM STUDI TEKNIK INFORMATIKA FAKULTAS TEKNOLOGI INFORMASI UNIVERSITAS MERCU BUANA YOGYAKARTA 2015
Kata Pengantar Puji syukur penulis panjatkan kepada Tuhan Yang Maha Esa yang telah memberikan rahmat dan karunianya, sehingga penulis dapat menyelesaikan Laporan Praktikum Pemrograman Berbasis Java ini. Tugas Praktikum Pemrograman Java ini merupakan salah satu matakuliah yang wajib ditempuh di Fakultas Teknologi Informasi Universitas MercuBuana Yogyakarta. Laporan Praktikum ini disusun sebagai pelengkap Tugas praktik yang telah dilaksanakan lebih kurang 1 Semester ganjil. Dengan selesainya laporan kerja praktek ini tidak terlepas dari bantuan banyak pihak yang telah memberikan masukan-masukan kepada penulis. Penulis menyadari bahwa masih banyak kekurangan dari laporan ini, baik dari materi maupun teknik penyajiannya, mengingat kurangnya pengetahuan dan pengalaman penulis. Oleh karena itu, kritik dan saran yang membangun sangat penulis harapkan.
Yogyakarta,
Januari 2016
Sholahuddin Al-Ayubi 13111019
2
Daftar Isi LAPORAN PRAKTIKUM .................................................................................................................................. 1 Kata Pengantar.............................................................................................................................................. 2 Daftar Isi ........................................................................................................................................................ 3 Daftar Gambar .............................................................................................................................................. 6 BAB I ............................................................................................................................................................ 8 Pengantar Pemrograman Java ....................................................................................................................... 8 1.1.
SOAL .............................................................................................................................................. 8
1.2
PROGRAM ..................................................................................................................................... 8
1.3
SCREENSHOT ................................................................................................................................. 9
1.4
ANALISA PROGRAM ...................................................................................................................... 9
BAB II......................................................................................................................................................... 10 ARRAY ...................................................................................................................................................... 10 2.1
SOAL ............................................................................................................................................ 10
2.2
PROGRAM ................................................................................................................................... 10
2.2.1
Program 2.1 dan 2.2 ............................................................................................................ 10
2.2.2
Program array a = 2 dan b = 3 output c = a+b ; d = a*b; ..................................................... 10
2.2.3
Penjumlahan dan perkalian ................................................................................................ 11
2.3
SCREENSHOT ............................................................................................................................... 13
2.4
ANALISA PROGRAM .................................................................................................................... 14
BAB III ....................................................................................................................................................... 14 Operator ...................................................................................................................................................... 14 3.1
SOAL ............................................................................................................................................ 14
3.2
PROGRAM ................................................................................................................................... 14
3.2.1
Menampilkan Nama, Alamat, Hobi, Hp menggunakan input keyboard ............................. 14
3.2.2
Menghitung Luas Persegi dan Persegi Panjang .................................................................. 16
3.3
SCREENSHOT ............................................................................................................................... 18
3.4
ANALISA PROGRAM .................................................................................................................... 18
BAB IV Seleksi Kondisi ............................................................................................................................ 20 4.1
SOAL ............................................................................................................................................ 20
4.2
PROGRAM ................................................................................................................................... 20 3
4.2.1
Program 4.1 ......................................................................................................................... 20
4.2.2
Program 4.2 ........................................................................................................................ 20
4.2.3
Prgoram dengan ketentuan ................................................................................................ 21
4.3
SCREENSHOT ............................................................................................................................... 23
4.4
ANALISA PROGRAM .................................................................................................................... 24
BAB V .......................................................................................................................................................... 1 Perulangan..................................................................................................................................................... 1 5.1
SOAL .............................................................................................................................................. 1
5.2
PROGRAM ..................................................................................................................................... 1
5.2.1
Melengkapi program ............................................................................................................. 1
5.2.2
Program segitiga bintang ...................................................................................................... 3
5.3
SCREENSHOT ................................................................................................................................. 4
5.4
ANALISA PROGRAM ...................................................................................................................... 6
BAB VI ......................................................................................................................................................... 8 Object Oriented Programing I ....................................................................................................................... 8 6.1
SOAL .............................................................................................................................................. 8
6.2
PROGRAM ..................................................................................................................................... 8
6.3
SCREENSHOT ............................................................................................................................... 10
6.4
ANALISA PROGRAM .................................................................................................................... 10
BAB VII Object Oriented Programing ...................................................................................................... 11 7.1
SOAL ............................................................................................................................................ 11
7.2
PROGRAM ................................................................................................................................... 11
7.2.1 Pengkapi Program ...................................................................................................................... 11 7.2.2 Buat program inheritance ........................................................................................................... 17 7.3
SCREENSHOT ............................................................................................................................... 20
7.4
ANALISA PROGRAM .................................................................................................................... 22
BAB VIII .................................................................................................................................................... 23 Pengantar Pemrograman Java ..................................................................................................................... 23 8.1
SOAL ............................................................................................................................................ 23
8.2
PROGRAM ................................................................................................................................... 23
8.2.1
Melengkapi Program 8.1 - 8.11........................................................................................... 23 4
8.2.2
Membuat mobil .................................................................................................................. 31
8.2.2
Membuat program form sederhana ................................................................................... 33
8.3
SCREENSHOT ............................................................................................................................... 35
8.4
ANALISA PROGRAM .................................................................................................................... 41
BAB IX Event Handler & Sistem Menu .................................................................................................... 42 9.1
SOAL ............................................................................................................................................ 42
9.2
PROGRAM ................................................................................................................................... 42
9.2.1
Program 9.1 Penanganan Text Field ................................................................................... 42
9.2.2
Program 9.2 Penanganan Radio Button.............................................................................. 43
9.2.3
Program 9.3 Penanganan Check Box .................................................................................. 46
9.2.4
Program 9.4 Sistem menu ................................................................................................... 50
9.2.5
Program mengembangkan menu ....................................................................................... 51
9.3
SCREENSHOT ............................................................................................................................... 53
9.4
ANALISA PROGRAM .................................................................................................................... 55
BAB X ........................................................................................................................................................ 56 JDBC & MySQL......................................................................................................................................... 56 10.1
SOAL ............................................................................................................................................ 56
10.2
PROGRAM ................................................................................................................................... 56
10.2.1
Lengkapi Program ............................................................................................................... 56
10.2.2
Kasus Penjualan Tiket Pesawat ........................................................................................... 78
10.3
SCREENSHOT ............................................................................................................................... 92
10.4
ANALISA PROGRAM .................................................................................................................... 97
5
Daftar Gambar 1. 1 Pengantar Java ............................................................................................................................... 9 2. 1 Program 2.1 dan 2.2 ..................................................................................................................... 13 2. 2 Output c=a+b dan d=a*b.............................................................................................................. 13 3. 1 Menampilkan data dengan Input Keyboard ................................................................................ 18 3. 2 Hitung luas Persegi dan Persegi panjang ..................................................................................... 18 4. 1 Program 4.1 .................................................................................................................................. 23 4. 2 Program 4.2 .................................................................................................................................. 23 4. 3 Program dengan ketentuan ......................................................................................................... 24 5. 1 Program 5.1 while .......................................................................................................................... 4 5. 2 Program 5.2 For.............................................................................................................................. 4 5. 3 Program 5.3 Break.......................................................................................................................... 5 5. 4 Program 5.4 Continue .................................................................................................................... 5 5. 5 Program 5.5 Return ........................................................................................................................ 6 5. 6 Program Segitiga bintang ............................................................................................................... 6 6. 1 Program Menghitung luas dan keliling segitiga dan persegi ....................................................... 10 7. 1 Program 7.1 Encapsulation .......................................................................................................... 20 7. 2 Program 7.2 Inheritance .............................................................................................................. 20 7. 3 Program 7.3 Polymorphisme ....................................................................................................... 21 7. 4 Program Inherintance fakultas..................................................................................................... 21 8. 1 Program 8.1 Frame SWT .............................................................................................................. 35 8. 2 Program 8.2 Font.......................................................................................................................... 35 8. 3 Program 8.3 Image ....................................................................................................................... 36 8. 4 Program 8.4 Button ...................................................................................................................... 36 8. 5 Program 8.5 Frame Swing ............................................................................................................ 37 8. 6 Program 8.6 Label ........................................................................................................................ 37 8. 7 Program 8.7 TextField .................................................................................................................. 37 8. 8 Program 8.8 RadioButton............................................................................................................. 38 8. 9 Program 8.8 ComboBox ............................................................................................................... 38 8. 10 Program 8.10 Check Box ............................................................................................................ 38 6
8. 11 Program 8.11 Button .................................................................................................................. 39 8. 12 Program Mobil .......................................................................................................................... 39 9. 1 Program 9.1 Event_1 .................................................................................................................... 53 9. 2 Program 9.2 Event_2 .................................................................................................................... 53 9. 3 Program 9.3 Check Box ................................................................................................................ 53 9. 4 Program 9.4 Sistem Menu............................................................................................................ 54 9. 5 MENU ........................................................................................................................................... 54 10. 1 Program 10.2 Input Table .......................................................................................................... 92 10. 2 Program 10.3 Search dan Update .............................................................................................. 92 10. 3 Program 10.4 Delete .................................................................................................................. 93 10. 4 Tabel Input Member .................................................................................................................. 93 10. 5 Edit Member .............................................................................................................................. 94 10. 6 Hapus Member........................................................................................................................... 95 10. 7 Menu .......................................................................................................................................... 96
7
BAB I Pengantar Pemrograman Java 1.1. SOAL Buatlah program untuk menampilkan Nama, NIM, Alamat, Hobi dan pendapat tentang program java.
1.2
PROGRAM class perkenalan1 { public static void main(String[] args) { System.out.println("Nama : Sholahuddin Al-Ayubi"); System.out.println("NIM : 13111019"); System.out.println("Prodi: Teknik Informatika"); System.out.println("Hobi : Suka Dengan Dunia Web, Jaringan, dan Juga Suka KAMU :D"); } }
8
1.3
SCREENSHOT
1. 1 Pengantar Java
1.4
ANALISA PROGRAM Program Sederhana diatas merupakan program untuk menampilkan suatu text pada pemograman java, lalu pada perintah println merupakan perintah untuk menginput text dan akan ganti baris karena jika tidak ditambahkan “LN” maka text baru tidak akan berganti baris.
9
BAB II ARRAY 2.1
SOAL A. Lengkapi program 2.1 dan 2.2 B. Buat program array dengan ketentuan : a=2 dan b=3 dengan output : c=a+b ; d=a*b; C. Buat program array untuk melakukan penjumlahan dan perkalian
2.2
PROGRAM 2.2.1
Program 2.1 dan 2.2 class duadimensi { public static void main (String [] args) { int[][] x = new int[3][2]; x[0][0] = 7; x[0][1] = 3; x[1][0] = 5; x[1][1] = 6; x[2][0] = 1; x[2][1] = 0; System.out.print("x [0][0] = "+ x[0][0] +" | "); System.out.print("x [0][1] = "+ x[0][1] +" \n"); System.out.print("x [1][0] = "+ x[1][0] +" | "); System.out.print("x [1][1] = "+ x[1][1] +" \n"); System.out.print("x [2][0] = "+ x[2][0] +" | "); System.out.print("x [2][1] = "+ x[2][1] +" \n"); } }
2.2.2
Program array a = 2 dan b = 3 output c = a+b ; d = a*b; class soal2 { public static void main(String [] args) { int[] a = new int[1]; int[] b = new int[1]; int[] c = new int[1]; int[] d = new int[1]; a[0] = 2; b[0] = 3; 10
c[0] = a[0] + b[0]; d[0] = a[0] * b[0]; //Penjumlahan System.out.println("Hasil Penjumlahan dari 2+3 = "+c[0]); //perkalian System.out.println("Hasil Perkalian dari 2X3 = "+d[0]); } } 2.2.3
Penjumlahan dan perkalian class soal3 { public static void main (String [] args) { int[][] x = new int[3][2]; int[] penjumlahan = new int[1]; int[] perkalian = new int [1];
x[0][0] = 7; x[0][1] = 3; x[1][0] = 5; x[1][1] = 6; x[2][0] = 1; x[2][1] = 0;
System.out.print("x [0][0] = "+ x[0][0] +" | "); System.out.print("x [0][1] = "+ x[0][1] +" \n"); System.out.print("x [1][0] = "+ x[1][0] +" | "); System.out.print("x [1][1] = "+ x[1][1] +" \n"); System.out.print("x [2][0] = "+ x[2][0] +" | "); 11
System.out.print("x [2][1] = "+ x[2][1] +" \n");
//Penjumlahan penjumlahan[0] = x[0][0]+x[0][1]+x[1][0]+x[1][1]+x[2][0]+x[2][1]; System.out.print("Penjumlahan = "+penjumlahan[0]+ "\n");
//Perkalian perkalian[0] = x[0][0]*x[0][1]*x[1][0]*x[1][1]*x[2][0]*x[2][1]; System.out.print("Perkalian = "+perkalian[0]+ "\n"); } }
12
2.3
SCREENSHOT
2. 1 Program 2.1 dan 2.2
2. 2 Output c=a+b dan d=a*b
13
2. 3 Penjumlahan dan Perkalian
2.4
ANALISA PROGRAM Mendeklarasikan variabel array dengan tipe data yang diinginkan dengan cara yang hampir sama dengan variabel biasa. Misalnya untuk mendeklarasikan variabel bertipe integer, dapat dilakukan dengan cara :int [ ] bilangan; atau int bilangan [ ];
BAB III Operator 3.1
SOAL A. Menampilkan Nama, Alamat, Hobi, Hp dengan menggunakan input keyboard B. Buat program menghitung luas persegi dan persegi panjang
3.2
PROGRAM 3.2.1 Menampilkan Nama, Alamat, Hobi, Hp menggunakan input keyboard import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.IOException; //import package java.io 14
public class Input_keyboard { public static void main (String[]args) { BufferedReader input = new BufferedReader (new InputStreamReader (System.in)); String Nama = ""; String Alamat = ""; String Hobi = ""; String Hp = "";
try { System.out.println("===Form Registrasi==="); System.out.println(); System.out.println("Masukkan Nama Anda:"); Nama = input.readLine(); System.out.println("Masukkan Alamat Anda:"); Alamat = input.readLine(); System.out.println("Masukkan Hobi Anda:"); Hobi = input.readLine(); System.out.println("Masukkan No HP Anda:"); Hp = input.readLine(); System.out.println(); System.out.println("++Selamat Datang++"); System.out.println("Kamu Terdaftar sebagai "+Nama); System.out.println("Alamat Kamu "+Alamat); System.out.println("Hobi Kamu "+Hobi); System.out.println("No HP Kamu "+Hp); } catch (IOException e) 15
{ System.out.println("Errrooorrr!!"); } } } 3.2.2 Menghitung Luas Persegi dan Persegi Panjang import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.IOException; //import package java.io public class Input_Keyboard2 { public static void main (String[]args) { BufferedReader input = new BufferedReader (new InputStreamReader (System.in)); int p,l,s; try { System.out.println("Menghitung Luas Persegi"); System.out.println(); System.out.println("Masukkan Nilai Sisi :"); s = Integer.parseInt(input.readLine()); float luasp=((float)(s*s)); System.out.println("Luas Persegi = "+luasp+"cm"); System.out.println(); System.out.println(); System.out.println("Menghitung Luas Persegi Panjang"); System.out.println("Masukkan Panjang = "); p = Integer.parseInt (input.readLine()); System.out.println("Masukkan Lebar = "); l = Integer.parseInt (input.readLine()); float luaspp=((float)(p*l)); System.out.println("Luas Persegi Panjang = "+luaspp+"cm"); } catch (IOException e) { 16
System.out.println("Errrooorrr!!"); } } }
17
3.3
SCREENSHOT
3. 1 Menampilkan data dengan Input Keyboard
3. 2 Hitung luas Persegi dan Persegi panjang
3.4
ANALISA PROGRAM Operator menggunakan tanda sama dengan (=) untuk mengisi sebuah nilai dalam suatu variabel. Beberapa operator aritmatika antara lain: * : untuk perkalian + : untuk penjumlahan 18
- : untuk pengurangan / : untuk pembagian % : untuk sisa pembagian (modulus)
19
BAB IV Seleksi Kondisi 4.1
SOAL A. Lengkapi program 4.1 dan 4.2 B. Buat program menentukan nilai Jika nilai 85-100 -> Nilai = a Jika nilai 70-84 -> Nilai = b Jika nilai 55-69 -> Nilai = c Jika nilai 30-54 -> Nilai = d Jika nilai 0-29 -> Nilai = e
4.2
PROGRAM 4.2.1 Program 4.1 class program4pertama { public static void main(String[]args) { int a=10,b=a/2; if(b>5){ System.out.println("Pernyataan1 dieksekusi"); } else { System.out.println("pernyataan2 dieksekusi"); System.out.println("pernyataan3 dieksekusi"); } } } 4.2.2
Program 4.2 public class switch_case { public static void main (String [] args) { int bulan = 1; switch(bulan){ case 1: System.out.println("Januari"); break; case 2: 20
System.out.println("Februari"); break; case 3: System.out.println("Maret"); break; default: System.out.println("April"); } } } 4.2.3 Prgoram dengan ketentuan import java.io.*;
public class input_nilai { public static void main (String [] Maurer) throws IOException { float Nilai; InputStreamReader inStream = new InputStreamReader (System.in); BufferedReader darikeyboard = new BufferedReader (inStream); String InputData; { System.out.println("Masukkan Nilai Anda"); } InputData = darikeyboard.readLine(); Nilai = Integer.parseInt(InputData); if (Nilai >=85) { 21
System.out.println("Nilai Anda A"); } else if (Nilai >=70) { System.out.println("Nilai Anda B"); } else if (Nilai >=55) { System.out.println("Nilai Anda C"); }
else if (Nilai >=30) { System.out.println("Nilai Anda D"); } else { System.out.println("Nilai anda E"); } } }
22
4.3
SCREENSHOT
4. 1 Program 4.1
4. 2 Program 4.2
23
4. 3 Program dengan ketentuan
4.4
ANALISA PROGRAM Statement if-else digunakan apabila ingin mengeksekusi sebuah statement dengan kondisi true dan statement yang lain dengan kondisi false.
24
BAB V Perulangan 5.1
SOAL A. Lengkapi program 5.1, 5.2, 5.3, 5.4 dan 5.5 B. Buat program menampilkan segitiga bintang
5.2
PROGRAM 5.2.1 Melengkapi program 5.2.1.1 Program 5.1 class iterasiwhile2 { public static void main (String[]masal) { int n = 1; System.out.println("Sebelum while"); while(n>=0) { System.out.println("Nilai n :"+n); n--; } System.out.println("Setelah While"); } }
5.2.1.2 Program 5.2 public class whilefor { public static void main (String[]masal) { System.out.println("Sebelum For"); for (int x=0;x<10;x++) { if (x==4) break; System.out.println("Nilai x :"+x); } System.out.println("Setelah for"); 1
} }
5.2.1.3 Program 5.3 public class break { public static void main(String[]masal) { System.out.println("Sebelum for"); for(int i=0;i<7;i++) { System.out.println("Nilai i :"+i); } System.out.println("Setelah for"); } }
5.2.1.4 Program 5.4 class iterasiwhile2 { public static void main (String[]masal) { int n = 1; System.out.println("Sebelum while"); while(n>=0) { System.out.println("Nilai n :"+n); n--; } System.out.println("Setelah While"); } } 5.2.1.5 Program 5.5 class iterasireturn { public static void main(String[]masal) { int x = 10,y; 2
label1: while(x-->0) { y=1; while(x++<10) { if(y>x) { System.out.println(); continue label1; } System.out.println(x*y +" "); } } } } 5.2.2 Program segitiga bintang public class segitigabintang { public static void main(String[]a) { for(int i=5;i>=0;i--) { for(int b=i;b>=1;b--) { System.out.print(" * "); } System.out.println(); } } }
3
5.3
SCREENSHOT
5. 1 Program 5.1 while
5. 2 Program 5.2 For
4
5. 3 Program 5.3 Break
5. 4 Program 5.4 Continue
5
5. 5 Program 5.5 Return
5. 6 Program Segitiga bintang
5.4
ANALISA PROGRAM Struktur perulangan dibagi menjadi tiga, Yaitu for, while, dan do while. Struktur for pada umumnya digunakan untuk melakukan perulangan yang banyaknya sudah pasti atau sudah diketahui sebelumya. Struktur while adalah jenis perulangan yang mendefinisikan kondisi di awal blok. Ini artinya apabila kondisi tidak terpenuhi (false) maka proses 6
perulangan pun tidak akan pernah dilakukan. Struktur do-while sebenarnya mirip dengan struktur while. Perbedaannya hanya terletak pada penempatan kondisinya saja.
7
BAB VI Object Oriented Programing I 6.1
SOAL Buatlah program degnan ketentuan menghitung luas dan keliling segitga dan persegi, ada konstruktor dan method Overloading
6.2
PROGRAM class bangun { int sisi=21; int alas=9; int tinggi=14; int LuasSegitiga,KelilingSegitiga,LuasPersegi,KelilingPersegi;
bangun() { LuasSegitiga=(alas*tinggi)/2;KelilingSegitiga=(alas+tinggi+tinggi); LuasPersegi=sisi*sisi; KelilingPersegi=sisi*4; } bangun(int w, int w2) { LuasSegitiga=w; KelilingSegitiga=w; LuasPersegi=w2; KelilingPersegi=w2; } void cetak() { if(LuasSegitiga==0 && LuasPersegi==0) return; System.out.println("Luas Segitiga:"+LuasSegitiga); System.out.println("Luas Persegi:"+LuasPersegi); 8
} void cetak2() { if(LuasSegitiga==0 & LuasPersegi==0) return; System.out.println("Keliling Segitiga:"+KelilingSegitiga); System.out.println("Keliling Persegi:"+KelilingPersegi); }} class hitung{ public static void main(String[] Masal){ bangun b,y; b= new bangun(); y= new bangun(); b.cetak(); y.cetak2(); } }
9
6.3
SCREENSHOT
6. 1 Program Menghitung luas dan keliling segitiga dan persegi
6.4
ANALISA PROGRAM Pemrograman berorientasi objek merupakan paradigma pemrograman yang berorientasikan kepada objek. Semua data dan fungsi di dalam paradigma ini dibungkus dalam class atau objek. Bandingkan dengan logika pemrograman terstruktur.
10
BAB VII Object Oriented Programing 7.1
SOAL A. Lengkapi program 7.1, 7.2, 7.3 B. Buat program Interitance
7.2
PROGRAM 7.2.1 Pengkapi Program
7.2.1.1 Program 7.1 Encapsulation public class segitiga { private double alas; private double tinggi;
public segitiga() { alas=0; tinggi=0; } private double luas(double a, double t) { return (a*t)/2; } private double getLuas(double luas) { return luas(alas,tinggi); } 11
public void setAlas(double alas) { this.alas=alas; } public double getAlas(double alas) { return alas; } public void setTinggi(double tinggi) { this.tinggi=tinggi; } public double getTinggi(double tinggi) { return tinggi; } public static void main(String[]Riska) { segitiga s=new segitiga(); s.setAlas(4); s.setTinggi(5); System.out.println("contoh program enkapsulasi"); System.out.println("luas segitiga = "+s.getLuas(0)); } } 12
13
7.2.1.2 Program 7.2 Inheritance class kendaraan { String nama; int roda; public kendaraan(String nm, int rd) { nama=nm; roda=rd; } public void cetak() { System.out.println("Jenis Kendaraan = " +nama); System.out.println("Jumlah Roda " +roda+ " Buah"); } } class mobil extends kendaraan { public mobil(String nama, int roda) { super (nama,roda); } } class truck extends kendaraan { 14
public truck(String nama, int roda) { super(nama,roda); } } class sepedamotor extends kendaraan { public sepedamotor(String nama, int roda) { super(nama,roda); } } class jenis_kendaraan { public static void main (String[]args) { mobil m = new mobil("Mobil", 4); truck t = new truck("Truck", 6); sepedamotor sm = new sepedamotor("sepedamotor",2); m.cetak(); t.cetak(); sm.cetak(); } }
15
7.2.1.3 Program 7.3 polymorphisme public class lingkaran { private double r; public lingkaran(double r) { this.r=r; } public lingkaran() { r=3.0; } public double keliling() { return 2*3.14*r; } public double luas() { return 3.14*r*r; } public static void main(String[]args) { lingkaran n; n = new lingkaran(); System.out.println("Contoh polimorfisme"); 16
System.out.println("luas = "+n.luas()+"cm"); System.out.println("keliling = "+n.keliling()+"cm"); } }
7.2.2 Buat program inheritance class fakultas { String namfak; int jumlah; String prodi; public fakultas(String nm, int jml, String pro) { namfak=nm; jumlah=jml; prodi=pro; } public void cetak() { System.out.println("Nama Fakultas = "+namfak); System.out.println("jumlah prodi = "+jumlah+"Prodi"); System.out.println("program study = "+prodi); } } class TI extends fakultas { 17
public TI(String namfak, int jumlah, String prodi) { super (namfak,jumlah,prodi); } } class ekonomi extends fakultas { public ekonomi(String namfak, int jumlah, String prodi) { super (namfak,jumlah,prodi); } } class psikologi extends fakultas { public psikologi(String namfak, int jumlah, String prodi) { super (namfak,jumlah,prodi); } } class ilkom extends fakultas { public ilkom(String namfak, int jumlah, String prodi) { super (namfak,jumlah,prodi); } 18
} class agro extends fakultas { public agro(String namfak, int jumlah, String prodi) { super (namfak,jumlah,prodi); } } class jenis_fakultas { public static void main(String[]args) { TI m = new TI("TI", 2, "TI DAN SI"); ekonomi t = new ekonomi("Ekonomi", 2, "Manajement dan Akutansi"); psikologi sm = new psikologi("Psikologi", 3, "Psikologi, Magister psik sains dan magsiter profesi psik"); ilkom ik = new ilkom("Ilmu Komunikasi", 1 , "Ilmu Komunikasi"); agro hm = new agro("Agro",2,"Peternakan dan agro industri"); m.cetak(); t.cetak(); sm.cetak(); ik.cetak(); hm.cetak(); } } 19
7.3
SCREENSHOT
7. 1 Program 7.1 Encapsulation
7. 2 Program 7.2 Inheritance
20
7. 3 Program 7.3 Polymorphisme
7. 4 Program Inherintance fakultas
21
7.4
ANALISA PROGRAM Inheritance atau pewarisan adalah suatu pewarisan sifat dari suatu class kepada subclassnya. Dengan adanya pewarisan ini, maka kita tidak perlu menuliskan kode program dari awal, cukup menuliskan spesifikasi dari class tersebut. Didalam inheritance terdapat isitilah superclass dan subclass. Superclass adalah class induk atau class orang tua, sedangkan subclass adalah class anak. Semua property, methode, ataupun konstruktor yang dimiliki oleh superclass jika diwariskan, maka subclass memiliki semua yang dimiliki oleh superclass. Untuk melakukan pewarisan / inheritance sebuah class, kita harus menggunakan kata kunci extends.
22
BAB VIII Pengantar Pemrograman Java 8.1
SOAL Buatlah program untuk menampilkan Nama, NIM, Alamat, Hobi dan Pendapat Pribadi Tentang Pemograman Java.
8.2
PROGRAM 8.2.1 Melengkapi Program 8.1 - 8.11 8.2.1.1 Program 8.1 Frame AWT import javax.swing.*; class ContohFrame extends JFrame { ContohFrame() { setTitle("Frame"); setLocation(300,100); setSize(300,200); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(true); } public static void main(String[]args) { ContohFrame al=new ContohFrame(); } }
8.2.1.2 Program 8.2 Font import java.awt.*; public class ContohFont extends Frame { ContohFont (){ setTitle("Contoh Font"); // Judul window } public void paint(Graphics g){ g.drawRect(20,40,250,60); g.drawString("Welcome To",30,60); g.setFont(new Font("Calibri",Font.BOLD, 14)); 23
g.drawString("Universitas Mercubuana Yogyakarta ",30,80); } public boolean handleEvent(Event evt) { if (evt.id == Event.WINDOW_DESTROY) System.exit(0); return false; } public static void main(String[]args) { Frame f = new ContohFont(); f.resize(300,150); f.show(); } }
8.2.1.3 Program 8.3 Image import java.awt.*; public class ContohImage extends Frame { String nmFILE="umby.jpg"; Image gb=Toolkit.getDefaultToolkit().getImage(nmFILE); public void paint(Graphics g){ setTitle("Contoh Image"); g.setColor(Color.magenta); g.drawString("Welcome !!",85,180); g.drawImage(gb,60,50,this); } public boolean handleEvent(Event evt) { if (evt.id == Event.WINDOW_DESTROY) System.exit(0); return false; } public static void main(String[]args) { Frame f = new ContohImage(); 24
f.resize(420,370); f.show(); } }
8.2.1.4 Program 8.4 Button import java.awt.*; public class ContohButton extends Frame { public ContohButton (){ setTitle("Contoh Button"); setLayout(new FlowLayout()); add(new Button("Yellow")); add(new Button("Blue")); add(new Button("Orange")); add(new Button("Cyan")); add(new Button("Pink")); add(new Button("Red")); add(new Button("White")); } public boolean action(Event evt, Object arg) { if (arg.equals("Yellow")) setBackground(Color.yellow); else if (arg.equals("Blue")) setBackground(Color.red); else if (arg.equals("Orange")) setBackground(Color.orange); else if (arg.equals("Cyan")) setBackground(Color.cyan); else if (arg.equals("Pink")) setBackground(Color.pink); else if (arg.equals("Red")) setBackground(Color.red); else if (arg.equals("White")) setBackground(Color.white); else return false; repaint(); return true; } public static void main(String[]args) { Frame f = new ContohButton(); f.resize(420,200); f.show(); } } 25
26
8.2.1.5 Program 8.5 Frame Swing import javax.swing.*; class ContohFrame extends JFrame { ContohFrame() { setTitle("Frame"); setLocation(300,100); setSize(300,200); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(true); } public static void main(String[]args) { ContohFrame al=new ContohFrame(); } }
8.2.1.6 Program 8.6 Label import javax.swing.*; class ContohLabel extends JFrame { JLabel labelContoh=new JLabel("Label"); ContohLabel() { setTitle("Contoh Label"); setLocation(300,100); setSize(250,100); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } void GUI() { getContentPane().setLayout(null); getContentPane().add(labelContoh);labelContoh.setBounds(10,10,80,20); setVisible(true); // Menampilkan Frame } public static void main(String[]args){ ContohLabel al=new ContohLabel(); al.GUI(); 27
} }
8.2.1.7 Program 8.7 Text Field import javax.swing.*; class ContohTextField extends JFrame { JTextField textContoh=new JTextField(40); ContohTextField() { setTitle("Text Field"); setLocation(300,100); setSize(240,120); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } void GUI() { getContentPane().setLayout(null); getContentPane().add(textContoh);textContoh.setBounds(10,10,80,20); setVisible(true); // Menampilkan Frame } public static void main(String[]args){ ContohTextField al=new ContohTextField(); al.GUI(); } }
8.2.1.8 Program 8.8 Radio Button import javax.swing.*; class ContohRadioButton extends JFrame { JRadioButton radioContoh1 = new JRadioButton("Radio 1"); JRadioButton radioContoh2 = new JRadioButton("Radio 2"); ButtonGroup groupContoh = new ButtonGroup(); ContohRadioButton() { setTitle("Radio Button"); setLocation(300,100); setSize(300,200); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 28
} void GUI() { getContentPane().setLayout(null); getContentPane().add(radioContoh1);radioContoh1.setBounds(10,10,80,20); getContentPane().add(radioContoh2);radioContoh2.setBounds(10,30,80,20); groupContoh.add(radioContoh1); groupContoh.add(radioContoh2); setVisible(true); // Menampilkan Frame } public static void main(String[]args){ ContohRadioButton al=new ContohRadioButton(); al.GUI(); } }
8.2.1.9
Program 8.9 Combo Box import javax.swing.*; class ContohComboBox extends JFrame { String[] Contoh={"Combo Box 1","Combo Box 2","Combo Box 3"}; JComboBox comboContoh = new JComboBox (Contoh); ContohComboBox() { setTitle("Contoh Combo Box"); setLocation(300,100); setSize(300,200); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } void GUI() { getContentPane().setLayout(null); getContentPane().add(comboContoh);comboContoh.setBounds(10,10,120,20); setVisible(true); // Menampilkan Frame } public static void main(String[]args){ ContohComboBox al=new ContohComboBox(); 29
al.GUI(); } }
8.2.1.10 Program 8.10 Check Box import javax.swing.*; class ContohCheckBox extends JFrame { JCheckBox checkContoh1=new JCheckBox("Check Box 1"); JCheckBox checkContoh2=new JCheckBox("Check Box 2"); ButtonGroup groupContoh = new ButtonGroup(); JPanel panel1=new JPanel(); ContohCheckBox() { setTitle("Contoh Check Box"); setLocation(300,100); setSize(300,200); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } void GUI() { getContentPane().setLayout(null); getContentPane().add(checkContoh1);checkContoh1.setBounds(10,10,120,20); getContentPane().add(checkContoh2);checkContoh2.setBounds(10,40,120,20); groupContoh.add(checkContoh1); groupContoh.add(checkContoh2); setVisible(true); // Menampilkan Frame } public static void main(String[]args){ ContohCheckBox al=new ContohCheckBox(); al.GUI(); } }
8.2.1.11 Program 8.11Button import javax.swing.*; class ContohhButton extends JFrame { JButton buttonContoh=new JButton("Button"); 30
ContohhButton() { setTitle("Contoh Tombol Button"); setLocation(300,100); setSize(300,200); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } void GUI() { getContentPane().setLayout(null); getContentPane().add(buttonContoh);buttonContoh.setBounds(10,10,100,20); setVisible(true); // Menampilkan Frame } public static void main(String[]args){ ContohhButton al=new ContohhButton(); al.GUI(); } }
8.2.2 Membuat mobil import java.awt.*; public class mobil extends Frame { public mobil() { setTitle("Contoh Mobil"); } public void paint(Graphics g) { g.setColor(Color.black); //kotak kap depan 31
g.fillRect(95,90,45,20); //kotak kap bawah depan g.drawRect(95,110,45,25); //body atas g.drawRect(140,53,150,57); //roda depan g.fillOval(107,123,25,25); //roda belakang g.fillOval(235,123,25,25); //body bawah g.drawRect(140,110,150,25); //jendela depan g.fillRect(140,53,30,57); //jendela tengah g.fillRect(190,53,30,57); //jendela belakang g.fillRect(249,53,30,57);
g.drawLine(65,150,450,150); }
public boolean handleEvent(Event evt) { if(evt.id == Event.WINDOW_DESTROY) System.exit(0); 32
return false; }
public static void main (String[]args) { Frame f = new mobil(); f.resize(500,350); f.show(); } }
8.2.2 Membuat program form sederhana import javax.swing.*; import java.awt.event.*; class form_mhs extends JFrame // pewarisan { JLabel labelContoh=new JLabel("NIM"); //membuat object labelContoh JTextField textfName=new JTextField(10); JLabel labelName=new JLabel("Name"); JTextField textfName1=new JTextField(30); JLabel labelAddress=new JLabel("Address"); JTextField textfName2=new JTextField(30); JLabel labelGender=new JLabel("Gender"); JRadioButton radioMale=new JRadioButton("Male"); JRadioButton radioFemale=new JRadioButton("Female"); ButtonGroup groupGender=new ButtonGroup(); JLabel labelClass=new JLabel("Class"); String[] Contoh={"morning","evening","extention"}; JComboBox comboContoh=new JComboBox(Contoh); JButton buttonSAVE=new JButton("SAVE"); JButton buttonUPDATE=new JButton("UPDATE"); JButton buttonEXIT=new JButton("EXIT"); form_mhs() 33
{ setTitle("Form Data Mahasiswa"); //judul di frame setLocation(300,100); //x,y setSize(300,250); //panjang,lebar //mengaktifkan button close di frame setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } void GUI() { //mengatur letak komponen, null=koordinat manual getContentPane().setLayout(null); // meletakkan object labelContoh di Frame getContentPane().add(labelContoh); labelContoh.setBounds(10,10,80,20); // x,y,lebar,tinggi getContentPane().add(textfName1); textfName1.setBounds(95,35,100,20); getContentPane().add(labelName);labelName.setBounds(10,35,70,20); getContentPane().add(textfName); textfName.setBounds(95,10,100,20); getContentPane().add(labelAddress);labelAddress.setBounds(10,60,70,20); getContentPane().add(textfName2); textfName2.setBounds(95,60,130,20); getContentPane().add(labelGender); labelGender.setBounds(10,90,70,20); getContentPane().add(radioMale); radioMale.setBounds(90,90,60,20); getContentPane().add(radioFemale); radioFemale.setBounds(160,90,80,20); groupGender.add(radioMale); groupGender.add(radioFemale); getContentPane().add(labelClass);labelClass.setBounds(10,115,70,20); getContentPane().add(comboContoh); comboContoh.setBounds(95,115,100,20); getContentPane().add(buttonSAVE); buttonSAVE.setBounds(10,150,80,20); getContentPane().add(buttonUPDATE); buttonUPDATE.setBounds(100,150,80,20); getContentPane().add(buttonEXIT);buttonEXIT.setBounds(190,150,80,20); setVisible(true); //menampilkan frame } public static void main(String[]args) { form_mhs al=new form_mhs(); //membuat object baru al.GUI(); // memanggil method GUI } }
34
8.3 SCREENSHOT
8. 1 Program 8.1 Frame SWT
8. 2 Program 8.2 Font
35
8. 3 Program 8.3 Image
8. 4 Program 8.4 Button
36
8. 5 Program 8.5 Frame Swing
8. 6 Program 8.6 Label
8. 7 Program 8.7 TextField
37
8. 8 Program 8.8 RadioButton
8. 9 Program 8.8 ComboBox
8. 10 Program 8.10 Check Box
38
8. 11 Program 8.11 Button
8. 12 Program Mobil
39
8. 13 Data Mahasiswa
40
8.4
ANALISA PROGRAM Program ini merupakan program sederhana pada java untuk membuat Halaman Input Form, Insert Gambar, dan Membuat Sebuah Gambar Mobil
41
BAB IX Event Handler & Sistem Menu 9.1
SOAL A. Lengkapi program 9.1 -9.5 B. kembangkan program
9.2
PROGRAM 9.2.1
Program 9.1 Penanganan Text Field import javax.swing.*; import java.awt.event.*; class Event_1 extends JFrame { JLabel labelName=new JLabel("Name"); JTextField textfName=new JTextField(30); JTextArea textaView = new JTextArea(); JButton buttonINPUT=new JButton("INPUT"); JButton buttonEXIT=new JButton("EXIT"); Event_1() { setTitle("Event_1"); setLocation(300,100); setSize(300,200); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } void GUI() { getContentPane().setLayout(null); getContentPane().add(labelName);labelName.setBounds(10,10,70,20); getContentPane().add(textfName); textfName.setBounds(90,10,130,20); getContentPane().add(buttonINPUT); buttonINPUT.setBounds(10,40,70,20); getContentPane().add(buttonEXIT);buttonEXIT.setBounds(10,65,70,20); 42
getContentPane().add(textaView); textaView.setBounds(90,40,150,50); setVisible(true); } void Perintah() { buttonINPUT.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { textaView.append(textfName.getText()); } }); buttonEXIT.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { System.exit(0); } }); } public static void main(String[]args) { Event_1 sq=new Event_1(); sq.GUI(); sq.Perintah(); } } 9.2.2
Program 9.2 Penanganan Radio Button import javax.swing.*; 43
import java.awt.event.*; class Event_2 extends JFrame { JLabel labelGender=new JLabel("Gender"); JRadioButton radioMale=new JRadioButton("Male"); JRadioButton radioFemale=new JRadioButton("Female"); ButtonGroup groupGender=new ButtonGroup(); JTextArea textaView = new JTextArea(); JButton buttonINPUT=new JButton("INPUT"); JButton buttonEXIT=new JButton("EXIT"); Event_2() { setTitle("Event_2"); setLocation(300,100); setSize(270,150); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } void GUI() { getContentPane().setLayout(null); getContentPane().add(labelGender); labelGender.setBounds(10,10,70,20); getContentPane().add(radioMale); radioMale.setBounds(90,10,60,20); getContentPane().add(radioFemale); radioFemale.setBounds(160,10,80,20); groupGender.add(radioMale); groupGender.add(radioFemale); 44
getContentPane().add(buttonINPUT); buttonINPUT.setBounds(10,40,70,20); getContentPane().add(buttonEXIT);buttonEXIT.setBounds(10,65,70,20); getContentPane().add(textaView); textaView.setBounds(90,40,150,50); setVisible(true); } void Perintah() { buttonINPUT.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if(radioMale.isSelected()==true) { textaView.append(radioMale.getText()); } else { textaView.append(radioFemale.getText()); } } }); buttonEXIT.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { 45
System.exit(0); } }); } public static void main(String[]args) { Event_2 sq=new Event_2(); sq.GUI(); sq.Perintah(); } } 9.2.3
Program 9.3 Penanganan Check Box import javax.swing.*; import java.awt.event.*; class Event_3 extends JFrame { JLabel labelClass=new JLabel("Class"); JCheckBox Morning=new JCheckBox("Morning"); JCheckBox Evening=new JCheckBox("Evening"); JCheckBox Extension=new JCheckBox("Extension"); JTextArea textaView = new JTextArea(); JButton buttonINPUT=new JButton("INPUT"); JButton buttonEXIT=new JButton("EXIT"); Event_3() { 46
setTitle("Event_3"); setLocation(300,100); setSize(270,180); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } void GUI() { getContentPane().setLayout(null); getContentPane().add(labelClass);labelClass.setBounds(10,10,70,20); getContentPane().add(Morning);Morning.setBounds(90,10,100,20); getContentPane().add(Evening);Evening.setBounds(90,30,100,20); getContentPane().add(Extension);Extension.setBounds(90,50,100,20); getContentPane().add(buttonINPUT); buttonINPUT.setBounds(10,80,70,20); getContentPane().add(buttonEXIT);buttonEXIT.setBounds(10,105,70,20); getContentPane().add(textaView); textaView.setBounds(90,80,150,50); setVisible(true); } void Perintah() {buttonINPUT.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if(Morning.isSelected()==true) { textaView.append(Morning.getText()+"\n"); 47
} if(Evening.isSelected()==true) { textaView.append(Evening.getText()+"\n"); } if(Extension.isSelected()==true) { textaView.append(Extension.getText()+"\n"); } } }); buttonEXIT.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { System.exit(0); } }); } public static void main(String[]args) { Event_3 sq=new Event_3(); sq.GUI(); sq.Perintah(); } 48
}
49
9.2.4
Program 9.4 Sistem menu import javax.swing.*; import java.awt.event.*; class Event_4 extends JFrame { JMenuBar menuContoh=new JMenuBar(); JMenu File=new JMenu("File"); JMenu OpenSubmenu=new JMenu("Open"); JMenuItem Mhs=new JMenuItem("Data Mahasiswa"); JMenuItem Exit=new JMenuItem("Exit"); JMenu About=new JMenu("About"); Event_4() { setTitle("Event_4"); setLocation(300,100); setSize(300,220); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } void GUI() { setJMenuBar(menuContoh); menuContoh.add(File); File.add(OpenSubmenu); OpenSubmenu.add(Mhs); File.add(Exit); 50
menuContoh.add(About); setVisible(true); } public static void main(String[]args) { Event_4 sq=new Event_4(); sq.GUI(); } } 9.2.5
Program mengembangkan menu import javax.swing.*; import java.awt.event.*; class Event_5 extends JFrame { JMenuBar menuContoh=new JMenuBar(); JMenu File=new JMenu("File"); JMenu OpenSubmenu=new JMenu("Open"); JMenuItem Mhs=new JMenuItem("Data Mahasiswa"); JMenuItem Exit=new JMenuItem("Exit"); JMenu About=new JMenu("About"); JMenuItem AboutDialog=new JMenuItem("About Us"); Event_3 InputData=new Event_3(); Event_5() { setTitle("Event_5"); setLocation(300,100); setSize(500,350); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } void GUI() { setJMenuBar(menuContoh); menuContoh.add(File); File.add(OpenSubmenu); OpenSubmenu.add(Mhs); 51
File.add(Exit); menuContoh.add(About); About.add(AboutDialog); setVisible(true); } void Perintahku() { Mhs.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { InputData.GUI(); InputData.Perintah(); } }); Exit.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { System.exit(0); } }); AboutDialog.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { JOptionPane.showMessageDialog(null," Program ~ By :AL
http://www.sholahudd.in ","About",JOptionPane.INFORMATION_MESSAGE); } }); } public static void main(String[]args) { Event_5 sq=new Event_5(); sq.GUI(); sq.Perintahku(); } }
52
9.3
SCREENSHOT
9. 1 Program 9.1 Event_1
9. 2 Program 9.2 Event_2
9. 3 Program 9.3 Check Box
53
9. 4 Program 9.4 Sistem Menu
9. 5 MENU
54
9.4
ANALISA PROGRAM Program ini Merupakan program sederhana java untuk menampilkan menu dan pemanggilan pada menu lainnya
55
BAB X JDBC & MySQL 10.1 SOAL A. Lengkapi program 10.1 – 10.5 B. Membuat Database dan Table Dengan Tema Kasus Penjualan Tiket Pesawat 10.2 PROGRAM 10.2.1 Lengkapi Program 10.2.1.1 Program 10.1 Koneksi Database import java.sql.*; class KoneksiMySQL { String url, usr, pwd, dbn; public KoneksiMySQL() { dbn = "data_mahasiswa"; url = "jdbc:mysql://localhost/" + dbn; usr = "root"; pwd = ""; } public Connection getConnection() { Connection con = null; try { Class.forName("com.mysql.jdbc.Driver"); con = DriverManager.getConnection(url, usr,pwd); System.out.println("Ok : Driver Ditemukan"); } catch (ClassNotFoundException e) 56
{ System.out.println("Error: Driver Tidak Ditemukan \n" +e+ "\n"); } catch (SQLException e) { System.out.println("Error: Tidak Bisa Koneksi Ke Database \n" +e+ "\n"); } return con; }
private void PembuatDialog() { System.out.println("Created By : DnD ~ http://www.dnd-sq.tk"); System.exit(0); }
public static void main(String[] args) { KoneksiMySQL dnd = new KoneksiMySQL(); dnd.getConnection(); dnd.PembuatDialog(); } }
10.2.1.2 Program 10.2 Input Table import java.awt.*; import javax.swing.*; import java.awt.event.*; import java.io.*; 57
import java.sql.*;
class Input_Mahasiswa extends JFrame { JLabel labelNIM=new JLabel("NIM"); JTextField textNIM=new JTextField(30); JLabel labelName=new JLabel("Name"); JTextField textName=new JTextField(30); JLabel labelAddress=new JLabel("Address"); JTextField textAddress=new JTextField(50); JLabel labelGender=new JLabel("Gender"); JButton buttonSAVE=new JButton("SAVE"); JButton buttonEXIT=new JButton("EXIT"); JRadioButton radioMale=new JRadioButton("Male"); JRadioButton radioFemale=new JRadioButton("Female"); ButtonGroup groupGender=new ButtonGroup(); JLabel labelClass=new JLabel("Class"); String[] GClass={"Morning","Evening","Extension"}; JComboBox comboClass=new JComboBox(GClass);
Input_Mahasiswa() { setTitle("Data Mahasiswa"); setLocation(300,100); setSize(300,200); 58
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); }
void GUI() { getContentPane().setLayout(null); getContentPane().add(labelNIM); labelNIM.setBounds(10,10,70,20); getContentPane().add(textNIM); textNIM.setBounds(80,10,110,20);
getContentPane().add(labelName);labelName.setBounds(10,30,70,20); getContentPane().add(textName); textName.setBounds(80,30,110,20);
getContentPane().add(labelAddress);labelAddress.setBounds(10,50,70,20); getContentPane().add(textAddress); textAddress.setBounds(80,50,160,20); getContentPane().add(labelGender); labelGender.setBounds(10,70,70,20); getContentPane().add(radioMale); radioMale.setBounds(80,70,80,20); getContentPane().add(radioFemale); radioFemale.setBounds(160,70,80,20); groupGender.add(radioMale); groupGender.add(radioFemale); getContentPane().add(labelClass);labelClass.setBounds(10,90,70,20);
getContentPane().add(comboClass);comboClass.setBounds(80,90,100,20);
59
getContentPane().add(buttonSAVE); buttonSAVE.setBounds(60,120,90,20);
getContentPane().add(buttonEXIT);buttonEXIT.setBounds(160,120,80,20); setVisible(true); }
void Perintah() { // button exit buttonEXIT.addActionListener ( new ActionListener() { public void actionPerformed(ActionEvent e) { if (JOptionPane.showConfirmDialog( null, "Apakah Anda benar-benar akan keluar?","Konfirmasi",
JOptionPane.OK_CANCEL_OPTION,JOptionPane.QUESTION_MESSAGE) == JOptionPane.OK_OPTION) { System.exit(0); } } 60
} );
//button save buttonSAVE.addActionListener ( new ActionListener() { public void actionPerformed (ActionEvent e) { String NIM, Name, Address, Gender, Kelas; NIM = textNIM.getText(); Name = textName.getText(); Address = textAddress.getText(); if(radioMale.isSelected()==true) { Gender = radioMale.getText(); } else { Gender = radioFemale.getText(); } Kelas = (String) comboClass.getSelectedItem();
61
//input ke database KoneksiMySQL open = new KoneksiMySQL (); Connection mysql = open.getConnection(); try { Statement stmt = mysql.createStatement(); String sql = "INSERT INTO T_Mahasiswa VALUES ('"+NIM+"','"+Name+"','"+Address+"','"+Gender+"','"+Kelas+"');"; int i = stmt.executeUpdate (sql); if (i == 1) { JOptionPane.showMessageDialog(null,"Data Mahasiswa Berhasil Disimpan"); } } catch (Exception ex) { JOptionPane.showMessageDialog(null, ex.getMessage()); } } }); }
public static void main(String[]args) { 62
Input_Mahasiswa sq=new Input_Mahasiswa(); sq.GUI(); sq.Perintah(); } }
10.2.1.3 Program 10.3 Search dan Update import java.awt.*; import javax.swing.*; import java.awt.event.*; import java.io.*; import java.sql.*;
class Update_Mahasiswa extends JFrame { JLabel labelNIM=new JLabel("NIM"); JTextField textNIM=new JTextField(30); JLabel labelName=new JLabel("Name"); JTextField textName=new JTextField(30); JLabel labelAddress=new JLabel("Address"); JTextField textAddress=new JTextField(50); JLabel labelGender=new JLabel("Gender"); JButton buttonSEARCH=new JButton("SEARCH"); JButton buttonUPDATE=new JButton("UPDATE"); JButton buttonEXIT=new JButton("EXIT"); JRadioButton radioMale=new JRadioButton("Male"); JRadioButton radioFemale=new JRadioButton("Female"); ButtonGroup groupGender=new ButtonGroup(); JLabel labelClass=new JLabel("Class"); String[] GClass={"Morning","Evening","Extension"}; 63
JComboBox comboClass=new JComboBox(GClass);
Update_Mahasiswa() { setTitle("Data Mahasiswa"); setLocation(300,100); setSize(300,200); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); }
void GUI() { getContentPane().setLayout(null); getContentPane().add(labelNIM); labelNIM.setBounds(10,10,70,20); getContentPane().add(textNIM); textNIM.setBounds(80,10,110,20); getContentPane().add(buttonSEARCH); buttonSEARCH.setBounds(200,10,90,20); getContentPane().add(labelName);labelName.setBounds(10,30,70,20); getContentPane().add(textName); textName.setBounds(80,30,110,20); getContentPane().add(labelAddress);labelAddress.setBounds(10,50,70,20); getContentPane().add(textAddress); textAddress.setBounds(80,50,160,20); getContentPane().add(labelGender); labelGender.setBounds(10,70,70,20); getContentPane().add(radioMale); radioMale.setBounds(80,70,80,20);
64
getContentPane().add(radioFemale); radioFemale.setBounds(160,70,80,20); groupGender.add(radioMale); groupGender.add(radioFemale); getContentPane().add(labelClass);labelClass.setBounds(10,90,70,20); getContentPane().add(comboClass);comboClass.setBounds(80,90,100,20); getContentPane().add(buttonUPDATE);buttonUPDATE.setBounds(60,120,90,20); getContentPane().add(buttonEXIT);buttonEXIT.setBounds(160,120,80,20); setVisible(true); }
void Perintah() { // button exit buttonEXIT.addActionListener ( new ActionListener() { public void actionPerformed(ActionEvent e) { if (JOptionPane.showConfirmDialog( null, "Apakah Anda benar-benar akan keluar?","Konfirmasi", JOptionPane.OK_CANCEL_OPTION,JOptionPane.QUESTION_MESSAGE) == JOptionPane.OK_OPTION) { System.exit(0); 65
} } } );
// button search buttonSEARCH.addActionListener ( new ActionListener() { public void actionPerformed (ActionEvent e) { String search; String NIM, Name, Address, Gender, Kelas; KoneksiMySQL open = new KoneksiMySQL (); Connection mysql = open.getConnection(); try { search=textNIM.getText(); Statement stmt = mysql.createStatement(); String sql = "SELECT * FROM T_Mahasiswa WHERE NIM like '"+search+"'"; ResultSet i = stmt.executeQuery (sql); if(i.next()) { textNIM.setText(i.getString(1)); textName.setText(i.getString(2)); textAddress.setText(i.getString(3)); Gender = i.getString(4); if(Gender.equals("Male")) 66
{ radioMale.setSelected(true); } else { radioFemale.setSelected(true); } comboClass.setSelectedItem(i.getString(5)); } else { JOptionPane.showMessageDialog(null, "Data Mahasiswa Tidak Ada"); } } catch (Exception ex) { JOptionPane.showMessageDialog(null, ex.getMessage()); } } });
// button update buttonUPDATE.addActionListener ( new ActionListener() { public void actionPerformed (ActionEvent e) { String NIM, Name, Address, Gender, Kelas; 67
NIM = textNIM.getText(); Name = textName.getText(); Address = textAddress.getText(); if(radioMale.isSelected()==true) { Gender = radioMale.getText(); } else { Gender = radioFemale.getText(); } Kelas = (String) comboClass.getSelectedItem();
//input ke database KoneksiMySQL open = new KoneksiMySQL (); Connection mysql = open.getConnection(); try { Statement stmt = mysql.createStatement(); String sql = "UPDATE T_Mahasiswa SET Name='"+Name+"',Address='"+Address+"',Gender='"+Gender+"',Kelas='"+Kelas+ "'WHERE NIM='"+NIM+"'"; int i = stmt.executeUpdate (sql); if (i == 1) { JOptionPane.showMessageDialog(null, "Data Mahasiswa Berhasil Diupdate"); } } 68
catch (Exception ex) { JOptionPane.showMessageDialog(null, ex.getMessage()); } } }); }
public static void main(String[]args) { Update_Mahasiswa sq=new Update_Mahasiswa(); sq.GUI(); sq.Perintah(); } }
10.2.1.4 Program 10.4 Delete import java.awt.*; import javax.swing.*; import java.awt.event.*; import java.io.*; import java.sql.*;
class Delete_Mahasiswa extends JFrame { JLabel labelNIM=new JLabel("NIM"); JTextField textNIM=new JTextField(30); JLabel labelName=new JLabel("Name"); JTextField textName=new JTextField(30); JLabel labelAddress=new JLabel("Address"); 69
JTextField textAddress=new JTextField(50); JLabel labelGender=new JLabel("Gender"); JButton buttonSEARCH=new JButton("SEARCH"); JButton buttonDELETE=new JButton("DELETE"); JButton buttonEXIT=new JButton("EXIT"); JRadioButton radioMale=new JRadioButton("Male"); JRadioButton radioFemale=new JRadioButton("Female"); ButtonGroup groupGender=new ButtonGroup(); JLabel labelClass=new JLabel("Class"); String[] GClass={"Morning","Evening","Extension"}; JComboBox comboClass=new JComboBox(GClass);
Delete_Mahasiswa() { setTitle("Data Mahasiswa"); setLocation(300,100); setSize(300,200); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); }
void GUI() { getContentPane().setLayout(null); getContentPane().add(labelNIM); labelNIM.setBounds(10,10,70,20); getContentPane().add(textNIM); textNIM.setBounds(80,10,110,20); getContentPane().add(buttonSEARCH); buttonSEARCH.setBounds(200,10,90,20);
70
getContentPane().add(labelName);labelName.setBounds(10,30,70,20); getContentPane().add(textName); textName.setBounds(80,30,110,20); getContentPane().add(labelAddress);labelAddress.setBounds(10,50,70,20); getContentPane().add(textAddress); textAddress.setBounds(80,50,160,20); getContentPane().add(labelGender); labelGender.setBounds(10,70,70,20); getContentPane().add(radioMale); radioMale.setBounds(80,70,80,20); getContentPane().add(radioFemale); radioFemale.setBounds(160,70,80,20); groupGender.add(radioMale); groupGender.add(radioFemale); getContentPane().add(labelClass);labelClass.setBounds(10,90,70,20); getContentPane().add(comboClass);comboClass.setBounds(80,90,100,20); getContentPane().add(buttonDELETE);buttonDELETE.setBounds(60,120,90,20); getContentPane().add(buttonEXIT);buttonEXIT.setBounds(160,120,80,20); setVisible(true); }
void Perintah() { // button exit buttonEXIT.addActionListener ( 71
new ActionListener() { public void actionPerformed(ActionEvent e) { if (JOptionPane.showConfirmDialog(null, "Apakah Anda benar-benar akan keluar?","Konfirmasi", JOptionPane.OK_CANCEL_OPTION,JOptionPane.QUESTION_MESSAGE) == JOptionPane.OK_OPTION) { System.exit(0); } } } );
// button search buttonSEARCH.addActionListener ( new ActionListener() { public void actionPerformed (ActionEvent e) { String search; String NIM, Name, Address, Gender, Kelas; KoneksiMySQL open = new KoneksiMySQL (); Connection mysql = open.getConnection(); try { search=textNIM.getText(); 72
Statement stmt = mysql.createStatement(); String sql = "SELECT * FROM T_Mahasiswa WHERE NIM like '"+search+"'"; ResultSet i = stmt.executeQuery (sql); if(i.next()) { textNIM.setText(i.getString(1)); textName.setText(i.getString(2)); textAddress.setText(i.getString(3)); Gender = i.getString(4); if(Gender.equals("Male")) { radioMale.setSelected(true); } else { radioFemale.setSelected(true); } comboClass.setSelectedItem(i.getString(5)); } else { JOptionPane.showMessageDialog(null, "Data Mahasiswa Tidak Ada"); } } catch (Exception ex) { JOptionPane.showMessageDialog(null, ex.getMessage()); } 73
} });
// button delete buttonDELETE.addActionListener ( new ActionListener() { public void actionPerformed (ActionEvent e) { String NIM, Name, Address, Gender, Kelas; NIM = textNIM.getText(); Name = textName.getText(); Address = textAddress.getText(); if(radioMale.isSelected()==true) { Gender = radioMale.getText(); } else { Gender = radioFemale.getText(); } Kelas = (String) comboClass.getSelectedItem();
//input ke database KoneksiMySQL open = new KoneksiMySQL (); Connection mysql = open.getConnection(); try 74
{ Statement stmt = mysql.createStatement(); String sql = "DELETE FROM T_Mahasiswa WHERE NIM='"+NIM+"'"; int i = stmt.executeUpdate (sql); if (i == 1) { JOptionPane.showMessageDialog(null, "Data Mahasiswa Berhasil Dihapus"); } } catch (Exception ex) { JOptionPane.showMessageDialog(null, ex.getMessage()); } } }); } public static void main(String[]args) { Delete_Mahasiswa sq=new Delete_Mahasiswa(); sq.GUI(); sq.Perintah(); } }
10.2.1.5 Program 10.5 View Table dengan Jtable import java.awt.*; import java.sql.*; import javax.swing.*; 75
import javax.swing.table.*; public class View_Mahasiswa extends JPanel { String[] header = {"NIM","Name","Address","Gender","Class"}; JTable table = new JTable(); JScrollPane scrollTable = new JScrollPane(); private Object[][] dataTable = null;
View_Mahasiswa() { KoneksiMySQL open = new KoneksiMySQL (); Connection mysql = open.getConnection(); try { Statement stmt = mysql.createStatement(); String sql = "SELECT * FROM T_Mahasiswa"; ResultSet rs = stmt.executeQuery(sql); ResultSetMetaData meta = rs.getMetaData(); int kolom = meta.getColumnCount(); int baris = 0; while(rs.next()) { baris = rs.getRow(); }
dataTable = new Object[baris][kolom]; int x = 0; rs.beforeFirst(); while(rs.next()) 76
{ dataTable[x][0] = rs.getString("NIM"); dataTable[x][1] = rs.getString("Name"); dataTable[x][2] = rs.getString("Address"); dataTable[x][3] = rs.getString("Gender"); dataTable[x][4] = rs.getString("Kelas"); x++; } scrollTable.setViewportView(table); table.setModel(new DefaultTableModel(dataTable, header)); add(scrollTable, BorderLayout.NORTH); stmt.close(); rs.close(); } catch (Exception ex) { JOptionPane.showMessageDialog(null, "Data Mahasiswa Error"); } }
void GUI() { JFrame frame = new JFrame("Data Mahasiswa"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); View_Mahasiswa content = new View_Mahasiswa(); content.setOpaque(true); frame.setContentPane(content); frame.pack(); frame.setLocationRelativeTo(null); 77
frame.setVisible(true); }
public static void main(String [] args) { View_Mahasiswa sq=new View_Mahasiswa(); sq.GUI(); } }
10.2.2 Kasus Penjualan Tiket Pesawat 10.2.2.1 Input Member import java.awt.*; import javax.swing.*; import java.awt.event.*; import java.io.*; import java.sql.*; class input_member extends JFrame { JLabel labelNo_ktp=new JLabel("No KTP"); JTextField textNo_ktp=new JTextField(50); JLabel labelNama=new JLabel("Nama"); JTextField textNama=new JTextField(50); JRadioButton radioL=new JRadioButton("L"); JRadioButton radioP=new JRadioButton("P"); ButtonGroup groupGender=new ButtonGroup(); JLabel labelGender=new JLabel("Gender"); JLabel labelAlamat=new JLabel("Alamat"); JTextField textAlamat=new JTextField(30); JLabel labelNo_tlp=new JLabel("No Telp"); JTextField textNo_tlp=new JTextField(30); JLabel labelTujuan=new JLabel("Tujuan"); JTextField textTujuan=new JTextField(30); JButton buttonSAVE=new JButton("SAVE"); JButton buttonEXIT=new JButton("EXIT"); input_member() { setTitle("Data Member"); setLocation(300,100); 78
setSize(350,300); setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); } void GUI() { getContentPane().setLayout(null); getContentPane().add(labelNo_ktp); labelNo_ktp.setBounds(10,10,110,20); getContentPane().add(textNo_ktp); textNo_ktp.setBounds(80,10,180,20); getContentPane().add(labelNama); labelNama.setBounds(10,40,60,20); getContentPane().add(textNama); textNama.setBounds(80,40,180,20); getContentPane().add(labelGender); labelGender.setBounds(10,70,70,20); getContentPane().add(radioL); radioL.setBounds(80,70,80,20); getContentPane().add(radioP); radioP.setBounds(160,70,80,20); groupGender.add(radioL); groupGender.add(radioP); getContentPane().add(labelAlamat); labelAlamat.setBounds(10,100,110,20); getContentPane().add(textAlamat); textAlamat.setBounds(80,100,180,20); getContentPane().add(labelNo_tlp); labelNo_tlp.setBounds(10,130,110,20); getContentPane().add(textNo_tlp); textNo_tlp.setBounds(80,130,180,20); getContentPane().add(labelTujuan); labelTujuan.setBounds(10,160,110,20); getContentPane().add(textTujuan); textTujuan.setBounds(80,160,180,20); getContentPane().add(buttonSAVE); buttonSAVE.setBounds(60,200,90,20); getContentPane().add(buttonEXIT); buttonEXIT.setBounds(160,200,80,20); setVisible(true); } void Perintah() { //buton exit buttonEXIT.addActionListener ( new ActionListener() { public void actionPerformed(ActionEvent e) { if (JOptionPane.showConfirmDialog( null, "Apakah anda benar-benar akan keluar?","konfirmasi", JOptionPane.OK_CANCEL_OPTION,JOptionPane.QUESTION_MESSAGE) == JOptionPane.OK_OPTION) { System.exit(0); } } } ); 79
//button save buttonSAVE.addActionListener ( new ActionListener() { public void actionPerformed (ActionEvent e) { String No_ktp, Nama, Gender, Alamat, No_tlp, Tujuan; No_ktp = textNo_ktp.getText(); Nama = textNama.getText(); Alamat = textAlamat.getText(); No_tlp = textNo_tlp.getText(); Tujuan = textTujuan.getText(); if(radioL.isSelected()==true) { Gender = radioL.getText(); } else { Gender = radioP.getText(); } //input ke database Koneksi open = new Koneksi (); Connection mysql = open.getConnection (); try { Statement stmt = mysql.createStatement(); String sql = "INSERT INTO tbl_member VALUES ('"+No_ktp+"','"+Nama+"','"+Gender+"','"+Alamat+"','"+No_tlp+"','"+Tujuan+"') ; "; int i = stmt.executeUpdate (sql); if (i == 1) { JOptionPane.showMessageDialog(null, "Data Berhasil Disimpan"); } } catch (Exception ex) { JOptionPane.showMessageDialog(null, ex.getMessage()); } } }); } public static void main(String[]args) 80
{ input_member sq=new input_member(); sq.GUI(); sq.Perintah(); } }
10.2.2.2 Edit Member import java.awt.*; import javax.swing.*; import java.awt.event.*; import java.io.*; import java.sql.*; class edit_member extends JFrame { JLabel labelNo_ktp=new JLabel("Input No KTP"); JTextField textNo_ktp=new JTextField(50); JLabel labelNama=new JLabel("Nama"); JTextField textNama=new JTextField(50); JRadioButton radioL=new JRadioButton("L"); JRadioButton radioP=new JRadioButton("P"); ButtonGroup groupGender=new ButtonGroup(); JLabel labelAlamat=new JLabel("Alamat"); JTextField textAlamat=new JTextField(30); JLabel labelNo_tlp=new JLabel("No Telp"); JTextField textNo_tlp=new JTextField(30); JLabel labelTujuan=new JLabel("Tujuan"); JTextField textTujuan=new JTextField(30); JButton buttonCARI=new JButton("Cari"); JButton buttonEDIT=new JButton("Edit"); JButton buttonKELUAR=new JButton("Keluar"); edit_member() { setTitle("Data Member"); setLocation(300,100); setSize(350,300); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } void GUI() { getContentPane().setLayout(null);
81
getContentPane().add(labelNo_ktp); labelNo_ktp.setBounds(10,10,90,20); getContentPane().add(textNo_ktp); textNo_ktp.setBounds(90,10,130,20); getContentPane().add(buttonCARI); buttonCARI.setBounds(230,10,90,20); getContentPane().add(labelNama); labelNama.setBounds(10,60,70,20); getContentPane().add(textNama); textNama.setBounds(90,60,130,20); getContentPane().add(labelAlamat); labelAlamat.setBounds(10,85,70,20); getContentPane().add(textAlamat); textAlamat.setBounds(90,85,130,20); getContentPane().add(radioL); radioL.setBounds(90,120,80,20); getContentPane().add(radioP); radioP.setBounds(170,120,80,20); groupGender.add(radioL); groupGender.add(radioP); getContentPane().add(labelNo_tlp); labelNo_tlp.setBounds(10,150,70,20); getContentPane().add(textNo_tlp); textNo_tlp.setBounds(90,150,130,20); getContentPane().add(labelTujuan); labelTujuan.setBounds(10,170,70,20); getContentPane().add(textTujuan); textTujuan.setBounds(90,170,130,20); getContentPane().add(buttonEDIT); buttonEDIT.setBounds(60,200,90,20); getContentPane().add(buttonKELUAR); buttonKELUAR.setBounds(160,200,80,20); setVisible(true); } void Perintah() { //button exit buttonKELUAR.addActionListener ( new ActionListener() { public void actionPerformed(ActionEvent e) { if (JOptionPane.showConfirmDialog( null, "Apakah Anda benar-benar akan Keluar?","Konfirmasi", JOptionPane.OK_CANCEL_OPTION,JOptionPane.QUESTION_MESSAGE) 82
== JOptionPane.OK_OPTION) { System.exit(0); } } } ); // button search buttonCARI.addActionListener ( new ActionListener() { public void actionPerformed (ActionEvent e) { String cari; String No_ktp, Nama, Gender, Alamat, No_tlp, Tujuan; Koneksi open = new Koneksi(); Connection mysql = open.getConnection(); try { cari=textNo_ktp.getText(); Statement stmt = mysql.createStatement(); String sql = "SELECT * FROM tbl_member WHERE No_ktp like'"+cari+"'"; ResultSet i = stmt.executeQuery (sql); if(i.next()) { textNo_ktp.setText(i.getString(1)); textNama.setText(i.getString(2)); textAlamat.setText(i.getString(4)); textNo_tlp.setText(i.getString(5)); textTujuan.setText(i.getString(6)); Gender = i.getString(3); if(Gender.equals("L")) { radioL.setSelected(true); } else { radioP.setSelected(true); } } else 83
{ JOptionPane.showMessageDialog(null, "Data Tidak Ada"); } } catch (Exception ex) { JOptionPane.showMessageDialog(null, ex.getMessage()); } } }); // button update buttonEDIT.addActionListener ( new ActionListener() { public void actionPerformed (ActionEvent e) { String No_ktp, Nama, Gender, Alamat, No_tlp, Tujuan; No_ktp = textNo_ktp.getText(); Nama = textNama.getText(); Alamat = textAlamat.getText(); No_tlp = textNo_tlp.getText(); Tujuan = textTujuan.getText(); if(radioL.isSelected()==true) { Gender = radioL.getText(); } else { Gender = radioP.getText(); } Koneksi open = new Koneksi(); Connection mysql = open.getConnection(); try { Statement stmt = mysql.createStatement(); String sql = "UPDATE tbl_member SET Nama='"+Nama+"',Alamat='"+Alamat+"',Gender='"+Gender+"',No_tlp='"+No_tlp +"',Tujuan='"+Tujuan+"'WHERE No_ktp='"+No_ktp+"'"; int i = stmt.executeUpdate (sql); if (i==1) { 84
JOptionPane.showMessageDialog(null, "Data Mahasiswa Berhasil DiUpdate"); } } catch (Exception ex) { JOptionPane.showMessageDialog(null, ex.getMessage()); } } }); } public static void main(String[] args) { edit_member sq=new edit_member(); sq.GUI(); sq.Perintah(); } }
10.2.2.3 Delete Member import java.awt.*; import javax.swing.*; import java.awt.event.*; import java.io.*; import java.sql.*; class delete_member extends JFrame { JLabel labelNo_ktp=new JLabel("Input No KTP"); JTextField textNo_ktp=new JTextField(50); JLabel labelNama=new JLabel("Nama"); JTextField textNama=new JTextField(50); JRadioButton radioL=new JRadioButton("L"); JRadioButton radioP=new JRadioButton("P"); ButtonGroup groupGender=new ButtonGroup(); JLabel labelAlamat=new JLabel("Alamat"); JTextField textAlamat=new JTextField(30); JLabel labelNo_tlp=new JLabel("No Telp"); JTextField textNo_tlp=new JTextField(30); JLabel labelTujuan=new JLabel("Tujuan"); JTextField textTujuan=new JTextField(30); JButton buttonCARI=new JButton("Cari"); 85
JButton buttonHAPUS=new JButton("Hapus"); JButton buttonKELUAR=new JButton("Keluar"); delete_member() {setTitle("Data Pelanggan"); setLocation(300,100); setSize(500,500); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); }
void GUI() { getContentPane().setLayout(null); getContentPane().add(labelNo_ktp); labelNo_ktp.setBounds(10,10,90,20); getContentPane().add(textNo_ktp); textNo_ktp.setBounds(90,10,130,20); getContentPane().add(buttonCARI); buttonCARI.setBounds(230,10,90,20); getContentPane().add(labelNama); labelNama.setBounds(10,60,70,20); getContentPane().add(textNama); textNama.setBounds(90,60,130,20); getContentPane().add(labelAlamat); labelAlamat.setBounds(10,85,70,20); getContentPane().add(textAlamat); textAlamat.setBounds(90,85,130,20); getContentPane().add(radioL); radioL.setBounds(90,120,80,20); getContentPane().add(radioP); radioP.setBounds(170,120,80,20); groupGender.add(radioL); groupGender.add(radioP); getContentPane().add(labelNo_tlp); labelNo_tlp.setBounds(10,150,70,20); getContentPane().add(textNo_tlp); textNo_tlp.setBounds(90,150,130,20); getContentPane().add(labelTujuan); labelTujuan.setBounds(10,170,70,20); getContentPane().add(textTujuan); textTujuan.setBounds(90,170,130,20); getContentPane().add(buttonHAPUS); buttonHAPUS.setBounds(60,200,90,20); getContentPane().add(buttonKELUAR); buttonKELUAR.setBounds(160,200,80,20); setVisible(true); } void Perintah() 86
{ //button exit buttonKELUAR.addActionListener ( new ActionListener() { public void actionPerformed(ActionEvent e) { if (JOptionPane.showConfirmDialog( null, "Apakah Anda benar-benar akan Keluar?","Konfirmasi", JOptionPane.OK_CANCEL_OPTION,JOptionPane.QUESTION_MESSAGE) == JOptionPane.OK_OPTION) { System.exit(0); } } } ); // button search buttonCARI.addActionListener ( new ActionListener() { public void actionPerformed (ActionEvent e) { String cari; String No_ktp, Nama, Gender, Alamat, No_tlp, Tujuan; Koneksi open = new Koneksi(); Connection mysql = open.getConnection(); try { cari=textNo_ktp.getText(); Statement stmt = mysql.createStatement(); String sql = "SELECT * FROM tbl_member WHERE No_ktp like'"+cari+"'"; ResultSet i = stmt.executeQuery (sql); if(i.next()) { textNo_ktp.setText(i.getString(1)); textNama.setText(i.getString(2)); textAlamat.setText(i.getString(4)); textNo_tlp.setText(i.getString(5)); 87
textTujuan.setText(i.getString(6)); Gender = i.getString(3); if(Gender.equals("L")) { radioL.setSelected(true); } else { radioP.setSelected(true); } } else { JOptionPane.showMessageDialog(null, "Data Tidak Ada"); } } catch (Exception ex) { JOptionPane.showMessageDialog(null, ex.getMessage()); } } }); // button delete buttonHAPUS.addActionListener ( new ActionListener() { public void actionPerformed (ActionEvent e) { String No_ktp, Nama, Gender, Alamat, No_tlp, Tujuan; No_ktp = textNo_ktp.getText(); Nama = textNama.getText(); Alamat = textAlamat.getText(); No_tlp = textNo_tlp.getText(); Tujuan = textTujuan.getText(); if(radioL.isSelected()==true) { Gender = radioL.getText(); } else { Gender = radioP.getText(); 88
} Koneksi open = new Koneksi(); Connection mysql = open.getConnection(); try { Statement stmt = mysql.createStatement(); String sql = "DELETE FROM tbl_member WHERE No_ktp='"+No_ktp+"'"; int i = stmt.executeUpdate (sql); if (i==1) { JOptionPane.showMessageDialog(null, "Data Berhasil Dihapus"); } } catch (Exception ex) { JOptionPane.showMessageDialog(null, ex.getMessage()); } } }); } public static void main(String[] args) { delete_member sq=new delete_member(); sq.GUI(); sq.Perintah(); } }
10.2.2.4 MENU import javax.swing.*; import java.awt.event.*; public class menu extends JFrame{ JMenuBar menuContoh = new JMenuBar(); JMenu File = new JMenu("File"); JMenu OpenSubmenu = new JMenu("Open"); JMenuItem mbr = new JMenuItem ("Tabel Member"); JMenuItem Exit = new JMenuItem("Exit"); 89
JMenu Data = new JMenu("Data"); JMenu Open = new JMenu("Open"); JMenuItem Imbr = new JMenuItem ("Input Member"); JMenuItem Embr = new JMenuItem ("Edit Member"); JMenuItem Dmbr = new JMenuItem ("Delete Member");
JMenu About = new JMenu("About"); JMenuItem AboutDialog = new JMenuItem("About Us"); tampil_member tampil = new tampil_member(); input_member input = new input_member(); edit_member edit = new edit_member(); delete_member delete = new delete_member (); void GUI(){ setJMenuBar(menuContoh); menuContoh.add(File); File.add(OpenSubmenu); OpenSubmenu.add(mbr); File.add(Exit); menuContoh.add(Data); Data.add(Open); Open.add(Imbr); Open.add(Embr); Open.add(Dmbr);
menuContoh.add(About); About.add(AboutDialog); setVisible(true); } menu(){ setTitle("Event_5"); setLocation(300,100); setSize(500,350); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } void perintahku(){ mbr.addActionListener((ActionEvent e) -> { tampil.GUI(); }); 90
Exit.addActionListener((ActionEvent e) -> { System.exit(0); }); AboutDialog.addActionListener((ActionEvent e) -> { JOptionPane.showMessageDialog(null, "Program Create By: Sholahuddin Al-Ayubi
www.sholahudd.inNIM: 13111019
Nama: Sholahuddin Al-Ayubi","About US",JOptionPane.INFORMATION_MESSAGE); }); Imbr.addActionListener((ActionEvent e) -> { input.GUI(); input.Perintah(); }); Embr.addActionListener((ActionEvent e) -> { edit.GUI(); edit.Perintah(); }); Dmbr.addActionListener((ActionEvent e) -> { delete.GUI(); delete.Perintah(); }); } public static void main(String[] args) { menu mlz = new menu(); mlz.GUI(); mlz.perintahku(); } }
91
10.3
SCREENSHOT
10. 1 Program 10.2 Input Table
10. 2 Program 10.3 Search dan Update
92
10. 3 Program 10.4 Delete
10. 4 Tabel Input Member
93
10. 5 Edit Member
94
10. 6 Hapus Member
95
10. 7 Menu
96
10.4
ANALISA PROGRAM Program Pada Bab ini merupakan program java Project Akhir yang bertujuan pembuatan Sistem Berbasis Desktop pada kasus penjualan tiket pesawat, baik yang komersial atau yang non komersial
97