Commit d3c7eddf authored by Fabian Dewantara Santonie's avatar Fabian Dewantara Santonie 🕊

Menambahkan menu Edit

parent 72c84e4f
package org.testing.bank;
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 edit
*/
@WebServlet("/editData")
public class edit extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public edit() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
try {
Connection con = DatabaseConnection.initializeDatabase();
Statement stmt = (Statement) con.createStatement();
ResultSet rs = stmt.executeQuery("select * from bank where id = "+Integer.valueOf(request.getParameter("id")));
rs.next();
request.setAttribute("id", rs.getInt("id"));
request.setAttribute("kode", rs.getString("kode"));
request.setAttribute("nama", rs.getString("nama"));
request.getRequestDispatcher("edit.jsp").forward(request, response);
}
catch(Exception e) {
e.printStackTrace();
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
Connection con = DatabaseConnection.initializeDatabase();
PreparedStatement st = con.prepareStatement("update bank set kode = ?, nama = ? where id = ?");
st.setString(1, request.getParameter("kode"));
st.setString(2, request.getParameter("nama"));
st.setString(3, request.getParameter("id"));
st.executeUpdate();
st.close();
con.close();
response.sendRedirect(request.getContextPath()+"/editData");
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}
}
}
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<div align="center">
<table class="table-sm" border="2">
<tr>
<th>ID</th>
<th>Kode</th>
<th>Nama Bank</th>
</tr>
<tr>
<th>${id}</th>
<th>${kode}</th>
<th>${nama}</th>
</tr>
</div>
</form>
</body>
</html>
\ No newline at end of file
......@@ -7,6 +7,7 @@
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<div align="center">
<body>
<a href="Latihan1.jsp"><button class="btn btn-dark">Latihan 1</button></a>
<a href="Latihan2.jsp"><button class="btn btn-dark">Latihan 2</button></a>
......@@ -14,4 +15,5 @@
<a href="Latihan4.jsp"><button class="btn btn-dark">Latihan 4</button></a>
<a href="/belajarJavaServlet_Fabian/DatabaseConnect"><button class="btn btn-dark">Latihan 5</button></a>
</body>
</div>
</html>
\ No newline at end of file
......@@ -26,6 +26,7 @@
<td><%=bank.getId()%></td>
<td><%=bank.getKode()%></td>
<td><%=bank.getNama()%></td>
<td><a href="editData?id=<%= bank.getId()%>"><button class="btn btn-dark">Edit</button></a></td>
</tr>
<%}%>
</table>
......
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