Membuat Projek Sederhana Pada JSP - Part 02 - sopian aji

Terbaru

Rabu, 08 April 2020

Membuat Projek Sederhana Pada JSP - Part 02


1. dengan disain projek sebagai berikut:
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix="c"uri="http://java.sun.com/jsp/jstl/core"%>
<%@taglib prefix="sql"uri="http://java.sun.com/jsp/jstl/sql"%>
<sql:setDataSource var="dataSource"
                   driver="com.mysql.jdbc.Driver"
                   url="jdbc:mysql://localhost:3306/masterprojekbaru"
                   user="root"password=""/>


<!DOCTYPE html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>JSP Page</title>
</head>
<body>
    
    <a href="content.jsp">Home</a> | <a href="content.jsp?buka=member_read">Member</a><br><br>   

<c:choose>
    <c:when test="${param.buka=='home'}">
        <%@include file="home.jsp" %>
    </c:when> 
    <c:when test="${param.buka=='member_read'}">
        <%@include file="member_read.jsp" %>
    </c:when> 
    <c:when test="${param.buka=='member_create'}">
        <%@include file="member_create.jsp" %>
    </c:when> 
    <c:when test="${param.buka=='member_edit'}">
        <%@include file="member_edit.jsp" %>
    </c:when> 
    <c:otherwise>
        <%@include file="home.jsp" %>
    </c:otherwise>
</c:choose>

</body>
</html>

2. form read
<%@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>
        <a href="content.jsp?buka=member_create"><button>Tambah</button></a>
        <table border="1" width="70%">
            <tr>
                <th>No</th>
                <th>Nama</th>
                <th>Hp</th>
                <th>email</th>
                <th>Aksi</th>
            </tr>
            <%
                String id_member=null;
                String nama_member=null;
                String hp_member=null;
                String email_member=null;
                int no=1;
                ResultSet rs=null;
                koneksi kon=new koneksi();
                rs=kon.stmt.executeQuery("SELECT*FROM member ORDER BY id_member desc");
                while(rs.next())
                {
                    id_member=rs.getString("id_member");
                    nama_member=rs.getString(2);
                    hp_member=rs.getString(3);
                    email_member=rs.getString(4);
            %>
            <tr>
                <td><% out.println(no++); %></td>
                <td><% out.println(nama_member); %></td>
                <td><% out.println(hp_member); %></td>
                <td><% out.println(email_member); %></td>
                <td><a href="content.jsp?buka=member_edit&id_member=<% out.print(id_member); %>">Ubah</a> 
                    |<a href="SrvMember?aksi=Delete&id_member=<% out.print(id_member); %>">Hapus</a>
            </tr>
            <%  } %>
        </table>
    </body>
</html>

3. disain form create
<%@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>
        <form method="POST" action="SrvMember">
            <h3>Tambah Member</h3>
            <%
                try {
                    ResultSet rs = null;
                    koneksi kon = new koneksi();
                    rs = kon.stmt.executeQuery("select max((id_member)) as no FROM member");
                    while (rs.next()) {
                        rs.last();
                        int autonokm = rs.getInt(1) + 1;
                        String nomorkm = String.valueOf(autonokm);
                        int noLong = nomorkm.length();
                        for(int a=1; a < 6 - noLong; a++) {
                            nomorkm = "" + nomorkm;
                        }
                        String nomerkm ="" + nomorkm;
                        //String nomerkm ="USR" + nomorkm;
                         out.println("<input type='hidden' value='" + nomerkm +"'name='id_member'>");
                    }
                } catch (Exception e) {
                    out.println(e);
                }
            %>
            Nama Member <input type="text" name="nama_member"><br>
            HP <input type="text" name="hp_member"><br>
            Email <input type="text" name="email_member"><br>
            <br>
            <button type="submit" name="aksi" value="Simpan">Simpan</button>
            <button type="reset" onclick="history.go(-1)">Batal</button>
        </form>
    </body>
</html>

4. disain form edit
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="control.koneksi" %>
<%@page import="java.sql.*" %>
<% 
String id_member=request.getParameter("id_member");
String nama_member=null;
String hp_member=null;
String email_member=null;
ResultSet rs=null;
koneksi kon=new koneksi();
rs=kon.stmt.executeQuery("SELECT*FROM member WHERE id_member='"+id_member+"'");
if(rs.next())
{
id_member=rs.getString("id_member");
nama_member=rs.getString(2);
hp_member=rs.getString(3);
email_member=rs.getString(4);
}
%>
<!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>Tambah Member</h3>
            Kode Member <input type="text" disabled="" name="id_member" value="<%=id_member %>">
            <input type="text" hidden="" name="id_member" value="<%=id_member %>">
            <br>
            Nama Member <input type="text" name="nama_member" value="<%=nama_member %>"><br>
            HP <input type="text" name="hp_member" value="<%=hp_member %>"><br>
            Email <input type="text" name="email_member" value="<%=email_member %>"><br>
            <button type="submit" name="aksi" value="Update">Perbaharui</button>
            <button type="reset" onclick="history.go(-1)">Batal</button>
        </form>
    </body>
</html>


untuk lebih jelasnya lihat tutorial klik disini



Tidak ada komentar:

Posting Komentar