Anda di halaman 1dari 10

MEMBUAT APLIKASI JAVA DENGAN MY SQL

Pertama buat data base dengan my sql http://localhost/phpmyadmin/

1 HUSNUL HELMI/11.240.0026

import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.sql.*; public class PinjamanDana extends JFrame implements ActionListener, KeyListener, ItemListener{ // pendeklarasian private JLabel lNoAplikasi, lNama, lAlamat, lMerkMotor, lTypeMotor, lPinjaman, lProsentaseBunga, lBunga, lTenor, lTotalBayar, lTanggal, lTotalHarusDibayar; private JTextField txtNoAplikasi, txtNama, txtAlamat, txtPinjaman, txtProsentaseBunga, txtBunga, txtTotalBayar, txtTipeMotor, txtTotalHarusDibayar; private JComboBox cmbMerkMotor, cmbTypeMotor; private JComboBox cbTgl, cbBln, cbThn; private JComboBox cbTglToday, cbBlnToday, cbThnToday; private JRadioButton rb3, rb6, rb12, rb24; private ButtonGroup groupBulan; private JButton btnBatal, btnSimpan, btnHitung; String[] namaBln={"Januari","Februari","Maret","April","Mei","Juni","Juli","Agustus","September","Oktober","N ovember","Desember"}; // pendeklarasian kebutuhan database Connection conn = null; String url = "jdbc:mysql://localhost:3306/"; String dbName = "pinjamdana"; String driver = "com.mysql.jdbc.Driver"; String userName = "root"; String password = ""; Statement statement = null; ResultSet resultSet = null; public PinjamanDana(){ // pengaturan FormBiodata/JFrame setTitle("Pinjaman Dana"); // pemberian judul JFrame setSize(690,550); // mengatur ukuran JFrame setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLayout(null); // null = tdk mnggunakan layout Manager //JLabel bgimage = new JLabel(new ImageIcon("ppblue.jpeg")); setBackground(Color.BLUE); //inisialisasi objeck JLabel lNoAplikasi, lNama, lAlamat, lMerkMotor, lTypeMotor, lPinjaman, lProsentaseBunga, lBunga, lTenor, lTotalBayar lNoAplikasi = new JLabel ("NoAplikasi"); lNama = new JLabel ("Nama"); lAlamat = new JLabel ("Alamat"); lMerkMotor = new JLabel ("MerkMotor"); lTypeMotor = new JLabel ("TypeMotor"); lPinjaman = new JLabel ("Pinjaman"); 2 HUSNUL HELMI/11.240.0026

lProsentaseBunga = new JLabel ("Bunga 2%"); lBunga = new JLabel ("Bunga Perbulan"); lTenor = new JLabel ("Tenor"); lTotalBayar = new JLabel ("Total Hutang"); lTanggal = new JLabel ("Tanggal"); lTotalHarusDibayar = new JLabel ("Angsuran perbulan"); //add Radiobutton rb3 = new JRadioButton("3 Bulan"); rb6 = new JRadioButton("6 Bulan"); rb12 = new JRadioButton("1 Tahun"); rb24 = new JRadioButton("2 Tahun"); groupBulan = new ButtonGroup(); groupBulan.add(rb3); groupBulan.add(rb6); groupBulan.add(rb12); groupBulan.add(rb24); //JTextField txtNoAplikasi = new JTextField(""); txtNama = new JTextField(""); txtAlamat = new JTextField(""); txtTipeMotor = new JTextField(); txtPinjaman = new JTextField(""); txtBunga = new JTextField(""); txtBunga.setEditable(false); txtTotalBayar = new JTextField(""); txtTotalBayar.setEditable(false); txtTotalHarusDibayar = new JTextField(""); txtTotalHarusDibayar.setEditable(false); //JComboBox String[] MerkMotor={"","Honda","Yamaha","Suzuki","MotorCina"}; cmbMerkMotor= new JComboBox(MerkMotor); String[] TypeMotor={"supra","jupiter","satria","tossa"}; cmbTypeMotor= new JComboBox(TypeMotor); cbTgl = new JComboBox(); for(int i=1 ; i<=31 ; i++){ cbTgl.addItem(i); } cbTgl.setEditable(true); String[] namaBln={"Januari","Februari","Maret","April","Mei","Juni","Juli","Agustus","September","Oktober","N ovember","Desember"}; cbBln= new JComboBox(namaBln); 3 HUSNUL HELMI/11.240.0026

cbThn = new JComboBox(); for(int i=1970 ; i<=2012 ; i++){ cbThn.addItem(i); } cbThn.setEditable(true);

//JButton btnBatal = new JButton ("Batal"); btnSimpan = new JButton ("Simpan"); //memasukan(add)JLabel add(lNoAplikasi); add(lNama); add(lAlamat); add(lMerkMotor); add(lTypeMotor); add(lPinjaman); add(lProsentaseBunga); add(lBunga); add(lTenor); add(lTotalBayar); add(lTanggal); add(lTotalHarusDibayar); //add JRadioButton add(rb3); add(rb6); add(rb12); add(rb24); //JTextField txtNoDaftar txtNama, txtAlamat, txtJaminan, txtJumlahHari, txtTotalBayar add(txtNoAplikasi); add(txtNama); add(txtAlamat); add(txtPinjaman); add(txtBunga); add(txtTotalBayar); add(txtTipeMotor); add(txtTotalHarusDibayar); // Combobox cmbJnsKdrn, cmbBulan,cmbTahun add(cmbMerkMotor); add(cmbTypeMotor); add(cbTgl); add(cbBln); add(cbThn); 4 HUSNUL HELMI/11.240.0026

//JButton btnSimpan,btnBatal add(btnSimpan); add(btnBatal);

//mengatur posisi dan ukuran dari object JLabel // setBounds(x,y,w,h) lNoAplikasi.setBounds(20,20,100,20); lNama.setBounds(20,50,100,20); lAlamat.setBounds(20,80,100,20); lMerkMotor.setBounds(20,110,100,20); lTypeMotor.setBounds(20,140,100,20); lPinjaman.setBounds(20,170,100,20); lProsentaseBunga.setBounds(20,200,100,20); lBunga.setBounds(20,230,100,20); lTenor.setBounds(20,260,100,20); lTotalBayar.setBounds(20,290,100,20); lTanggal.setBounds(210,20,50,20); lTotalHarusDibayar.setBounds(20,320,150,20); btnSimpan.setBounds(20,400,100,50); btnBatal.setBounds(120,400,100,50); txtNoAplikasi.setBounds(140,20,50,20); txtNama.setBounds(140,50,200,20); txtAlamat.setBounds(140,80,200,20); txtTipeMotor.setBounds(140,140,200,20); txtPinjaman.setBounds(140,170,200,20); txtBunga.setBounds(140,230,200,20); txtTotalBayar.setBounds(140,290,200,20); txtTotalHarusDibayar.setBounds(140,320,200,20); rb3.setBounds(140,260,100,20); rb6.setBounds(250,260,100,20); rb12.setBounds(360,260,100,20); rb24.setBounds(470,260,100,20); cmbMerkMotor.setBounds(140,110,100,20); cmbTypeMotor.setBounds(140,290,100,20); cbTgl.setBounds(260,20,70,20); cbBln.setBounds(340,20,100,20); cbThn.setBounds(450,20,80,20); setVisible(true);

5 HUSNUL HELMI/11.240.0026

txtPinjaman.addKeyListener(this); rb3.addItemListener(this); rb6.addItemListener(this); rb12.addItemListener(this); rb24.addItemListener(this); btnBatal.addActionListener(this); btnSimpan.addActionListener(this); } public void keyReleased(KeyEvent evt){ if((txtPinjaman.getText()).length()>0){ double Bunga = 0.02; double BungaperBulan = Double.parseDouble(txtPinjaman.getText()); double hasil = Bunga*BungaperBulan; txtBunga.setText(""+ (int)(hasil)); } }

public void keyPressed(KeyEvent evt){} public void keyTyped(KeyEvent evt){} public void itemStateChanged(ItemEvent e){ if (rb3.isSelected()) { int totalBunga = Integer.parseInt(txtBunga.getText()); int Hutang = Integer.parseInt(txtPinjaman.getText()); int totalBayar = (totalBunga*3) + Hutang; txtTotalBayar.setText(""+ totalBayar); int bulan = 3; int semuaTotalBayar= Integer.parseInt(txtTotalBayar.getText()); int bayarPerBulan = semuaTotalBayar / bulan; txtTotalHarusDibayar.setText("" + bayarPerBulan); } else if (rb6.isSelected()) { int totalBunga = Integer.parseInt(txtBunga.getText()); int Hutang = Integer.parseInt(txtPinjaman.getText()); int totalBayar = (totalBunga*6) + Hutang; txtTotalBayar.setText(""+ totalBayar); int bulan = 6; int semuaTotalBayar= Integer.parseInt(txtTotalBayar.getText()); int bayarPerBulan = semuaTotalBayar / bulan; txtTotalHarusDibayar.setText("" + bayarPerBulan); 6 HUSNUL HELMI/11.240.0026

} else if (rb12.isSelected()) { int totalBunga = Integer.parseInt(txtBunga.getText()); int Hutang = Integer.parseInt(txtPinjaman.getText()); int totalBayar = (totalBunga*12) + Hutang; txtTotalBayar.setText(""+ totalBayar); int bulan = 12; int semuaTotalBayar= Integer.parseInt(txtTotalBayar.getText()); int bayarPerBulan = semuaTotalBayar / bulan; txtTotalHarusDibayar.setText("" + bayarPerBulan); } else if (rb24.isSelected()) { int totalBunga = Integer.parseInt(txtBunga.getText()); int Hutang = Integer.parseInt(txtPinjaman.getText()); int totalBayar = (totalBunga*24) + Hutang; txtTotalBayar.setText(""+ totalBayar); int bulan = 24; int semuaTotalBayar= Integer.parseInt(txtTotalBayar.getText()); int bayarPerBulan = semuaTotalBayar / bulan; txtTotalHarusDibayar.setText("" + bayarPerBulan); } } // public void actionPerformed(ActionEvent e){ if(e.getSource() == btnBatal){ txtAlamat.setText(""); txtNama.setText(""); txtNoAplikasi.setText(""); txtTipeMotor.setText(""); txtPinjaman.setText(""); txtBunga.setText(""); txtTotalBayar.setText(""); txtTotalHarusDibayar.setText(""); cmbMerkMotor.setSelectedIndex(0); cbBln.setSelectedIndex(0); cbTgl.setSelectedIndex(0); cbThn.setSelectedIndex(0); groupBulan.clearSelection(); } else if (e.getSource() == btnSimpan) { if (rb3.isSelected()) { try{ Class.forName(driver).newInstance(); conn = DriverManager.getConnection(url+dbName,userName,password); System.out.println("Terhubung dengan database"); 7 HUSNUL HELMI/11.240.0026

statement = conn.createStatement(); statement.execute("INSERT INTO `pinjaman` (`no_aplikasi`, `tanggal`, `nama`, `alamat`, `merek_motor`, `type_motor`, `pinjaman`, `bungaperbulan`, `tenor`, `totalhutang`, `angsuranperbulan`) VALUES ('"+txtNoAplikasi.getText()+"', '"+cbThn.getSelectedItem()+""+(cbBln.getSelectedIndex()+1)+"-"+cbTgl.getSelectedItem()+"', '"+txtNama.getText()+"', '"+txtAlamat.getText()+"', '"+cmbMerkMotor.getSelectedItem()+"', '"+txtTipeMotor.getText()+"', "+txtPinjaman.getText()+", "+txtBunga.getText()+", 3 , "+txtTotalBayar.getText()+", "+txtTotalHarusDibayar.getText()+");"); statement.close(); conn.close(); System.out.println("Menutup Koneksi Ke Database"); } catch (Exception ee) { ee.printStackTrace(); } } else if (rb6.isSelected()) { try{ Class.forName(driver).newInstance(); conn = DriverManager.getConnection(url+dbName,userName,password); System.out.println("Terhubung dengan database"); statement = conn.createStatement(); statement.execute("INSERT INTO `pinjaman` (`no_aplikasi`, `tanggal`, `nama`, `alamat`, `merek_motor`, `type_motor`, `pinjaman`, `bungaperbulan`, `tenor`, `totalhutang`, `angsuranperbulan`) VALUES ('"+txtNoAplikasi.getText()+"', '"+cbThn.getSelectedItem()+""+(cbBln.getSelectedIndex()+1)+"-"+cbTgl.getSelectedItem()+"', '"+txtNama.getText()+"', '"+txtAlamat.getText()+"', '"+cmbMerkMotor.getSelectedItem()+"', '"+txtTipeMotor.getText()+"', "+txtPinjaman.getText()+", "+txtBunga.getText()+", 6 , "+txtTotalBayar.getText()+", "+txtTotalHarusDibayar.getText()+");"); statement.close(); conn.close(); System.out.println("Menutup Koneksi Ke Database"); } catch (Exception ee) { ee.printStackTrace(); } } else if (rb12.isSelected()) { try{ Class.forName(driver).newInstance(); conn = DriverManager.getConnection(url+dbName,userName,password); System.out.println("Terhubung dengan database"); statement = conn.createStatement(); statement.execute("INSERT INTO `pinjaman` (`no_aplikasi`, `tanggal`, `nama`, `alamat`, `merek_motor`, `type_motor`, `pinjaman`, `bungaperbulan`, `tenor`, `totalhutang`, `angsuranperbulan`) VALUES ('"+txtNoAplikasi.getText()+"', '"+cbThn.getSelectedItem()+"8 HUSNUL HELMI/11.240.0026

"+(cbBln.getSelectedIndex()+1)+"-"+cbTgl.getSelectedItem()+"', '"+txtNama.getText()+"', '"+txtAlamat.getText()+"', '"+cmbMerkMotor.getSelectedItem()+"', '"+txtTipeMotor.getText()+"', "+txtPinjaman.getText()+", "+txtBunga.getText()+", 12 , "+txtTotalBayar.getText()+", "+txtTotalHarusDibayar.getText()+");"); statement.close(); conn.close(); System.out.println("Menutup Koneksi Ke Database"); } catch (Exception ee) { ee.printStackTrace(); } } else if (rb24.isSelected()) { try{ Class.forName(driver).newInstance(); conn = DriverManager.getConnection(url+dbName,userName,password); System.out.println("Terhubung dengan database"); statement = conn.createStatement(); statement.execute("INSERT INTO `pinjaman` (`no_aplikasi`, `tanggal`, `nama`, `alamat`, `merek_motor`, `type_motor`, `pinjaman`, `bungaperbulan`, `tenor`, `totalhutang`, `angsuranperbulan`) VALUES ('"+txtNoAplikasi.getText()+"', '"+cbThn.getSelectedItem()+""+(cbBln.getSelectedIndex()+1)+"-"+cbTgl.getSelectedItem()+"', '"+txtNama.getText()+"', '"+txtAlamat.getText()+"', '"+cmbMerkMotor.getSelectedItem()+"', '"+txtTipeMotor.getText()+"', "+txtPinjaman.getText()+", "+txtBunga.getText()+", 24 , "+txtTotalBayar.getText()+", "+txtTotalHarusDibayar.getText()+");"); statement.close(); conn.close(); System.out.println("Menutup Koneksi Ke Database"); } catch (Exception ee) { ee.printStackTrace(); } }

} } public static void main (String args []) { PinjamanDana p = new PinjamanDana(); } }

9 HUSNUL HELMI/11.240.0026

Lalu buka cmd lalu kita seting spt berikut D:\amel>set path=C:\Program Files\Java\jdk1.6.0_14\bin;%path%; D:\amel>javac PinjamanDana.java D:\amel>java PinjamanDana

Hasil outputnya Apabila kita Klik tombol simpan maka akan otomatis menyimpan di

http://localhost/phpmyadmin/

Info : http://gushusnulhelmi.blogspot.com/

10 HUSNUL HELMI/11.240.0026

Anda mungkin juga menyukai