Logika Standar

Thursday, 17 March 2016

Java Memanggil Store Procedure Oracle Insert PL/SQL

Store Procedured adalah sebuah kelompok kode SQL yang di simpan di katalog database dan dapat di panggil kemudian oleh program, trigger atau bahkan stored procedure. Sebuah Stored Procedure yang memanggil dirinya sendiri disebut rekursif stored procedure.

Untuk langkah pertama, persiapkan Database beserta Table'nya:
 - table STUDENT didalam schema KAMPUS :
CREATE TABLE "KAMPUS"."STUDENT"
 (    "NIM" VARCHAR2(20 BYTE) NOT NULL ENABLE,
  "NAME" VARCHAR2(50 BYTE),
  "ADDRESS" VARCHAR2(100 BYTE),
   CONSTRAINT "STUDENT" PRIMARY KEY ("NIM")
USING INDEX PCTFREE 10 INITRANS 2 MAXTRANS 255 COMPUTE STATISTICS
STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT FLASH_CACHE DEFAULT CELL_FLASH_CACHE DEFAULT)
TABLESPACE "TB_SPACE"  ENABLE
 ) SEGMENT CREATION IMMEDIATE
PCTFREE 10 PCTUSED 40 INITRANS 1 MAXTRANS 255 NOCOMPRESS LOGGING
STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT FLASH_CACHE DEFAULT CELL_FLASH_CACHE DEFAULT)
TABLESPACE "TB_SPACE" ;

Setelah itu, buat Store Procedure :

create or replace
PROCEDURE SP_STUDENT
(
nim_in         IN student.nim%type,
name_in        IN student.name%type,
address_in     IN student.address%type,
result_cursor  OUT SYS_REFCURSOR
)
AS
BEGIN
  INSERT INTO STUDENT  (
  nim,
  name,
  address
  )
  VALUES
  (
  nim_in,
  name_in,
  address_in
  );
 
  OPEN result_cursor FOR
  select 'success' as status from dual;
  EXCEPTION
    WHEN DUP_VAL_ON_INDEX THEN
     OPEN result_cursor FOR
        select 'error' as status from dual;
    
END;

Kemudian beralih ke Program Java, untuk memanggil Store Procedure yang telah kita buat tadi :

public Mahasiswa insert(Mahasiswa o) throws SQLException {
 
        //use store procedure oracle
        String insertMahasiswa = "{call kampus.sp_student(?,?,?,?)}";
        CallableStatement callableStatement = DatabaseUtilities.getConnection().prepareCall(insertMahasiswa);
        callableStatement.setString(1, o.getNim());
        callableStatement.setString(2, o.getNama());
        callableStatement.setString(3, o.getAlamat());
        callableStatement.registerOutParameter(4, OracleTypes.CURSOR);
        callableStatement.executeUpdate();
        ResultSet rs = (ResultSet) callableStatement.getObject(4);
        if(rs.next()){
        o.setStatus(rs.getString("status"));
        System.out.println(rs.getString("status"));
        }
        return o;
    }

Sekian Proses pemanggilan Store Procedure

Wednesday, 9 March 2016

Face Recognition

Membuat aplikasi face Recognition,

import java.awt.*;
import java.awt.image.*;
import java.awt.event.*;

 class ImageFileViewer extends Frame implements WindowListener, ActionListener, MouseListener {

    // control buttons

    Button ExitButton;
    ImageCanvas canvas;

    public ImageFileViewer() {

    // constructor--initialize frame

    super("PPM/JPG File Viewer (Click on canvas to load image)");
    setup();
    }

    // initialize frame contents (buttons, etc.)

    public void setup() {

    Panel CanvasPanel;   // panel for drawing canvas
    Panel ButtonPanel;   // main button panel

    setSize(500,400);

    setLayout(new BorderLayout());

    this.setBackground(Color.gray);
    this.setForeground(Color.black);

    // initialize buttons, make frame responsible for actions

    ExitButton = new Button("Exit");
    ExitButton.addActionListener(this);

    // initialize canvas

    canvas = new ImageCanvas();
    canvas.addMouseListener(this);
    canvas.setSize(400, 300);
    canvas.setBackground(Color.black);
    canvas.setForeground(Color.white);

    ButtonPanel = new Panel();

    // buttons will be centered

    ButtonPanel.setLayout(new FlowLayout(FlowLayout.CENTER));

    // add buttons to button panel

    ButtonPanel.add(ExitButton);

    // add canvas to canvas panel centered

    CanvasPanel = new Panel();
    CanvasPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
    CanvasPanel.add(canvas);

    // put main panels into the frame

    add("North", new Label(""));
    add("South", ButtonPanel);
    add("Center", CanvasPanel);

    // frame is hidden until you show it

    setVisible(true);

    // care about window events

    addWindowListener(this);
    }

    // under the new event handling model, you have to "implement" all
    // of these window operations if you're interested in any of them.
    // We only care about window close events.

    public void windowDeiconified(WindowEvent event) {
    }
    public void windowIconified(WindowEvent event) {
    }
    public void windowActivated(WindowEvent event) {
    }
    public void windowDeactivated(WindowEvent event) {
    }
    public void windowOpened(WindowEvent event) {
    }
    public void windowClosed(WindowEvent event) {
    }

    public void windowClosing(WindowEvent event) {

    // handle user closing frame

    setVisible(false); // hide frame first...
    dispose();         // release resources and destroy frame
    System.exit(0);    // exit program

    }

    public void mouseDragged(MouseEvent event) {

    }


    public void mousePressed(MouseEvent event) {

    // show file dialog for loading PPM when the user clicks...

        FileDialog f = new FileDialog(this, "Open a JPG/PPM image file",
                      FileDialog.LOAD);
        f.setFile("*.*");
        f.show();
        if (f.getFile() == null) {
        System.out.println("Image open cancelled.");
        }
        else {
              try {
                xxxFile file = null;
                String temp = (f.getDirectory()+f.getFile()).toLowerCase();

                temp = temp.substring(temp.lastIndexOf('.')+1,temp.length());
                if (temp.equals("jpg") || temp.equals("jpeg"))
                    file = new JPGFile(f.getDirectory() + f.getFile());
                else if (temp.equals("ppm") || temp.equals("pnm"))
                    file = new PPMFile(f.getDirectory() + f.getFile());

        setImage(file.getBytes(), file.getWidth(), file.getHeight());
              } catch (Exception e) { e.printStackTrace(); }
        }


    }
    public void setImage(byte[] b, int w, int h) {
      canvas.readImage(b, w, h);
    }

    public void setImage(int[] b, int w, int h) {
      canvas.readImage(b, w, h);
    }
    public void setImage(double[] b, int w, int h) {
      canvas.readImage(b, w, h);
    }
    public void mouseReleased(MouseEvent event) {
    }
    public void mouseMoved(MouseEvent event) {
    }
    public void mouseEntered(MouseEvent event) {
    }
    public void mouseExited(MouseEvent event) {
    }
    public void mouseClicked(MouseEvent event) {
    }

    public void actionPerformed(ActionEvent event) {

    // handle pushbutton events--first get source of event

    Object source = event.getSource();

    if (source == ExitButton) {
        setVisible(false); // hide frame first...
        dispose();         // release resources and destroy frame
        System.exit(0);    // exit program
    }
    }


}