Commit 1f624fb1 authored by Dio Harvandy's avatar Dio Harvandy

Latihan 5

Pembuatan koneksi dan CRUD database
parent a46f98ed
package com.latihan.web;
public class Latihan5_Data {
private int id_bank;
private String kode;
private String nama;
public int getId_bank() {
return id_bank;
}
public void setId_bank(int id_bank) {
this.id_bank = id_bank;
}
public String getKode() {
return kode;
}
public void setKode(String kode) {
this.kode = kode;
}
public String getNama() {
return nama;
}
public void setNama(String nama) {
this.nama = nama;
}
}
package com.latihan.web;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class Latihan5_conncectDatabase {
protected static Connection initializeDatabase() throws SQLException, ClassNotFoundException{
String dbDriver = "com.mysql.cj.jdbc.Driver";
String dbUrl = "jdbc:mysql://localhost/";
String dbName = "java_minggu_ketiga";
String dbUsername = "root";
String dbPassword = "root";
Class.forName(dbDriver);
Connection con = DriverManager.getConnection(dbUrl+dbName,dbUsername,dbPassword);
return con;
}
}
package com.latihan.web;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class Latihan5_deleteData
*/
@WebServlet("/latihan5-deletedata")
public class Latihan5_deleteData extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public Latihan5_deleteData() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
Connection con = Latihan5_conncectDatabase.initializeDatabase();
PreparedStatement st = con.prepareStatement("delete from bank where id_bank = ?");
st.setString(1, request.getParameter("id_bank"));
st.execute();
st.close();
con.close();
response.sendRedirect(request.getContextPath()+"/latihan5-tampildata");
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
package com.latihan.web;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class Latihan5_editData
*/
@WebServlet("/latihan5-editdata")
public class Latihan5_editData extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public Latihan5_editData() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
Connection con = Latihan5_conncectDatabase.initializeDatabase();
Statement stmt = (Statement) con.createStatement();
ResultSet rs = stmt.executeQuery("select * from bank where id_bank = "+Integer.valueOf(request.getParameter("id_bank")));
rs.next();
request.setAttribute("id_bank", rs.getInt("id_bank"));
request.setAttribute("kode", rs.getString("kode"));
request.setAttribute("nama", rs.getString("nama"));
request.getRequestDispatcher("Latihan5_editData.jsp").forward(request, response);
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
Connection con = Latihan5_conncectDatabase.initializeDatabase();
PreparedStatement st = con.prepareStatement("update bank set kode = ?, nama = ? where id_bank = ?");
st.setString(1, request.getParameter("kode"));
st.setString(2, request.getParameter("nama"));
st.setString(3, request.getParameter("id_bank"));
st.executeUpdate();
st.close();
con.close();
response.sendRedirect(request.getContextPath()+"/latihan5-tampildata");
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}
}
}
package com.latihan.web;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class Latihan5_tambahData
*/
@WebServlet("/latihan5-tambahdata")
public class Latihan5_tambahData extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public Latihan5_tambahData() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
Connection con = Latihan5_conncectDatabase.initializeDatabase();
PreparedStatement st = con.prepareStatement("insert into bank(kode,nama) values(?,?)");
st.setString(1, request.getParameter("kode"));
st.setString(2, request.getParameter("nama"));
st.executeUpdate();
st.close();
con.close();
response.sendRedirect(request.getContextPath()+"/latihan5-tampildata");
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}
}
}
package com.latihan.web;
import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class Latihan5_tampilData
*/
@WebServlet("/latihan5-tampildata")
public class Latihan5_tampilData extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public Latihan5_tampilData() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
Connection con = Latihan5_conncectDatabase.initializeDatabase();
Statement stmt = (Statement) con.createStatement();
ResultSet rs = stmt.executeQuery("select * from bank");
List<Latihan5_Data> bankList = new ArrayList<Latihan5_Data>();
while(rs.next()) {
Latihan5_Data bank = new Latihan5_Data();
bank.setId_bank(rs.getInt("id_bank"));
bank.setKode(rs.getString("kode"));
bank.setNama(rs.getString("nama"));
bankList.add(bank);
}
request.setAttribute("bankList", bankList);
request.getRequestDispatcher("Latihan5_tampilData.jsp").forward(request, response);
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
...@@ -16,7 +16,7 @@ ...@@ -16,7 +16,7 @@
color: white; color: white;
} }
</style> </style>
<title>Insert title here</title> <title>Login</title>
</head> </head>
<body> <body>
<form action="latihan4-login" method="post"> <form action="latihan4-login" method="post">
......
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<style type="text/css">
.button{
padding: 10px 27px;
text-align: center;
font-size: 10px;
cursor: pointer;
}
.yellow {
background-color: #e9f018;
color: black;
}
</style>
<title>Edit Data</title>
</head>
<body>
<form action="latihan5-editdata" method="post">
<input name="id_bank" type="hidden" value="${id_bank}">
<table style="border: 1px solid; margin: auto;">
<tr>
<th colspan="2" style="padding: 10px">Edit Data</th>
</tr>
<tr>
<td><label>Kode</label></td>
<td>: <input name = "kode" type = "text" value="${kode}"></td>
</tr>
<tr>
<td><label>Nama</label></td>
<td>: <input name = "nama" type = "text" value = "${nama}"></td>
</tr>
<tr>
<th colspan="2" style="padding: 10px"><button class="button yellow" type = "submit">Edit</button></th>
</tr>
</table>
</form>
</body>
</html>
\ No newline at end of file
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<style type="text/css">
.button{
padding: 10px 27px;
text-align: center;
font-size: 10px;
cursor: pointer;
}
.blue {
background-color: #188bf0;
color: white;
}
</style>
<title>Tambah Data</title>
</head>
<body>
<form action="latihan5-tambahdata" method="post">
<table style="border: 1px solid; margin: auto;">
<tr>
<th colspan="2" style="padding: 10px">Tambah Data</th>
</tr>
<tr>
<td><label>Kode</label></td>
<td>: <input name = "kode" type = "text"></td>
</tr>
<tr>
<td><label>Nama</label></td>
<td>: <input name = "nama" type = "text"></td>
</tr>
<tr>
<th colspan="2" style="padding: 10px"><button class="button blue" type = "submit">Tambah</button></th>
</tr>
</table>
</form>
</body>
</html>
\ No newline at end of file
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ page import = "java.util.ArrayList"%>
<%@ page import = "java.util.List"%>
<%@ page import = "com.latihan.web.Latihan5_Data" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<style>
table, td, th {
border: 1px solid;
padding: 5px;
}
</style>
<title>Data Bank</title>
</head>
<body>
<table style="margin: auto; border-collapse: collapse;">
<tr>
<th colspan="5" style="padding: 10px">Data Bank | <a href="Latihan5_tambahData.jsp">Tambah Data</a></th>
</tr>
<tr>
<th>ID BANK</th>
<th>KODE</th>
<th>NAMA</th>
<th>ACTION</th>
</tr>
<% List<Latihan5_Data> bankList = (ArrayList<Latihan5_Data>)request.getAttribute("bankList");
for (Latihan5_Data bank : bankList){ %>
<tr>
<td><%= bank.getId_bank() %></td>
<td><%= bank.getKode() %></td>
<td><%= bank.getNama() %></td>
<td><a href="http://localhost:8080/LatihanJavaServlet_dioharvandy/latihan5-editdata?id_bank=<%= bank.getId_bank() %>">Edit</a> |
<a href="http://localhost:8080/LatihanJavaServlet_dioharvandy/latihan5-deletedata?id_bank=<%= bank.getId_bank() %>" onclick="return confirm('Delete This Item?');">Delete</a></td>
</tr>
<%}%>
</table>
</body>
</html>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment