Saturday, June 6, 2015

MODUL 9 : DATA ACCESS OBJECT (DAO)

A.      Objectives
Objectives of this practical laboratory are :
1.       Students are able to understand how to work with JDBC.
2.       Stundets are able to implement JDBC using concept of DAO model.

B.      Theoritical Basic
In this material, we will know how java application that built can work with DBMS (Database Managemenet System) by using concept of JDBS (Java Database Connectivity) that had by java. Implementation by using JDBC on application will use DAO model (Data Access Object) that separate  data access function from other part of application (Presentation and Control functions).

C.      Tools And Materials
1.       Laptop
2.       Java Development Kit (JDK 8)
3.       Netbeans IDE 8.0.2

D.      Work Steps
1.       Exeriment 3.1
a.      Creating  Java project named “LatihanMateri3_L200134020”.

b.      Making some new packages inside the project :
·         Packages : Login
·         Packages : FormUtama
·         Packages : Petugas
·         Packages : Buku
·         Packages : Peminjaman
·         Packages : Pengambilan
·         Packages : Laporan

c.       Adding 2 classes in Login package.
·         ViewLogin class : a Jframe (used for making interface).
·         DAOLogin class : DAO used for login (for accessing to DBMS).

d.      Before working more with DAO (inside the java-application), preparing database that want to be used.
e.       Running phpmyadmin, to make database that needed :
perpustakaan_L200134020

f.        Inside database, adding table named “pengguna” that cointains 3 columns.

g.      Inside table of “pengguna”, adding 4 record data.

h.      Next, return back to java application, on viewLogin, adding object as figure below.

Changing object property with this rules.
No
Object Type
Parameter
Value
       1
           Jlabel
              Name
                  lblNamaPengguna
              Text
                  Nama Pengguna
       2
           JtextField
              Name
                  txtNamaPengguna
              Text

       3
           JTextField
              Name
                  txtPassword
              Text

       4
           Jlabel
              Name
                  lblPassword
              Text
                  Password
       5
           JButton
              Name
                  btnLogin
              Text
                  Login
       6
           Jbutton
              Name
                  btnKeluar
              Text
                  Keluar

The views will be change become :


i.         Next, on viewLogin, move to source views.
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Login;

import javax.swing.JOptionPane;

/**
 *
 * @author Jhonny
 */
public class ViewLogin extends javax.swing.JFrame {

    /**
     * Creates new form ViewLogin
     */  
    public ViewLogin() {
        initComponents();
    }
j.        Declaring one for Object DAOLogin.
package Login;

import javax.swing.JOptionPane;

/**
 *
 * @author Jhonny
 */
public class ViewLogin extends javax.swing.JFrame {

    private DAOLogin daoLogin; 

    public ViewLogin() {
        daoLogin = new DAOLogin();
        initComponents();
    }
k.       Then, finishing this program on part of DAOLogin.
l.         First Step, works with JDBC is (1) determining Driver JDBC that used.
In this step contains 2 parts : (a) adding file Driver JDBC that needed. (b) Listing the Driver to DriverManager.

Adding program codes below.
public class DAOLogin {
   
    public DAOLogin() {
        try {
            Class.forName("org.gjt.mm.mysql.Driver");
        } catch (ClassNotFoundException ex) {
            System.out.println("Driver Error");
        }
    }
m.    Second Step, determining connection address (URL, User, Password).
public class DAOLogin {
   
    private String url = "jdbc:mysql://localhost:3306/perpustakaan_l200134020";
    private String username = "root";
    private String password = "";
   
    public DAOLogin() {
        try {
            Class.forName("org.gjt.mm.mysql.Driver");
        } catch (ClassNotFoundException ex) {
            System.out.println("Driver Error");
        }
    }
n.      Third and Fourth Step, making method to connect to server + making statement object.
public class DAOLogin {
   
    private String url = "jdbc:mysql://localhost:3306/perpustakaan_l200134020";
    private String username = "root";
    private String password = "";
   
    private Connection koneksi;
    private Statement sttmt;
   
    public DAOLogin() {
        try {
            Class.forName("org.gjt.mm.mysql.Driver");
        } catch (ClassNotFoundException ex) {
            System.out.println("Driver Error");
        }
    }
   
    public void bukaKoneksi(){
        try {
            koneksi = DriverManager.getConnection(url, username, password);
            sttmt = koneksi.createStatement();
        } catch (SQLException ex) {
            System.out.println("Koneksi Error");
        }
    }
o.      Fifth Step, making method to check login.
public int login(String namaPengguna, String passwordPengguna) {
        ResultSet rsLogin;
        int status = 0;
        try {
            rsLogin = sttmt.executeQuery("select count(*) from pengguna where "
                    + "namaPengguna='" + namaPengguna + "'"
                    + "and password='" + passwordPengguna + "'");
            rsLogin.first();
            status = rsLogin.getInt(1);
        } catch (SQLException ex) {
            System.out.println("Query Error");
        }
        return status;
    }
p.      For DAO, we will break in this fifth step.
q.      Next, adding program codes inide viewLogin class in part of constructor, to open connection.
public class ViewLogin extends javax.swing.JFrame {

    /**
     * Creates new form ViewLogin
     */
    private DAOLogin daoLogin;
   
    public ViewLogin() {
        daoLogin = new DAOLogin();
        daoLogin.bukaKoneksi();
        initComponents();
    }
r.        Then adding event listener in btnLogin to check login.

Addind this program codes :
private void btnLoginActionPerformed(java.awt.event.ActionEvent evt) {                                        
        // TODO add your handling code here:
        int status = daoLogin.login(txtNamaPengguna.getText(), txtPassword.getText());
        if (status == 0) {
            JOptionPane.showMessageDialog(null, "LOGIN GAGAL");
        } else if (status > 0) {
            JOptionPane.showMessageDialog(null, "LOGIN SUKSES");
        }
    } 
s.       Running the program.


t.        Adding program codes in btnKeluar.

private void btnKeluarActionPerformed(java.awt.event.ActionEvent evt) {                                        
        // TODO add your handling code here:
        System.exit(0);

2 comments:

  1. Mas punya email? saya punya pertanyaan tentang python. Saya ikut kursus python secara online. Sudah 5 hari saya stuck di quiz tentang struktur data list & tuple. Maklum, pengarahan hanya lewat video. Sebelumnya saya mengucapkan terima kasih .

    ReplyDelete
  2. Mas Arsal bisa lihat kontak saya di About Me. Terimakasih

    ReplyDelete