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

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

Selamat siang.. ketemu lagi bersama saya di tutorial CRUD Java web menggunakan Netbeans. Masih melanjutkan pembahasan CRUD Java Netbeans yaitu update data master_akun. Pada postingan sebelumnya telah dibahas mengenai CRUD insert dan retrieve (menampilkan data) tabel master_akun. Untuk yang baru saja bergabung di artikel ini, ada baiknya baca terlebih dulu postingan sebelumnya disini agar dapat dengan mudah mengikuti tutorial kali ini.

Sebelumnya telah dibahas mengenai pembuatan database dan tabel yang dibutuhkan serta pembuatan file JSP untuk insert dan menampilkan data tabel master_akun. Pada kesempatan kali ini, kita akan membuat file JSP edit_akun dan menambahkan perintah untuk update data didalam Servlet.

Ok, langsung aja buat file JSP dengan nama edit_akun.jsp.

edit_akun.jsp

File name: edit_akun


<%--
Document : edit_akun
Created on : Dec 8, 2016, 12:39:31 PM
Author : kutuonline
--%>

<%@page import="java.sql.*, model.Akun" %>
<%
Akun akun = new Akun();

/* koneksi ke database */
Connection koneksi = null;
Statement stmt = null;
ResultSet rs = null;
Class.forName("com.mysql.jdbc.Driver");
koneksi = DriverManager.getConnection("jdbc:mysql://localhost:3306/sia", "root", "");
stmt = koneksi.createStatement();
String kode = request.getParameter("kode");

if(kode != null){
rs = stmt.executeQuery("SELECT * FROM master_akun "
+ " WHERE kode_akun = '" + kode + "' ");

if(rs.next()){
akun.setKode_akun(rs.getString("kode_akun"));
akun.setNama_akun(rs.getString("nama_akun"));
akun.setJenis_akun(rs.getString("jenis_akun"));
akun.setSaldo_normal(rs.getString("saldo_normal"));
}
}
%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Edit Akun</title>
</head>
<body>
<h1>Ubah Data Akun</h1>
<form action="akunServlet" method="POST">
<table border="0" cellpadding="4">
<tbody>
<tr>
<td valign="top">Jenis Akun</td>
<td><input type="text" name="jenis" size="20" value="<%=akun.getJenis_akun()%>"><br>
<b>Daftar Jenis Akun</b>
<ul>
<li>Kas/Bank</li>
<li>Aktiva Lancar</li>
<li>Aktiva Tetap</li>
<li>Kewajiban</li>
<li>Modal</li>
<li>Pendapatan</li>
<li>Beban</li>
</ul>
</td>
</tr>

<tr>
<td>Kode *</td>
<td><input type="text" name="kode" size="5" value="<%=akun.getKode_akun() %>" readonly="readonly"></td>
</tr>

<tr>
<td>Nama Akun</td>
<td><input type="text" name="nama" size="20" value="<%=akun.getNama_akun() %>"></td>
</tr>

<tr>
<td>Saldo Normal</td>
<td>
<select name="saldo">
<% if (akun.getSaldo_normal().equalsIgnoreCase("Debet")) { %>
<option value="Debet" selected>Debet</option>
<option value="Kredit">Kredit</option>
<% } else { %>
<option value="Debet">Debet</option>
<option value="Kredit" selected>Kredit</option>
<% } %>
</select>
</td>
</tr>

<tr>
<td colspan="2">
<input type="submit" name="aksi" value="Update">&nbsp;
<input type="reset" name="reset" value="Batal" OnClick="self.history.back();">
</td>
</tr>
</tbody>
</table>
</form>
</body>
</html>

Berikut penampakan file edit_akun.jsp. Prosesnya adalah jalankan file tampil_akun.jsp terlebih dahulu, kemudian klik aksi Edit pada salah satu record yang akan di ubah datanya.

Misal pada contoh dibawah ini, saya akan merubah data akun Peralatan.

Choose Record

Setelah klik aksi Edit pada record Peralatan dengan kode akun 12000 diatas, maka akan ditampilkan file edit_akun.jsp seperti dibawah ini.

Edit Akun

Selanjutnya, buka kembali class akunServlet.java, tambahkan perintah untuk update data persis dibawah statement break; case Insert. Berikut hasil penambahan script pada 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
*/
/* dinonaktifkan
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
*/
/**
* 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;

/* perintah yang ditambahkan - 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>
}

Demikian tutorial CRUD edit data akun kali ini. Pada pertemuan berikutnya akan dibahas mengenai hapus data master akun.

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