Membuat Projek Sederhana Pada JSP - Part 01 - sopian aji

Terbaru

Rabu, 08 April 2020

Membuat Projek Sederhana Pada JSP - Part 01



1. membuat databases dan tabel, buat database dengan nama dbprojekjsp dengan table member sebagai berikut:






2. download masterprojekbaru download disini
3. membuat model
package model;

public class member {
    private String id_member;
    private String nama_member;
    private String hp_member;
    private String email_member;

    public String getId_member() {
        return id_member;
    }

    public void setId_member(String id_member) {
        this.id_member = id_member;
    }

    public String getNama_member() {
        return nama_member;
    }

    public void setNama_member(String nama_member) {
        this.nama_member = nama_member;
    }

    public String getHp_member() {
        return hp_member;
    }

    public void setHp_member(String hp_member) {
        this.hp_member = hp_member;
    }

    public String getEmail_member() {
        return email_member;
    }

    public void setEmail_member(String email_member) {
        this.email_member = email_member;
    }

    public void getHp_member(String parameter) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    public void getEmail_member(String parameter) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    public void getId_member(String parameter) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }
    
}
4. membuat koneksi
package control;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

public class koneksi {
    Connection conn=null;
    public Statement stmt=null;
    ResultSet rs;
    
    public koneksi()throws ClassNotFoundException, SQLException{
    String dbName="dbprojekjsp";
     String uName = "root";
        String pass = "";
        String URL = "jdbc:mysql://localhost/" + dbName;
        Class.forName("com.mysql.jdbc.Driver");
        try {
            conn = (Connection) DriverManager.getConnection(URL, uName, pass);
            stmt = (Statement) conn.createStatement();
        } 
catch (SQLException ex) {
            Logger.getLogger(koneksi.class.getName()).log(Level.SEVERE,null,ex);
        } 
    }
    
    public void close()throws SQLException{
        conn.close();
        stmt.close();
    }
    
}
5.membuat Servlet
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;

public class SrvMember extends HttpServlet {


    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.setId_member(request.getParameter("id_member"));
               mbr.setNama_member(request.getParameter("nama_member"));
               mbr.setHp_member(request.getParameter("hp_member"));
               mbr.setEmail_member(request.getParameter("email_member"));
               switch(aksi){
                   case "Simpan":
                       pstmt=kon.conn.prepareStatement("INSERT INTO member values('"
                               +mbr.getId_member()+"','"
                               +mbr.getNama_member()+"','"
                               +mbr.getHp_member()+"','"
                               +mbr.getEmail_member()+"')");
                       result=pstmt.executeUpdate();
                       if(result > 0) {
                            out.println("<script>" + "alert('Data berhasil tersimpan');"
                                    + "document.location='content.jsp?buka=member_read';"
                                    + "</script>");
                        }
                       break;
                      case"Delete":
                         pstmt=kon.conn.prepareStatement("DELETE FROM member WHERE id_member='"
                                 +mbr.getId_member()+"'");
                         result=pstmt.executeUpdate();
                         if(result > 0) {
                            out.println("<script>" + "alert('Data telah dihapus');"
                                    + "document.location='content.jsp?buka=member_read';"
                                    + "</script>");
                        }  
                       break;
                       case "Update":
                          pstmt=kon.conn.prepareStatement("UPDATE member SET "
                                  + "nama_member='"+ mbr.getNama_member()
                                  + "',hp_member='"+ mbr.getHp_member()
                                  + "',email_member='"+ mbr.getEmail_member()
                                  + "'WHERE id_member='" + mbr.getId_member()
                                  + "'");
                         result=pstmt.executeUpdate();
                         if(result > 0) {
                            out.println("<script>" + "alert('Data telah diperbaharui');"
                                    + "document.location='content.jsp?buka=member_read';"
                                    + "</script>");
                        }  
               }
           }
        }
    }

    // <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>

}

untuk lebih jelasnya lihat tutorial klik disini

Tidak ada komentar:

Posting Komentar