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);

Wednesday, June 3, 2015

MODUL 8 : JAVA SWING

A.      Objectives
Objectives of this practical laboratory are :
1.       Students are able to understand what the definition of Java Swing.
2.       Stundets are able to implement Java Swing in this practical laboratory.

B.      Theoritical Basic
Swing is other alternative to implement window programming. Although implemented based on AWT class, swing doesn’t use components that’s on AWT so that component appearance of swing is not depending on local window system. So, graphics of swing for all operating system become similar.

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

D.      Work Steps
1.       Experiment Steps
a.       Opening Netbeans.
b.      Right clicking on project and making a new jFrame Form.
c.       Then, adding jPanel component on the form already made.

d.      Adding menu component and changing the name become “Ubah warna”.

e.      Then giving menu item component on menu so that when clicking the menu, it will be appear a dropdown of menu.

f.        On panel inspector, chosing one of menu item that will be given an action so that when the menu item clicked, it will change the color of window background.
Right clicking on menu item -- chosing Even -- actionPerformed.
g.       Adding this code below of actionPerformed.
jPanel1.setBackground(Color.Red);
jLabel1.setText(“Warna Background Merah”);
h.      The code above will change the color of background become “Red” and center of window will  be written “Warna Background Merah”.


E.       Analyze
Code of jPanel1.setBackground(Color.Red);  sets background of Panel 1 having Red Color.
Code of jLabel1.setText(“Warna Background Merah”);  sets text of Label 1 written “Warna Backgroung Merah”.


F.       Tasks
1.       Adding 2 colors (Green and Yellow).


2.       Adding icon symbol beside on menu item.


Friday, May 15, 2015

MODUL 7 : APPLET

A.    Objectives
Objectives of making this report are :
1.      Students are able to understand what the definition of Applet in Java.
2.      Students are able to implement Applet in this practical laboratory.

B.     Theoritical Basic
Applet is a java program that is for web browser environment. Applet is subclass of panel placed on java.applet package, thereofere anything that able to be placed in panel will also able in applet.
As program that

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

D.    Work Steps
1.      Experiment 1
a.       Creating a new applet class named “TesApplet.java”, writing this program codes below :

import java.applet.Applet;
import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;

/**
 *
 * @author Jhonny
 */
public class TesApplet extends Applet{
    public void paint (Graphics grafik){
        Font font = new Font("sanserif", Font.BOLD, 20);
        grafik.setFont(font);
        grafik.setColor(Color.blue);
       
        int xPusat = this.getSize().width/2;
        int yPusat = this.getSize().height/2;
       
        String ucapan = "Hai Jhonny";
        FontMetrics fontMetrics = this.getFontMetrics(font);
        int posisiX = xPusat-(fontMetrics.stringWidth(ucapan)/2);
       
        grafik.drawString(ucapan, posisiX, yPusat);
    }

}

b.    Compiling and running the program codes above.
c.    After the program is compiled, it will appear “TesApplet.class”. This file that will be called in HTML script.
d.   Then, creating HTML file as this codes below :

<html>
    <head>
        <title>Selamat Belajar Bahasa Appler</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
    </head>
<body>
    <h1>Tes Apllet</h1>
    <applet code = "TesApplet.class" width = 250 height = 80>
    </applet>
    <br>
</body>

</html>

e.   Running the HTML file from web browser.


3.      Experiment 2
a.       Creating a new java file named “TesParameter.java”, then writing program codes below :

import java.awt.*;
import java.applet.Applet;

/**
 *
 * @author joni
 */
public class TesParameter extends Applet{
    String nama;
   
    public void init(){
        nama = getParameter("nama");
        if (nama == null)
            nama = "Jhonny";
    nama = "Hai "+nama +"!!!";
    }
    public void paint (Graphics grafik){
        grafik.drawString(nama, 10, 25);
    }

}

b.      Compiling and running the program codes above.
c.       Then, creating HTML file to access the applet java file above, writing program codes below :

<html>
    <head>
        <title>Akses Parameter</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
    </head>
    <body>
    <applet code ="TesParameter.class" width=250 height=200>
        <param name = nama value = "Jhonny">
    </applet>
    </body>
</html>
              
                    d.   Running the HTML script to browser. The result is :


4.      Analyze
When applet java file is runned, a java class will automatically created. With this java class, we can display this in web browser environment.

5.      Tasks
a.       Creating java applet file that contain nama, nim, and semester.

import java.awt.*;
import java.applet.Applet;
import java.awt.Graphics;


/**
 *
 * @author joni
 */
public class appletKu extends Applet {
  
    String nama;
    String NIM;
    String jurusan;
    String semester;
  
    public void init(){
        nama = getParameter("nama");
        NIM = getParameter("NIM");
        jurusan = getParameter("jurusan");
        semester = getParameter("semester");
        if (nama == null)
            nama = "Joni";
    nama = "Nama : " +nama +
            " NIM : " + NIM +
            " Jurusan : " + jurusan +
            " Semester : " + semester;
    }
    public void paint (Graphics grafik){
        grafik.drawString(nama, 10, 25);
    }  
}

b.      Using Parameter.

<html>
    <head>
        <title>Akses Parameter</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
    </head>
    <body>
    <applet code ="appletKu.class" width=500 height=400>
        <param name = nama value = "Joni">
        <param name = NIM value = "L200134020">
        <param name = jurusan value = "Informatika">
        <param name = semester value = "04">
    </applet>
    </body>
</html>

c.       Result of the program codes :