Anda di halaman 1dari 7

Java GUI dengan Database MySQL

1. BuatdatabaseMySQLdengannamadbjava
2. Buattabledengannamabuku

3. BuatprojectdengannamaLatUasnim
4. BuatpackagedengannamaAplikasi
5. Buatclassdengannamaconfig

packageAplikasi;
importjava.sql.*;
importjavax.swing.*;

publicclassconfig{
privatestaticConnectionconect;
publicstaticConnectiongetConnection()throwsSQLException{
Stringurl="jdbc:mysql://localhost/dbjava";
Stringuser="root";
Stringpassword="";
if(conect==null){
conect=DriverManager.getConnection(url,user,password);
}
returnconect;}

publicstaticvoidmain(String[]args){
try{
getConnection();
JOptionPane.showMessageDialog(null,"terkoneksi");
}

catch(SQLExceptioner){
JOptionPane.showMessageDialog(null,"gagalkoneksi"+er);
}
}
}

6. BuatJFrameformdengannamaPenjualanBuku

packageAplikasi;
importjava.sql.*;
importjavax.swing.*;
importjavax.swing.table.DefaultTableModel;

publicclassPenjualanBukuextendsjavax.swing.JFrame{
inthrgbuku,subtotal,diskon,total;

publicPenjualanBuku(){
initComponents();
bersih();
pasif();
tampilTable();
}


privatevoidjCkodeActionPerformed(java.awt.event.ActionEventevt){
if(jCkode.getSelectedItem().toString().equalsIgnoreCase("bk001")){
jTjudul.setText("JavaPrograming");
hrgbuku=95000;
}elseif(jCkode.getSelectedItem().toString().equalsIgnoreCase("bk002")){
jTjudul.setText("WebPrograming");
hrgbuku=105000;
}else{
jTjudul.setText("MahirLinux");
hrgbuku=60000;
}
jTharga.setText(String.valueOf(hrgbuku));
}

privatevoidjTjumlahActionPerformed(java.awt.event.ActionEventevt){
intjumbel=Integer.parseInt(jTjumlah.getText());
subtotal=hrgbuku*jumbel;
if(jumbel>5)
diskon=subtotal*10/100;
else
diskon=0;
total=subtotaldiskon;
jTsubtotal.setText(String.valueOf(subtotal));
jTdiskon.setText(String.valueOf(diskon));
jTtotal.setText(String.valueOf(total));
}

privatevoidjTnotransActionPerformed(java.awt.event.ActionEventevt){
try{
Statementa=config.getConnection().createStatement();
Stringtampil="SELECT*FROMbukuWHEREnotrans='"+jTnotrans.getText()+"'";
ResultSetrs=a.executeQuery(tampil);
if(rs.next()){
jCkode.setSelectedItem(rs.getString(2));
jTjudul.setText(rs.getString(3));
jTharga.setText(rs.getString(4));
jTjumlah.setText(rs.getString(5));
jTdiskon.setText(rs.getString(6));
jTsubtotal.setText(rs.getString(7));
jTtotal.setText(rs.getString(8));
aktif();

}else{
JOptionPane.showMessageDialog(null,"Datatidakditemukan");
aktif();
}
}catch(Exceptione){
JOptionPane.showMessageDialog(null,"Error"+e);
}
}

publicvoidbersih(){
jTnotrans.setText("");
jCkode.setSelectedIndex(0);
jTjudul.setText("");
jTharga.setText("");
jTjumlah.setText("");
jTsubtotal.setText("");
jTdiskon.setText("");
jTtotal.setText("");
}
publicvoidaktif(){
jCkode.setEnabled(true);
jTjudul.setEnabled(true);
jTharga.setEnabled(true);
jTjumlah.setEnabled(true);
jTsubtotal.setEnabled(true);
jTdiskon.setEnabled(true);
jTtotal.setEnabled(true);
}
publicvoidpasif(){
jCkode.setEnabled(false);
jTjudul.setEnabled(false);
jTharga.setEnabled(false);
jTjumlah.setEnabled(false);
jTsubtotal.setEnabled(false);
jTdiskon.setEnabled(false);
jTtotal.setEnabled(false);
}
publicvoidtampilTable(){
Object[]kolom={"No.Trans","KodeBuku","Judul","Harga","Jumlah
Beli","Diskon","Subtotal","TotalBayar"};
DefaultTableModeltabMode=newDefaultTableModel(null,kolom);
try{

StatementX=config.getConnection().createStatement();
StringA="Select*Frombuku";
ResultSetRS=X.executeQuery(A);
while(RS.next()){
String
[]data={RS.getString(1),RS.getString(2),RS.getString(3),RS.getString(4),RS.getString(5),RS.getStrin
g(6),RS.getString(7),RS.getString(8)};
tabMode.addRow(data);}}
catch(ExceptionE){
JOptionPane.showMessageDialog(null,E);
}
jTable1.setModel(tabMode);
}

privatevoidjBaddActionPerformed(java.awt.event.ActionEventevt){
aktif();
jTnotrans.requestFocus();
}

privatevoidjBsaveActionPerformed(java.awt.event.ActionEventevt){
try{
Statementa=config.getConnection().createStatement();
Stringsimpan="INSERTINTObuku
VALUES('"+jTnotrans.getText()+"','"+jCkode.getSelectedItem()+"','"+jTjudul.getText()+"','"+jThar
ga.getText()+"','"+jTjumlah.getText()+"','"+jTdiskon.getText()+"','"+jTsubtotal.getText()+"','"+jTto
tal.getText()+"')";
a.executeUpdate(simpan);
a.close();
JOptionPane.showMessageDialog(null,"Datatersimpan");
bersih();
pasif();
tampilTable();
}catch(Exceptione){
JOptionPane.showMessageDialog(null,"Erorr"+e);
}
}

privatevoidjBeditActionPerformed(java.awt.event.ActionEventevt){
try{
Statementa=config.getConnection().createStatement();

Stringupdate="UPDATEbukuSET
kdbuku='"+jCkode.getSelectedItem()+"',judul='"+jTjudul.getText()+"',harga='"+jTharga.getText()
+"',"
+
"jumbel='"+jTjumlah.getText()+"',diskon='"+jTdiskon.getText()+"',subtotal='"+jTsubtotal.getText(
)+"',total='"+jTtotal.getText()+"'WHEREnotrans='"+jTnotrans.getText()+"'";
a.executeUpdate(update);
a.close();
JOptionPane.showMessageDialog(null,"Datatelahdiubah");
bersih();
pasif();
tampilTable();
}catch(Exceptione){
JOptionPane.showMessageDialog(null,"Erorr"+e);
}
}

privatevoidjBdeleteActionPerformed(java.awt.event.ActionEventevt){
try{
Statementa=config.getConnection().createStatement();
Stringhapus="DELETEFROMbukuWHEREnotranslike'"+jTnotrans.getText()+"'";
a.executeUpdate(hapus);
a.close();
JOptionPane.showMessageDialog(null,"Datatelahdihapus");
tampilTable();
bersih();
pasif();
}catch(Exceptione){
JOptionPane.showMessageDialog(null,"Error"+e);
}
}

privatevoidjBcancelActionPerformed(java.awt.event.ActionEventevt){
bersih();
pasif();
}

privatevoidjBexitActionPerformed(java.awt.event.ActionEventevt){
System.exit(0);
}

7. JikasudahklikkananpadaLibrariesdiprojectmasingmasing,kemudianpilihAddLibrary,
pilihMySQLJDBCDRIVER,kemudianklikAddLibrary