MEMODELKAN OBYEK DAN KELAS SERTA MODEL DIMAMIS BAGIAN II DI TAHAP PERANCANGAN

1 4 MEMODELKAN OBYEK DAN KELAS SERTA MODEL DIMAMIS BAGIAN II DI TAHAP PERANCANGAN 1.1 Tujuan Praktikum 1. Praktikan mampu memahami konsep permodelan d...
Author:  Liani Kartawijaya

42 downloads 195 Views 1MB Size

Recommend Documents

25



2. Controller – ActionControl.java package controller;

import java.io.IOException;

import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;

import service.Databaseakses; import service.ManageAction;

/** * Servlet implementation class ActionControl */ public class ActionControl extends HttpServlet { private static final long serialVersionUID = 1L;

public ActionControl(){ super(); } private Databaseakses database=null; private static final byte input_barang = 0; 26

private static final byte view_barang = 0;

private String[] COMMAND = {"input_barang","view_barang"};

private int identifyAction(String action){ for (int i=0; i
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); }

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { int action = identifyAction(request.getParameter("action")); String nextPage = "";

switch (action) { case input_barang: nextPage = inputBarang(request, response); break; case view_registrasi: nextPage = viewBarang(request, response); break; default: nextPage="inputBarang.jsp"; 27

break; }

RequestDispatcher dispatcher =request.getRequestDispatcher(nextPage); dispatcher.forward(request, response); }

private String inputBarang(HttpServletRequest request, HttpServletResponse response){ String nextPage="";

String id_brg = request.getParameter("id_brg"); String nama_brg = request.getParameter("nama_brg"); String jml_brg = request.getParameter("jml_brg"); String harga_brg = request.getParameter("harga_brg"); try { nextPage = ManageAction.getInstance(database, request, response).inputDataBarang(id_brg, nama_brg, jml_brg, harga_brg); } catch (Exception e) { e.printStackTrace(); }

return nextPage; }

private String viewBarang(HttpServletRequest request, HttpServletResponse response){ String nextPage=""; try { nextPage = ManageAction.getInstance(database, request, response).viewDataBarang(); } catch (Exception e) { e.printStackTrace(); } return nextPage; } 28

}

3. Service-ManageAction.java package service;

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

import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;

import model.BarangBean;

public class ManageAction {

private Databaseakses database; private static ManageAction instance; private HttpServletResponse response; private HttpServletRequest request;

private ManageAction(Databaseakses database, HttpServletRequest request, HttpServletResponse response){ this.database = database; this.request = request; this.response = response; }

public static ManageAction getInstance(Databaseakses database, HttpServletRequest request, HttpServletResponse response){ if(instance == null){ instance = new ManageAction(database, request, response); } 29

return instance; }

public String inputDataBarang(String id_brg, String nama_brg, String jml_brg, String harga_brg){ String address=""; try { BarangBean bean = new BarangBean(); bean.setId_brg(id_brg); bean.setNama_brg(nama_brg); bean.setJml_brg(jml_brg); bean.setHarga_brg(harga_brg); String[] query = {"insert into barang (id_brg, nama_brg, jml_brg, harga_brg) values('"+id_brg+"','"+nama_brg+"','"+jml_brg+"','"+harga_brg+"')"}; database.getInstance().executeUpdateQuery(query); System.out.println(query[0]); address = "/ActionControl?action=view_barang"; } catch (Exception e) { e.printStackTrace(); } return address; }

public String viewDataBarang() throws SQLException{ String address=""; String query = "select*from barang"; ResultSet rs = database.getInstance().executeSelectQuery(query); ArrayList DataList = new ArrayList(); BarangBean barang; while(rs.next()){ barang = new BarangBean(); barang.setId_brg(rs.getString(1)); barang.setNama_brg(rs.getString(2)); barang.setJml_brg(rs.getString(3)); 30

barang.setHarga_brg(rs.getString(4)); DataList.add(barang); } request.setAttribute("viewBarang", DataList); address = "Results/success.jsp"; return address; } }

4. Model-BarangBean.java package model;

import java.io.*; import java.util.*;

public class BarangBean { private String id_brg; private String nama_brg; private String jml_brg; private String harga_brg;

public BarangBean() { id_brg=""; nama_brg=""; jml_brg=""; harga_brg=""; }

public String getId_brg() { return id_brg; } public String getNama_brg() { return nama_brg; 31

} public String getJml_brg() { return jml_brg; } public String getHarga_brg() { return harga_brg; } public void setId_brg(String id_brg) { this.id_brg =id_brg; } public void setNama_brg(String nama_brg) { this.nama_brg =nama_brg; } public void setJml_brg(String jml_brg) { this.jml_brg=jml_brg; } public void setHarga_brg(String harga_brg) { this.harga_brg=harga_brg; }

5. View Barang-success.jsp <%@ page language="java" contentType="text/html; charset=ISO-8859-1"pageEncoding="ISO-8859-1"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <jsp:useBean id="success" class="model.FormBean" scope="request"/> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> MODUL IV 32

33

ID Barang Nama Barang Jumlah Barang Harga Barang
${view.id_brg}${view.nama_brg} ${view.jml_brg} ${view.harga_brg}


34