Jangn bingung mulai dari mana ya belajar JSP mari kita belajar CRUD (Create, Read, Update dan Delete) JSP benar-benar dari dasar sekali. Namuan pada pembahasan ini kita akan focus pada Create terlebih dulu, oke kita langsung mulai saja ya:
1. membuat databases dan tabel, buat database dengan nama dbbelajarjsp dengan table member sebagai berikut:
lihat tutorialnya klik disni
2. buat nama projek nya bebas teman-teman
3. membuat model member.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 model;
/**
*
* @author Sopian Aji
*/
public class member {
private String kd_member;
private String nama_member;
public String getKd_member() {
return kd_member;
}
public void setKd_member(String kd_member) {
this.kd_member = kd_member;
}
public String getNama_member() {
return nama_member;
}
public void setNama_member(String nama_member) {
this.nama_member = nama_member;
}
}
lihat tutorialnya klik disni* 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 model;
/**
*
* @author Sopian Aji
*/
public class member {
private String kd_member;
private String nama_member;
public String getKd_member() {
return kd_member;
}
public void setKd_member(String kd_member) {
this.kd_member = kd_member;
}
public String getNama_member() {
return nama_member;
}
public void setNama_member(String nama_member) {
this.nama_member = nama_member;
}
}
4. membuat control koneksi.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 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;
/**
*
* @author Sopian Aji
*/
public class koneksi {
Connection conn=null;
public Statement stmt=null;
ResultSet rs;
public koneksi()throws ClassNotFoundException, SQLException{
String dbName="dbbelajarjsp";
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();
}
}
* 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 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;
/**
*
* @author Sopian Aji
*/
public class koneksi {
Connection conn=null;
public Statement stmt=null;
ResultSet rs;
public koneksi()throws ClassNotFoundException, SQLException{
String dbName="dbbelajarjsp";
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 disan form create member_create.jsp
<%--
Document : member_create
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>
<form method="POST" action="SrvMember">
<h3>Tambah Member</h3>
Kode Member <input type="text" name="kd_member"><br>
Nama Member <input type="text" name="nama_member"><br>
<br>
<button type="submit" name="aksi" value="Simpan">Simpan</button>
<button type="reset" onclick="history.go(-1)">Batal</button>
</form>
</body>
</html>
Document : member_create
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>
<form method="POST" action="SrvMember">
<h3>Tambah Member</h3>
Kode Member <input type="text" name="kd_member"><br>
Nama Member <input type="text" name="nama_member"><br>
<br>
<button type="submit" name="aksi" value="Simpan">Simpan</button>
<button type="reset" onclick="history.go(-1)">Batal</button>
</form>
</body>
</html>
lihat tutorialnya klik disni
6. membuat 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');"
+ "</script>");
}
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>
}
lihat tutorialnya klik disni
* 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');"
+ "</script>");
}
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