Rabu, 21 Desember 2011

region

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

package conectsql;

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

/**
 *
 * @author Eka
 */
public class Region {

    private Integer regionId;
    private String regionName;
    private static Connection conn;

    public Region() throws SQLException {
        DataHandler dataHandler = new DataHandler();
        dataHandler.getDBConnection();
        conn = dataHandler.conn;
    }
    public Region(Integer regionId, String regionName) {
        this.regionId = regionId;
        this.regionName = regionName;
    }
    public Integer getRegionId() {
        return regionId;
    }
    public void setRegionId(Integer regionId) {
        this.regionId = regionId;
    }
    public String getRegionName() {
        return regionName;
    }
    public void setRegionName(String regionName) {
        this.regionName = regionName;
    }  
    public static ArrayList TampilRegion() throws SQLException {
        ArrayList result = null;
        DataHandler dataHandler = new DataHandler();
        dataHandler.getDBConnection();
        Connection conn = dataHandler.conn;
        Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
                ResultSet.CONCUR_UPDATABLE);
        String query2 = "Select * from regions";
        result = new ArrayList();
        ResultSet rset = stmt.executeQuery(query2);
        while (rset.next()) {
            Region temp = new Region(rset.getInt(1),
                    rset.getString(2));
            result.add(temp);
        }
        conn.close();
        return result;
    }
    public static ArrayList addRegion(int id, String name) throws SQLException {
        ArrayList result = null;
        DataHandler dataHandler = new DataHandler();
        dataHandler.getDBConnection();
        Connection conn = dataHandler.conn;
        Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
                ResultSet.CONCUR_UPDATABLE);

        String query = "INSERT INTO regions VALUES (" + id + ",'" + name + "')";

        result = new ArrayList();
        try {
            result.add(stmt.executeUpdate(query));
        } catch (SQLException ex) {
            System.out.println("error : " + ex.getMessage());
        }
        conn.close();
        return result;
    }
//    public static void addRegion(int id, String name) throws SQLException
//    {   PreparedStatement pstmt=null;
//        String sisip ="insert into regions values("+id+","+name+")";
//        pstmt=conn.prepareStatement(sisip);
////int hasil=pstmt.executeUpdate(sisip);
//        pstmt.setObject(1, id);
//        pstmt.setObject(2, name);
//        int cek=pstmt.executeUpdate();
//        if (cek >0)
//            System.out.println("Satu data sudah berhasil dimasukkan");
//        else
//            System.out.println("Data belum berhasil ditambah");
//        //alternatif
////        Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
////        String sisip2 ="insert into regions (region_id,region_name) values(id,name)";
////        int hasil=stmt.executeUpdate(sisip2);
//    }

//    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 = " + keyword ;
//
//        String query="select * from regions where region_name like '%"+keyword+"%'";
//        System.out.println(query);
//        ResultSet rset = stmt.executeQuery(query);
//        result = new ArrayList();
//        while (rset.next()) {
//          Region temp = new Region(rset.getInt(1),rset.getString(2));
//            result.add(temp);
////            result.add(temp.getRegionId());
////            result.add(temp.getRegionName());
//        }
//        conn.close();
//        return result;
//    }
     

//    public static void main(String[] args) throws SQLException {
//        Region r=new Region();
////        System.out.println("isRegionExist " + r.isRegionExist(1));
////        r.addRegion(8,"Asia Timur");
//        ArrayList ar=Region.searchRegions("Asia");
//        Region or;
//        for (int i=0;i<ar.size();i++)
//        {
//            or=(Region)ar.get(i);
//
//            System.out.println(or.getRegionId()+" "+or.getRegionName());
//        }
//        conn.close();
//    }
}
//package Region;
//
//import java.sql.Connection;
//import java.sql.ResultSet;
//import java.sql.SQLException;
//import java.sql.Statement;
//import java.util.ArrayList;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author Queen AZT
 */
//public class Region {
//
//    private int region_id;
//    private String region_name;
//
//    public Region() {
//    }
//
//    public Region(int region_id, String region_name) {
//        this.region_id = region_id;
//        this.region_name = region_name;
//    }
//
//    public int getRegion_id() {
//        return region_id;
//    }
//
//    public void setRegion_id(int region_id) {
//        this.region_id = region_id;
//    }
//
//    public String getRegion_name() {
//        return region_name;
//    }
//
//    public void setRegion_name(String region_name) {
//        this.region_name = region_name;
//    }

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

import java.awt.*;
import java.awt.event.*;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;

/**
 *
 * @author admin
 */
public class JIFEmp extends javax.swing.JInternalFrame implements ActionListener {

    static int openFrameCount = 0;
    static final int xOffset = 30,  yOffset = 30;
    JPanel pa, pb, p1, p2;
    JLabel label1;
    JButton but1;
    JTextField text1;
    JTable jtabel1;

    public JIFEmp(String title) {
        super(title + (++openFrameCount), true, //resizable
                true, //closable
                true, //maximizable
                true);//iconifiable
        setSize(800, 600);
        addPanel(); //Set the window's location.
        setLocation(xOffset * openFrameCount, yOffset * openFrameCount);
    }

    public void addPanel() {
        pa = new JPanel();
        pa.setBorder(BorderFactory.createTitledBorder(
                BorderFactory.createLineBorder(Color.red), "Employee"));
        jtabel1 = new JTable();
        jtabel1.setPreferredScrollableViewportSize(new Dimension(700, 350));
        JScrollPane scrollPane = new JScrollPane(jtabel1);

        pa.add(scrollPane);
        pb = new JPanel();
        pb.setLayout(new GridLayout(2, 1));
        p1 = new JPanel();
        p1.setLayout(new GridLayout(2, 1));
        p2 = new JPanel();
        p2.setLayout(new FlowLayout());
        label1 = new JLabel("Masukkan nama : ");
        p1.add(label1);
        text1 = new JTextField();
        p1.add(text1);
        but1 = new JButton("Tampil");
        but1.addActionListener(this);
        p2.add(but1);
        pb.add(p1);
        pb.add(p2);
        this.setLayout(new GridLayout(2, 1));
        this.add(pa);
        this.add(pb);
    }

    public void actionPerformed(ActionEvent event) {
//        throw new UnsupportedOperationException("Not supported yet.");
        JButton clickedButton = (JButton) event.getSource();
        if ((clickedButton.getText()).compareTo("Tampil") == 0) {
            EmployeesTableModel e;
            try {
                System.out.println(text1.getText());
                e = new EmployeesTableModel(text1.getText());
                jtabel1.setModel(e);
                jtabel1.revalidate();
            } catch (SQLException ex) {
                Logger.getLogger(JIFEmp.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
}



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

package conectsql;

import java.sql.SQLException;
import java.util.ArrayList;
import javax.swing.table.AbstractTableModel;

/**
 *
 * @author Tatik
 */
public class RegionTableModel extends AbstractTableModel {

    String columNames[] = {"Nomor", "Regions"};
    ArrayList data,data1;

    public RegionTableModel(int id,String keyword) throws SQLException {
        Region r=new Region();
         data1 = Region.addRegion(id, keyword);
         data = Region.TampilRegion();
    }
    public int getRowCount() {
        return data.size();
    }
    public int getColumnCount() {
        return columNames.length;
    }
    @Override
    public String getColumnName(int col) {
        return columNames[col];
    }
    public Object getValueAt(int rowIndex, int columnIndex) {
        Region temp = (Region) data.get(rowIndex);
        if (columnIndex == 0) {
            return temp.getRegionId();
        } else {
            return temp.getRegionName();
        }
    }
    @Override
    public Class getColumnClass(int c) {
        return getValueAt(0, c).getClass();
    }
}

//    public static void main(String[] args) throws SQLException {
//        RegionTableModel test = new RegionTableModel("Asia");
//        System.out.println("nama :" + test.getValueAt(1, 1));
//    }


//package Region;

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

/**
 *
 * @author Queen AZT
 */
//
//import java.sql.SQLException;
//import java.util.ArrayList;
//import javax.swing.table.AbstractTableModel;

//public class RegionTableModel extends AbstractTableModel {
//    //region_id, region_name
//
//    String columNames[] = {"Region Id", "Region Name"};
//    ArrayList data, data1;
//
//
//    @SuppressWarnings("static-access")
//    public RegionTableModel(int id, String name) throws SQLException  {
//        Region r = new Region();
//  
//
//    }
//
//    public int getRowCount() {
//        return data.size();
//
//    }
//
//    public int getColumnCount() {
//        return columNames.length;
//    }
//
//    @Override
//    public String getColumnName(int col) {
//        return columNames[col];
//    }
//
//    public Object getValueAt(int rowIndex, int columnIndex) {
//      
//
//    @Override
//    public Class getColumnClass(int c) {
//        return getValueAt(0, c).getClass();
//    }
//}

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

import java.sql.*;
import oracle.jdbc.pool.OracleDataSource;

/**
 *
 * @author admin
 */
public class DataHandler {

    // string „localhost‟ anda ganti dengan IP server yang digunakan
    String jdbcUrl = "jdbc:oracle:thin:local host:orcl";
    String userid = "System";
    String password = "veraeka";
    Connection conn;
    Statement stmt;
    ResultSet rset;
    String query;
    String sqlString;

    public DataHandler() {
       
    }

    public void getDBConnection() {
           try{
               OracleDataSource ds;  
    ds  = new OracleDataSource();

    ds.setURL (jdbcUrl);
    conn  = ds.getConnection(userid, password);

    System.out.println ("Koneksi berhasil");
           }
catch

(SQLException ex) {
    System.out.println("Masih belum terkoneksi");

}
}
public void close() {
    try {
        conn.close();

} catch (SQLException ex) {
    System.out.println("Tidak bisa tutup koneksi");
    }
}

}





Tidak ada komentar:

Posting Komentar