Senin, 22 Juni 2015

Modul 9 Praktikum Object Oriented Programming

A. OBJECTIVE
to master the way of working with JDBC and JDBC implementation with DAO concept model.

B. OVERVIEW
In this subject will be learnt how the application created can work with DBMS (Database Management System) using JDBC (Java Database Connectivity) concept. The implementation of using JDBC in application will use DAO (Data Access Object) model working system which separates the data access function from the other part of application (presentation and control function).

C. INSTRUMENTS AND MATERIALS
1. Computer/laptop
2. Module Object Oriented Programming
3. Software
   a. Netbeans IDE 8.0.2
   b. Java Development Kit (JDK)

D. WORK STEPS
1. Create new project.
2. Create some new packages inside the project.
   a. Login
   b. FormUtama
   c. Petugas
   d. Buku
   e. Peminjaman
   f. Pengembalian
   g. Laporan

3. Add 2 classes in Login packages.
   a. ViewLogin class >> a JFrame (used for creating interface)
   b. DAOLogin class >> a DAO for login function (used for accessing DBMS)
4. Before working further with DAO (inside the java appliaction), prepare the database which will be used by the application.
5. Run phpmyadmin (from XAMPP) to create the needed database. Name it with perpustakaan_NIM (ex: perpustakaan_005).
6. In the database, add a table named user with 3 columns.
7. Add 4 data in the user table.
8. Back to java application, on ViewLogin, add object as follows.
   Change the properties.
   The view will be as follows.
9. On ViewLogin, move to source code view mode.
10. Declare an object in DAOLogin.

public class ViewLogin extends javax.swing.JFrame {
    private DAOLogin daoLogin;

    /**
     * Creates new form ViewLogin
     */
    public ViewLogin() {
        daoLogin = new DAOLogin();
        initComponents();
    }

11. Now we will finish the programming in DAOLogin part.
12. First step, work with JDBC are (1) determining the JDBC Driver to be used. there are 2 stages: (a) add JDBC Driver file used (b) enlist the Driver to DriverManager
Add the following code.


public DAOLogin(){
        try{
            Class.forName("org.gjt.mm.mysql.Driver");
        } catch (ClassNotFoundException ex){
            System.out.println("DRIVER Error");
        }
    }

13. Second step, determine the connection address (URL, user, password)

public class DAOLogin {
 
    private String url = "jdbc:mysql://localhost:3306/perpustakaan_005";
    private String username = "root";
    private String password = "";

14. Third and forth steps, create the method to connect to the server and create object statement.
private Connection koneksi;

    private Statement sttmt;

public void bukaKoneksi(){
        try{
            koneksi = DriverManager.getConnection(url, username, password);
            sttmt = (Statement) koneksi.createStatement();
        } catch (SQLException ex){
            System.out.println("KONEKSI Error");
        }
    } 

15. Fifth step, create the method to check the 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;
    }

  16. Add program code in ViewLogin class in constructor part to open the connection.

public class ViewLogin extends javax.swing.JFrame {
    private DAOLogin daoLogin;

    /**
     * Creates new form ViewLogin
     */
    public ViewLogin() {
        daoLogin = new DAOLogin();
        daoLogin.bukaKoneksi();
        initComponents();
    }

17. Add event listener in btnLogin to check login.
18. Add the source code.


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

19. Run the program.
20. Add program code in btnKeluar.


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

Here is the full source code in DAOLogin:

package Login;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 *
 * @author Dessy
 */
public class DAOLogin {
    //2. menemtukan alamat/parameter koneksi
    private String url = "jdbc:mysql://localhost:3306/perpustakaan_005";
    private String username = "root";
    private String password = "";
 
    //3. buat object koneksi
    private Connection koneksi;
 
    //4. buat object statement
    private Statement sttmt;
 
    public DAOLogin(){
        //1. memanggil driver jdbc
        try{
            Class.forName("org.gjt.mm.mysql.Driver");
        } catch (ClassNotFoundException ex){
            System.out.println("DRIVER Error");
        }
    }
 
    //3 & 4. buat metode koneksi dan object statement
    public void bukaKoneksi(){
        try{
            koneksi = DriverManager.getConnection(url, username, password);
            sttmt = (Statement) koneksi.createStatement();
        } catch (SQLException ex){
            System.out.println("KONEKSI Error");
        }
    }
 
    /**5. buat metode untuk login
     * pseudocode
     * cari dalam tabel pengguna, pengguna yang sesuai dengan nama pengguna dan password pengguna
     * jika ada data yang sesuai
     * resultset maka rsLogin akan berubah nilai menjadi lebih dari 0
     */
    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;
    }
}

Tidak ada komentar:

Posting Komentar