Rabu, 21 Desember 2011

JDBC_Kelas Region

package JDBC;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

public class Regions {

    private Integer regionId;
    private String regionName;

    public Regions() {
    }

    public Regions(Integer regionId, String regionName) {
        this.regionId = regionId;
        this.regionName = regionName;
    }

    public static boolean isRegionExist(Integer regionID) throws SQLException {
        boolean result = false;

        DataHandler dataHandler = new DataHandler();
        dataHandler.getDBConnection();
        Connection conn = dataHandler.conn;
        Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
        String query = "SELECT * FROM regions where region_id = " + regionID;

        ResultSet rset = stmt.executeQuery(query);

        rset.next();
        if (rset.isLast()) {
            result = true;
        } else {
            result = false;
        }

        conn.close();
        return result;
    }

    /**
     * @return the regionId
     */
    public Integer getRegionId() {
        return regionId;
    }

    /**
     * @param regionId the regionId to set
     */
    public void setRegionId(Integer regionId) {
        this.regionId = regionId;
    }

    public static  ResultSet getAllRegion() throws SQLException{
        DataHandler dataHandler = new DataHandler();
        dataHandler.getDBConnection();
        Connection conn = dataHandler.conn;
        Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
//        query = "SELECT * FROM Employees ORDER BY employee_id";
        String query = "SELECT * FROM Regions ORDER BY region_id";
        System.out.println("\nExecuting query: " + query);
        ResultSet rset = stmt.executeQuery(query);
        return rset;
    }

    public static void addRegion(Regions region) throws SQLException{
        DataHandler dataHandler = new DataHandler();
        dataHandler.getDBConnection();
        Connection conn = dataHandler.conn;
        Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
//        query = "SELECT * FROM Employees ORDER BY employee_id";
        String query = "INSERT INTO Regions (region_id, region_name) VALUES ("+region.getRegionId()+",'"+region.getRegionName()+"'";
//        System.out.println("\nExecuting query: " + query);
//        stmt.executeUpdate(query);
        stmt.executeUpdate(query);
//        return rset;

        conn.close();
    }

    public static ArrayList searchRegions(String keyword) throws SQLException {
        ArrayList result = null;

        DataHandler dataHandler = new DataHandler();
        dataHandler.getDBConnection();
        Connection conn = dataHandler.conn;
        Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
        String query = "SELECT * FROM regions where region_name like '%" + keyword + "%'";

        ResultSet rset = stmt.executeQuery(query);
        result = new ArrayList();
        while (rset.next()) {
          Regions temp = new Regions(rset.getInt(1),rset.getString(2));
            result.add(temp);
        }
        conn.close();
        return result;
    }


    /**
     * @return the regionName
     */
    public String getRegionName() {
        return regionName;
    }

    /**
     * @param regionName the regionName to set
     */
    public void setRegionName(String regionName) {
        this.regionName = regionName;
    }

    public static void main(String[] args) throws SQLException {
        System.out.println("isRegionExist " + Regions.isRegionExist(1));
        Regions.addRegion(new Regions(6, "Kalimantan"));
        ResultSet rset = Regions.getAllRegion();
        while (rset.next()) {
//            System.out.println(rset.getInt(1) + " " + rset.getString(2) + " " + rset.getString(3) + " " + rset.getString(4));
            System.out.println(rset.getString(1)+" "+rset.getString(2));
        }
        System.out.println("");
        ArrayList test=Regions.searchRegions("J");
        for (int i = 0; i < test.size(); i++) {
            Regions temp = (Regions) test.get(i);
            System.out.println(temp.regionName);

        }

    }

}

Tidak ada komentar:

Posting Komentar