CRUD NETBEANS JAVA USING JSP AND SERVLETS WITH MYSQL DATABASE #3

CRUD Netbeans Java Using JSP and Servlet With MySQL Database #3

Well.. ketemu lagi di hari yang sama namun dengan jam dan menit yang berbeda. Masih melanjutkan pembahasan CRUD Java web menggunakan Netbeans, beberapa jam lalu telah dibahas mengenai update data master_akun. Nah, saat ini kita akan selesaikan pembahasan CRUD ini dengan menambahkan code yang digunakan untuk menghapus data master akun didalam Servlet. Untuk yang baru saja bergabung, silahkan baca artikel sebelumnya yaitu CRUD Netbeans Java Using JSP and Servlets With MySQL Database #2.

Ok langsung saja ke materi, seperti yang kita ketahui pembahasan Java ini adalah mengenai CRUD menggunakan JSP dan Servlet, dimana setiap proses CRUD  yang berhubungan dengan database dilakukan didalam Servlet. Terdapat dua metode didalam Servlet yaitu doGet dan doPost. Lalu apa perbedaannya?

doGet digunakan untuk meletakkan code tampil dan hapus data, sedangkan doPost untuk meletakkan code insert dan update data.

Buka kembali class akunServlet.java kemudian tambahkan code hapus data tepat dibawah doGet.


@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

Akun akun = new Akun();

String URL = "jdbc:mysql://localhost:3306/sia";
String USERNAME = "root";
String PASSWORD = "";

Connection koneksi = null;
PreparedStatement pstmt = null;
int result = 0;

try (PrintWriter out = response.getWriter()) {
Class.forName("com.mysql.jdbc.Driver");
koneksi = DriverManager.getConnection(URL, USERNAME, PASSWORD);

String aksi = request.getParameter("aksi");
if (aksi != null) {
akun.setKode_akun(request.getParameter("kode"));
akun.setNama_akun(request.getParameter("nama"));
akun.setJenis_akun(request.getParameter("jenis"));
akun.setSaldo_normal(request.getParameter("saldo"));

switch (aksi) {
/* hapus data master akun */
case "Delete":
pstmt = koneksi.prepareStatement("DELETE FROM "
+ " master_akun WHERE kode_akun = ?");

pstmt.setString(1, akun.getKode_akun());
result = pstmt.executeUpdate();

if (result > 0){
out.println("<script> "
+ " alert('Data akun telah di hapus');"
+ " document.location='tampil_akun.jsp';"
+ " </script>");
}
/*----------------------------------------------------*/
default:
break;
}
}
} catch (ClassNotFoundException | SQLException ex) {
Logger.getLogger(akunServlet.class.getName()).log(Level.SEVERE, null, ex);
}

}

Ini code versi lengkapnya dari class akunServlet.java

akunServlet.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 javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import model.Akun;
import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
*
* @author kutuonline
*/
@WebServlet(name = "akunServlet", urlPatterns = {"/akunServlet"})
public class akunServlet 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
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

}

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

Akun akun = new Akun();

String URL = "jdbc:mysql://localhost:3306/sia";
String USERNAME = "root";
String PASSWORD = "";

Connection koneksi = null;
PreparedStatement pstmt = null;
int result = 0;

try (PrintWriter out = response.getWriter()) {
Class.forName("com.mysql.jdbc.Driver");
koneksi = DriverManager.getConnection(URL, USERNAME, PASSWORD);

String aksi = request.getParameter("aksi");
if (aksi != null) {
akun.setKode_akun(request.getParameter("kode"));
akun.setNama_akun(request.getParameter("nama"));
akun.setJenis_akun(request.getParameter("jenis"));
akun.setSaldo_normal(request.getParameter("saldo"));

switch (aksi) {
/* hapus data master akun */
case "Delete":
pstmt = koneksi.prepareStatement("DELETE FROM "
+ " master_akun WHERE kode_akun = ?");

pstmt.setString(1, akun.getKode_akun());
result = pstmt.executeUpdate();

if (result > 0){
out.println("<script> "
+ " alert('Data akun telah di hapus');"
+ " document.location='tampil_akun.jsp';"
+ " </script>");
}
/*----------------------------------------------------*/
default:
break;
}
}
} catch (ClassNotFoundException | SQLException ex) {
Logger.getLogger(akunServlet.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
*/
/**
* 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
*/
/**
* 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 {

Akun akun = new Akun();

String URL = "jdbc:mysql://localhost:3306/sia";
String USERNAME = "root";
String PASSWORD = "";

Connection koneksi = null;
PreparedStatement pstmt = null;
int result = 0;

try (PrintWriter out = response.getWriter()) {
Class.forName("com.mysql.jdbc.Driver");
koneksi = DriverManager.getConnection(URL, USERNAME, PASSWORD);

String aksi = request.getParameter("aksi");
if (aksi != null) {
akun.setKode_akun(request.getParameter("kode"));
akun.setNama_akun(request.getParameter("nama"));
akun.setJenis_akun(request.getParameter("jenis"));
akun.setSaldo_normal(request.getParameter("saldo"));

switch (aksi) {
case "Insert":
pstmt = koneksi.prepareStatement("INSERT INTO master_akun "
+ " VALUES (?, ?, ?, ?)");
pstmt.setString(1, akun.getKode_akun());
pstmt.setString(2, akun.getNama_akun());
pstmt.setString(3, akun.getJenis_akun());
pstmt.setString(4, akun.getSaldo_normal());

result = pstmt.executeUpdate();
if (result > 0) {
out.println("<script> "
+ "alert('Data akun telah ditambahkan');"
+ "document.location='add_akun.jsp';"
+ "</script>");
}
break;

/* update data master akun */
case "Update":
pstmt = koneksi.prepareStatement("UPDATE master_akun SET"
+ " nama_akun = ?,"
+ " jenis_akun = ?,"
+ " saldo_normal = ?"
+ " WHERE kode_akun = ?");

pstmt.setString(1, akun.getNama_akun());
pstmt.setString(2, akun.getJenis_akun());
pstmt.setString(3, akun.getSaldo_normal());
pstmt.setString(4, akun.getKode_akun());

result = pstmt.executeUpdate();
if (result > 0) {
out.println("<script> "
+ " alert('Data akun telah di update');"
+ " document.location='tampil_akun.jsp';"
+ " </script>");
}
break;
/*----------------------------------------------------*/

default:
break;
}
}
} catch (ClassNotFoundException | SQLException ex) {
Logger.getLogger(akunServlet.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>
}

Dengan selesainya penambahan code hapus ini maka selesai pula pembahasan CRUD Java web menggunakan JSP dan Servlet.

Semoga bermanfaat.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s