Modul Praktek Java OOP

2,288 views
2,195 views

Published on

Modul untuk Praktikum Pengolahan Database di Java dengan menggunakan konsep OOP Mata Pelajaran Pemrograman Internet dengan Java-JSP SMK Telkom Sandhy Putra Malang

Published in: Education, Technology
2 Comments
1 Like
Statistics
Notes
No Downloads
Views
Total views
2,288
On SlideShare
0
From Embeds
0
Number of Embeds
18
Actions
Shares
0
Downloads
377
Comments
2
Likes
1
Embeds 0
No embeds

No notes for slide

Modul Praktek Java OOP

  1. 1. Project Netbeans dengan OOPUrutan Pengerjaan : 1. File New Project – Java Application 2. Buat Package utils. Package Utils digunakan sebagai tempat class-class java yang berfungi sebagai utilitas dalam aplikasi. Untuk Project ini ada buat java.class DBConnection.java yang berfungsi untuk membuat koneksi antara applikasi dengan database mysql. 3. Buat Package model. Package model digunakan untuk membuat java class yang merepresentasikan/mewakili table didalam database. 4. Buat Package dao (data access object). Berisi java.interface yang menggambarkan fungsi/method apa saja yang dimiliki oleh applikasi 5. Buat Package query. Package ini class-class yang mempunyai fungsi/method yang berakitan dengan pengolahan data (INSERT,UPDATE, DELETE, SELECT, dsb) 6. Buat Pacakge Form. Package ini digunakan untuk java class yang berkaitan dengan desain form applikasi. 7. Jangan lupa untuk menambahkan library mysql-jdbc driver sebagai connector antara applikasi java dengan database mysql.
  2. 2. Class DBConnection.java package com.moklet.utils; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; /** * @author learning */ public class DBConnection { private Connection conn = null; private static DBConnection dBConn = null; static final String DB_URL = "jdbc:mysql://localhost:3306/db"; static final String DB_USER = "root"; static final String DB_PASS = ""; private DBConnection() { if (conn == null) { try { Class.forName("com.mysql.jdbc.Driver").newInstance(); conn = DriverManager.getConnection(DB_URL, DB_USER, DB_PASS); } catch (Exception ex) { ex.printStackTrace(); } } } public static DBConnection getInstance() { DBConnection conn = null; if (dBConn == null) { dBConn = new DBConnection(); conn = dBConn;
  3. 3. } else { conn = dBConn; } return conn; } public Connection getCon() throws SQLException { return this.conn; } }Class Siswa package com.moklet.model; public class siswa { private String nis; private String nama; private String alamat; private String IDtelp; public String getNis() { return nis; } public void setNis(String nis) { this.nis = nis; } public String getNama() { return nama; } public void setNama(String nama) { this.nama = nama; }
  4. 4. public String getAlamat() { return alamat; } public void setAlamat(String alamat) { this.alamat = alamat; } public String getIDtelp() { return IDtelp; } public void setIDtelp(String IDtelp) { this.IDtelp = IDtelp; } }Interface SiswaDAO package com.moklet.dao; import com.moklet.model.siswa; import java.util.List; public interface SiswaDAO { public void insert(siswa sw); public void delete(String nis); public void update(String oldNis, siswa sw); public siswa loadNama(String nama); public siswa load(String nis); public List<siswa> getAll(); public List<siswa> getAll(String name); public List<siswa>getAllNis(String nis); }
  5. 5. Class SiswaQuery package com.moklet.query; import com.moklet.dao.SiswaDAO; import com.moklet.model.siswa; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import java.util.logging.Level; import java.util.logging.Logger; public class SiswaQuery implements SiswaDAO { private Connection conn = null; public SiswaQuery(Connection conn) { this.conn = conn; } public void insert(Siswa sw) { String sql = "INSERT INTO siswa(nis,nama,alamat,IDtelp) VALUES(?,?,?,?)"; try { PreparedStatement st = conn.prepareStatement(sql); st.setString(1, sw.getNis()); st.setString(2, sw.getNama()); st.setString(3, sw.getAlamat()); st.setString(4, sw.getIDtelp()); st.executeUpdate(); } catch (SQLException ex) { Logger.getLogger(SiswaDAO.class.getName()).log(Level.SEVERE, null, ex); } }
  6. 6. public void delete(String nis) { String sql = "DELETE FROM siswa WHERE nis=?"; try { PreparedStatement st = conn.prepareStatement(sql); st.setString(1, nis); st.executeUpdate(); } catch (SQLException ex) { Logger.getLogger(SiswaDAO.class.getName()).log(Level.SEVERE, null, ex); }}public siswa loadNama(String nama) { Siswa siswa = null; String sql = "SELECT nis,nama,alamat,IDTelp FROM siswa WHERE namasiswa=?"; try { PreparedStatement st = conn.prepareStatement(sql); ResultSet rs = st.executeQuery(); if (rs.next()) { siswa = new siswa(); siswa.setNis(rs.getString(1)); siswa.setNama(rs.getString(2)); siswa.setAlamat(rs.getString(3)); siswa.setIDtelp(rs.getString(4)); } } catch (SQLException ex) { Logger.getLogger(SiswaDAO.class.getName()).log(Level.SEVERE, null, ex); } return namaSiswa;}
  7. 7. public List<siswa> getAll() { String sql = "SELECT nis,nama,alamat,IDTelp FROM siswa"; List<siswa> list = new ArrayList<siswa>(); try { PreparedStatement st = conn.prepareStatement(sql); ResultSet rs = st.executeQuery(); while (rs.next()) { siswa s = new siswa(); s.setNis(rs.getString(1)); s.setNama(rs.getString(2)); s.setAlamat(rs.getString(3)); s.setIDtelp(rs.getString(4)); list.add(s); } } catch (SQLException ex) { Logger.getLogger(SiswaDAO.class.getName()).log(Level.SEVERE, null, ex); } return list;}public List<siswa> getAll(String name) { String sql = "SELECT * FROM siswa WHERE nama like?"; List<siswa> list = new ArrayList<siswa>(); try { PreparedStatement st = conn.prepareStatement(sql); st.setString(1, "%" + name + "%"); ResultSet rs = st.executeQuery(); while (rs.next()) { siswa s = new siswa(); s.setNis(rs.getString(1)); s.setNama(rs.getString(2));
  8. 8. s.setAlamat(rs.getString(3)); s.setIDtelp(rs.getString(4)); list.add(s); } } catch (SQLException ex) { Logger.getLogger(SiswaDAO.class.getName()).log(Level.SEVERE, null, ex); } return list;}public siswa load(String nis) { Siswa siswa = null; String sql = "SELECT nis,nama,alamat,IDtelp FROM siswa WHERE nis=?"; try { PreparedStatement st = conn.prepareStatement(sql); st.setString(1, nis); ResultSet rs = st.executeQuery(); if (rs.next()) { siswa = new siswa(); siswa.setNis(rs.getString(1)); siswa.setNama(rs.getString(2)); siswa.setAlamat(rs.getString(3)); siswa.setIDtelp(rs.getString(4)); } } catch (SQLException ex) { Logger.getLogger(SiswaDAO.class.getName()).log(Level.SEVERE, null, ex); } return namaSiswa;}
  9. 9. public void update(String oldNis, siswa sw) { String sql = "UPDATE siswa SET nama=?,alamat=?,IDtelp=? WHERE nis=?"; try { PreparedStatement st = conn.prepareStatement(sql); st.setString(1, sw.getNama()); st.setString(2, sw.getAlamat()); st.setString(3, sw.getIDtelp()); st.setString(4, oldNis); st.executeUpdate(); st.executeUpdate(); } catch (SQLException ex) { Logger.getLogger(SiswaDAO.class.getName()).log(Level.SEVERE, null, ex); }}public List<siswa> getAllNis(String nis) { String sql = "SELECT * FROM siswa WHERE nis LIKE ?"; List<siswa> list = new ArrayList<siswa>(); try { PreparedStatement st = conn.prepareStatement(sql); st.setString(1, "%" + nis + "%"); ResultSet rs = st.executeQuery(); while (rs.next()) { siswa s = new siswa(); s.setNis(rs.getString(1)); s.setNama(rs.getString(2)); s.setAlamat(rs.getString(3)); s.setIDtelp(rs.getString(4)); list.add(s); } } catch (SQLException ex) {
  10. 10. Logger.getLogger(SiswaDAO.class.getName()).log(Level.SEVERE, null, ex); } return list; } }Class FormSiswa package com.moklet.form; import com.moklet.model.siswa; import com.moklet.query.SiswaQuery; import com.moklet.utils.DBConnection; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import java.util.logging.Level;
  11. 11. import java.util.logging.Logger;import javax.swing.JOptionPane;public class FrmSiswa extends javax.swing.JFrame { private List<siswa> records = new ArrayList<siswa>(); private int row = 0; private boolean status; public FrmSiswa() { initComponents(); loadRecords(); updateRow(); } public void loadRecords() { try { DBConnection conn = DBConnection.getInstance(); SiswaQuery daoSiswa = new SiswaQuery(conn.getCon()); records = daoSiswa.getAll(); } catch (SQLException ex) { Logger.getLogger(FrmSiswa.class.getName()).log(Level.SEVERE, null, ex); } } public void updateRow() { if (this.records.size() > 0) { if (row < 0) { row = 0; } siswa sw = records.get(row); txtNis.setText(sw.getNis()); txtNama.setText(sw.getNama()); txtAlamat.setText(sw.getAlamat());
  12. 12. txtNoTelp.setText(sw.getIDtelp()); } else { bersihkanForm(); editForm(false); }}private void txtNisActionPerformed(java.awt.event.ActionEvent evt) { loadRecords2(); updateRow2();}private void btnAddActionPerformed(java.awt.event.ActionEvent evt) { status = true; editForm(true); bersihkanForm(); txtNis.requestFocus();}private void btnSaveActionPerformed(java.awt.event.ActionEvent evt) { if (status == true) { try { siswa sw = new siswa(); sw.setNis(txtNis.getText()); sw.setNama(txtNama.getText()); sw.setAlamat(txtAlamat.getText()); sw.setIDtelp(txtNoTelp.getText()); DBConnection conn = DBConnection.getInstance(); SiswaQuery dao = new SiswaQuery(conn.getCon()); dao.insert(sw); JOptionPane.showMessageDialog(this, "Data Saved"); loadRecords();
  13. 13. updateRow(); } catch (SQLException ex) { Logger.getLogger(FrmSiswa.class.getName()).log(Level.SEVERE, null, ex); } } else { try { siswa sw = new siswa(); sw.setNis(txtNis.getText()); sw.setNama(txtNama.getText()); sw.setAlamat(txtAlamat.getText()); sw.setIDtelp(txtNoTelp.getText()); DBConnection conn = DBConnection.getInstance(); SiswaQuery dao = new SiswaQuery(conn.getCon()); dao.update(sw.getNis(), sw); JOptionPane.showMessageDialog(this, "Data Update"); loadRecords(); updateRow(); } catch (SQLException ex) { Logger.getLogger(FrmSiswa.class.getName()).log(Level.SEVERE, null, ex); } }}private void btnUpdateActionPerformed(java.awt.event.ActionEvent evt) { status = false;}private void btnDeleteActionPerformed(java.awt.event.ActionEvent evt) { try { siswa sw = new siswa(); sw.setNis(txtNis.getText()); DBConnection conn = DBConnection.getInstance();
  14. 14. SiswaQuery dao = new SiswaQuery(conn.getCon()); dao.delete(txtNis.getText()); JOptionPane.showMessageDialog(this, "Data Delete"); loadRecords(); updateRow(); } catch (SQLException ex) { Logger.getLogger(FrmSiswa.class.getName()).log(Level.SEVERE, null, ex); }}private void btnExitActionPerformed(java.awt.event.ActionEvent evt) { this.dispose();}public void loadRecords2() { try { DBConnection conn = DBConnection.getInstance(); SiswaQuery daoSiswa = new SiswaQuery(conn.getCon()); records = daoSiswa.getAllNis(txtNis.getText()); } catch (SQLException ex) { Logger.getLogger(FrmSiswa.class.getName()).log(Level.SEVERE, null, ex); }}public void updateRow2() { if (this.records.size() > 0) { if (row < 0) { row = 0; } siswa sw = records.get(row);
  15. 15. txtNis.setText(sw.getNis()); txtNama.setText(sw.getNama()); txtAlamat.setText(sw.getAlamat()); txtNoTelp.setText(sw.getIDtelp()); } else { JOptionPane.showMessageDialog(this, "Not Found"); loadRecords(); updateRow(); }}void bersihkanForm() { txtNis.setText(""); txtNama.setText(""); txtAlamat.setText(""); txtNoTelp.setText("");}void editForm(boolean x) { txtNis.setEditable(x); txtNama.setEditable(x); txtAlamat.setEditable(x); txtNoTelp.setEditable(x);}public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new FrmSiswa().setVisible(true); } });}

×