Rabu, 25 Januari 2012

Department

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package uas;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author
 */
public class Department {

    private int departmentID;
    private String departmentName;
    private int managerID;
    private int locationID;

    /**
     * @return the departmentID
     */
    public int getDepartmentID() {
        return departmentID;
    }

    /**
     * DepartmentID punya panjang angka maksimal 4 digit atau dari 0001 sd 9999
     * @param departmentID the departmentID to set
     */
    public void setDepartmentID(int departmentID) throws Exception {
        if (departmentID >= 1 && departmentID <= 9999) {
            this.departmentID = departmentID;
        } else {
            throw new Exception("Departemen ID salah");
        }
    }

    /**
     * @return the departmentName
     */
    public String getDepartmentName() {
        return departmentName;
    }

    /**
     * DepartmentName punya panjang karakter sebesar 30.
     * DepartmentName hanya boleh mempunyai unsur alfabet, angka dan karakter lain tidak boleh.
     * @param departmentName the departmentName to set
     */
    public void setDepartmentName(String departmentName) throws Exception {
        if (departmentName.length() <= 30) {
            this.departmentName = departmentName;
        } else {
            throw new Exception("Nama terlalu panjang");
        }
    }

    /**
     * @return the managerID
     */
    public int getManagerID() {
        return managerID;
    }

    /**
     * @param managerID the managerID to set
     */
    public void setManagerID(int managerID) {
        this.managerID = managerID;
    }

    /**
     * @return the locationID
     */
    public int getLocationID() {
        return locationID;
    }

    /**
     * @param locationID the locationID to set
     */
    public void setLocationID(int locationID) {
        this.locationID = locationID;
    }

    /**
     * Fungsi ini untuk membaca tabel departemen
     * @return the list of department
     */
    public static ArrayList<Department> read() {
        ArrayList<Department> dept = new ArrayList<Department>();
        Connection conn = OraConnection.open("jdbc:oracle:thin:@172.23.9.185:1521:orcl", "mhs105314084", "mhs105314084");
        String sql = "select * from DEPARTMENTS";
        try {
            Statement statement = conn.createStatement();
            ResultSet result = statement.executeQuery(sql);
            while (result.next()) {
                Department temp = new Department();
                temp.setDepartmentID(result.getInt(1));
                temp.setDepartmentName(result.getString("department_name"));
                dept.add(temp);
            }
            conn.close();
        } catch (Exception ex) {
            System.out.println("error : " + ex.getMessage());
        }

        return dept;
    }

    public static boolean add(int id, String name) {
//        ArrayList<Department> depts = new ArrayList<Department>();
        Connection conn = OraConnection.open("jdbc:oracle:thin:@172.23.9.185:1521:orcl", "mhs105314084", "mhs105314084");
//        String sql = "select * from DEPARTMENTS";
        try {
            Statement statement = conn.createStatement();

            String queries = "INSERT INTO DEPARTMENTS VALUES ('" + id + "','" + name + "')";
            statement.executeUpdate(queries);
//            System.out.println("Data ");
            conn.close();
        } catch (SQLException ex) {
            Logger.getLogger(Department.class.getName()).log(Level.SEVERE, null, ex);
        }
        return true;
    }
}

Tidak ada komentar:

Posting Komentar