kita lanjutkan materi pada Belajar Dasar JSP Membuat CRUD - Part 01 kali ini kita selesaikan Read, Update dan Delete
1. disan form read member_read.jsp
<%--
Document : member_read
Created on : Mar 25, 2020, 1:15:11 PM
Author : Sopian Aji
--%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="control.koneksi" %>
<%@page import="java.sql.*" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<h3>Data Member</h3>
<a href="member_create.jsp"> <button>Tambah</button> </a>
<table border="1" width="70%">
<tr>
<th>No</th>
<th>Kode</th>
<th>Nama</th>
<th>Aksi</th>
</tr>
<%
String kd_member=null;
String nama_member=null;
int no=1;
ResultSet rs=null;
koneksi kon=new koneksi();
rs=kon.stmt.executeQuery("SELECT*FROM member ORDER BY kd_member desc");
while(rs.next())
{
kd_member=rs.getString("kd_member");
nama_member=rs.getString(2);
%>
<tr>
<td><% out.println(no++); %></td>
<td><% out.println(kd_member); %></td>
<td><% out.println(nama_member); %></td>
<td>
<a href="member_edit.jsp?kd_member=<% out.print(kd_member); %>">Edit</a>
| <a href="SrvMember?aksi=Delete&kd_member=<% out.print(kd_member); %>">Hapus</a>
</td>
</tr>
<% } %>
</table>
<% kon.close(); %>
</body>
</html>
2. disan form read edit_read.jspDocument : member_read
Created on : Mar 25, 2020, 1:15:11 PM
Author : Sopian Aji
--%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="control.koneksi" %>
<%@page import="java.sql.*" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<h3>Data Member</h3>
<a href="member_create.jsp"> <button>Tambah</button> </a>
<table border="1" width="70%">
<tr>
<th>No</th>
<th>Kode</th>
<th>Nama</th>
<th>Aksi</th>
</tr>
<%
String kd_member=null;
String nama_member=null;
int no=1;
ResultSet rs=null;
koneksi kon=new koneksi();
rs=kon.stmt.executeQuery("SELECT*FROM member ORDER BY kd_member desc");
while(rs.next())
{
kd_member=rs.getString("kd_member");
nama_member=rs.getString(2);
%>
<tr>
<td><% out.println(no++); %></td>
<td><% out.println(kd_member); %></td>
<td><% out.println(nama_member); %></td>
<td>
<a href="member_edit.jsp?kd_member=<% out.print(kd_member); %>">Edit</a>
| <a href="SrvMember?aksi=Delete&kd_member=<% out.print(kd_member); %>">Hapus</a>
</td>
</tr>
<% } %>
</table>
<% kon.close(); %>
</body>
</html>
<%--
Document : member_create
Created on : Mar 25, 2020, 11:59:13 AM
Author : Sopian Aji
--%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="control.koneksi" %>
<%@page import="java.sql.*" %>
<%
String kd_member=request.getParameter("kd_member");
String nama_member=null;
ResultSet rs=null;
koneksi kon=new koneksi();
rs=kon.stmt.executeQuery("SELECT*FROM member WHERE kd_member='" +kd_member+ "'");
if(rs.next())
{
kd_member=rs.getString("kd_member");
nama_member=rs.getString(2);
}
%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<form method="POST" action="SrvMember">
<h3>Ubah Member</h3>
Kode Member <input type="text" name="kd_member" value="<%=kd_member %>" disabled="">
<input type="text" name="kd_member" value="<%=kd_member %>" hidden="">
<br>
Nama Member <input type="text" name="nama_member" value="<%=nama_member %>"><br>
<br>
<button type="submit" name="aksi" value="Update">Perbaharui</button>
<button type="reset" onclick="history.go(-1)">Batal</button>
</form>
</body>
</html>
Document : member_create
Created on : Mar 25, 2020, 11:59:13 AM
Author : Sopian Aji
--%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="control.koneksi" %>
<%@page import="java.sql.*" %>
<%
String kd_member=request.getParameter("kd_member");
String nama_member=null;
ResultSet rs=null;
koneksi kon=new koneksi();
rs=kon.stmt.executeQuery("SELECT*FROM member WHERE kd_member='" +kd_member+ "'");
if(rs.next())
{
kd_member=rs.getString("kd_member");
nama_member=rs.getString(2);
}
%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<form method="POST" action="SrvMember">
<h3>Ubah Member</h3>
Kode Member <input type="text" name="kd_member" value="<%=kd_member %>" disabled="">
<input type="text" name="kd_member" value="<%=kd_member %>" hidden="">
<br>
Nama Member <input type="text" name="nama_member" value="<%=nama_member %>"><br>
<br>
<button type="submit" name="aksi" value="Update">Perbaharui</button>
<button type="reset" onclick="history.go(-1)">Batal</button>
</form>
</body>
</html>
3. update kembali source code pada Servlet SrvMember.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package control;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import model.member;
/**
*
* @author Sopian Aji
*/
public class SrvMember extends HttpServlet {
/**
* Processes requests for both HTTP <code>GET</code> and <code>POST</code>
* methods.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
* @throws java.lang.ClassNotFoundException
* @throws java.sql.SQLException
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException, ClassNotFoundException, SQLException {
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter()) {
/* TODO output your page here. You may use following sample code. */
member mbr=new member();
koneksi kon=new koneksi();
PreparedStatement pstmt=null;
int result=0;
String aksi=request.getParameter("aksi");
if(aksi!=null){
mbr.setKd_member(request.getParameter("kd_member"));
mbr.setNama_member(request.getParameter("nama_member"));
switch(aksi){
case "Simpan":
pstmt=kon.conn.prepareStatement("INSERT INTO member values('"
+mbr.getKd_member()+"','"
+mbr.getNama_member()+"')");
result=pstmt.executeUpdate();
if(result > 0) {
out.println("<script>" + "alert('Data berhasil tersimpan');"
+ "document.location='member_read.jsp';"
+ "</script>");
}
break;
case"Delete":
pstmt=kon.conn.prepareStatement("DELETE FROM member WHERE "
+ "kd_member='"+mbr.getKd_member()+"'");
result=pstmt.executeUpdate();
if(result>0){
out.println("<script>" + "alert('Data telah dihapus');"
+ "document.location='member_read.jsp';"
+ "</script>");
}
break;
case"Update":
pstmt = kon.conn.prepareStatement("UPDATE member SET "
+ "nama_member='" + mbr.getNama_member()
+ "' where kd_member='" + mbr.getKd_member()
+ "'");
result = pstmt.executeUpdate();
if(result > 0) {
out.println("<script>" + "alert('Data telah diperbaharui');"
+ "document.location='member_read.jsp';"
+ "</script>");
}
default:
break;
}
}
}
}
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
* Handles the HTTP <code>GET</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
processRequest(request, response);
} catch (ClassNotFoundException ex) {
Logger.getLogger(SrvMember.class.getName()).log(Level.SEVERE, null, ex);
} catch (SQLException ex) {
Logger.getLogger(SrvMember.class.getName()).log(Level.SEVERE, null, ex);
}
}
/**
* Handles the HTTP <code>POST</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
processRequest(request, response);
} catch (ClassNotFoundException ex) {
Logger.getLogger(SrvMember.class.getName()).log(Level.SEVERE, null, ex);
} catch (SQLException ex) {
Logger.getLogger(SrvMember.class.getName()).log(Level.SEVERE, null, ex);
}
}
/**
* Returns a short description of the servlet.
*
* @return a String containing servlet description
*/
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>
}
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package control;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import model.member;
/**
*
* @author Sopian Aji
*/
public class SrvMember extends HttpServlet {
/**
* Processes requests for both HTTP <code>GET</code> and <code>POST</code>
* methods.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
* @throws java.lang.ClassNotFoundException
* @throws java.sql.SQLException
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException, ClassNotFoundException, SQLException {
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter()) {
/* TODO output your page here. You may use following sample code. */
member mbr=new member();
koneksi kon=new koneksi();
PreparedStatement pstmt=null;
int result=0;
String aksi=request.getParameter("aksi");
if(aksi!=null){
mbr.setKd_member(request.getParameter("kd_member"));
mbr.setNama_member(request.getParameter("nama_member"));
switch(aksi){
case "Simpan":
pstmt=kon.conn.prepareStatement("INSERT INTO member values('"
+mbr.getKd_member()+"','"
+mbr.getNama_member()+"')");
result=pstmt.executeUpdate();
if(result > 0) {
out.println("<script>" + "alert('Data berhasil tersimpan');"
+ "document.location='member_read.jsp';"
+ "</script>");
}
break;
case"Delete":
pstmt=kon.conn.prepareStatement("DELETE FROM member WHERE "
+ "kd_member='"+mbr.getKd_member()+"'");
result=pstmt.executeUpdate();
if(result>0){
out.println("<script>" + "alert('Data telah dihapus');"
+ "document.location='member_read.jsp';"
+ "</script>");
}
break;
case"Update":
pstmt = kon.conn.prepareStatement("UPDATE member SET "
+ "nama_member='" + mbr.getNama_member()
+ "' where kd_member='" + mbr.getKd_member()
+ "'");
result = pstmt.executeUpdate();
if(result > 0) {
out.println("<script>" + "alert('Data telah diperbaharui');"
+ "document.location='member_read.jsp';"
+ "</script>");
}
default:
break;
}
}
}
}
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
* Handles the HTTP <code>GET</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
processRequest(request, response);
} catch (ClassNotFoundException ex) {
Logger.getLogger(SrvMember.class.getName()).log(Level.SEVERE, null, ex);
} catch (SQLException ex) {
Logger.getLogger(SrvMember.class.getName()).log(Level.SEVERE, null, ex);
}
}
/**
* Handles the HTTP <code>POST</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
processRequest(request, response);
} catch (ClassNotFoundException ex) {
Logger.getLogger(SrvMember.class.getName()).log(Level.SEVERE, null, ex);
} catch (SQLException ex) {
Logger.getLogger(SrvMember.class.getName()).log(Level.SEVERE, null, ex);
}
}
/**
* Returns a short description of the servlet.
*
* @return a String containing servlet description
*/
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>
}
Tidak ada komentar:
Posting Komentar