Commit d0a5f13e by Yan Rega

tugas servlet perpustakaan

parents
Manifest-Version: 1.0
Class-Path:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>Perpustakaan</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
\ No newline at end of file
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Add New Buku</title>
</head>
<body>
<form action="BukuController" method="post">
<fieldset>
<div>
<label for="idBuku">ID Buku</label> <input type="text"
name="idBuku" value="<c:out value="${buku.idBuku}" />"
readonly="readonly" placeholder="ID Buku" />
</div>
<div>
<label for="judulBuku">Judul Buku</label> <input type="text"
name="judulBuku" value="<c:out value="${buku.judulBuku}" />"
placeholder="Judul Buku" />
</div>
<div>
<label for="pengarangBuku">Pengarang Buku</label> <input type="text"
name="pengarangBuku" value="<c:out value="${buku.pengarangBuku}" />"
placeholder="Pengarang Buku" />
</div>
<div>
<label for="isbn">ISBN</label> <input type="text" name="isbn"
value="<c:out value="${buku.isbn}" />" placeholder="ISBN" />
</div>
<div>
<label for="jumlah">Jumlah Halaman</label> <input type="text" name="jumlah"
value="<c:out value="${buku.jumlah}" />" placeholder="Jumlah" />
</div>
<div>
<input type="submit" value="Submit" />
</div>
</fieldset>
</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 PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Perpustakaan</title>
</head>
<body>
<jsp:forward page="/BukuController?action=listBuku" />
</body>
</html>
\ No newline at end of file
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Show All Buku</title>
</head>
<body>
<table border=1>
<thead>
<tr>
<th>ID Buku</th>
<th>Judul Buku</th>
<th>Pengarang Buku</th>
<th>ISBN</th>
<th>Jumlah</th>
<th colspan="2">Action</th>
</tr>
</thead>
<tbody>
<c:forEach items="${bukus}" var="buku">
<tr>
<td><c:out value="${buku.idBuku}" /></td>
<td><c:out value="${buku.judulBuku}" /></td>
<td><c:out value="${buku.pengarangBuku}" /></td>
<td><c:out value="${buku.isbn}" /></td>
<td><c:out value="${buku.jumlah}" /></td>
<td><a
href="BukuController?action=edit&idBuku=<c:out value="${buku.idBuku}"/>">Edit</a></td>
<td><a
href="BukuController?action=delete&idBuku=<c:out value="${buku.idBuku}"/>">Delete</a></td>
</tr>
</c:forEach>
</tbody>
</table>
<p>
<a href="BukuController?action=insert">Add Buku</a>
</p>
</body>
</html>
\ No newline at end of file
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/perpustakaan
user=root
password=
\ No newline at end of file
-- phpMyAdmin SQL Dump
-- version 4.5.1
-- http://www.phpmyadmin.net
--
-- Host: 127.0.0.1
-- Generation Time: Dec 02, 2016 at 08:28 AM
-- Server version: 10.1.16-MariaDB
-- PHP Version: 7.0.9
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;
--
-- Database: `perpustakaan`
--
-- --------------------------------------------------------
--
-- Table structure for table `buku`
--
CREATE TABLE `buku` (
`idBuku` int(11) NOT NULL,
`judulBuku` varchar(255) DEFAULT NULL,
`pengarangBuku` varchar(255) DEFAULT NULL,
`isbn` varchar(255) DEFAULT NULL,
`jumlah` int(100) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
--
-- Dumping data for table `buku`
--
INSERT INTO `buku` (`idBuku`, `judulBuku`, `pengarangBuku`, `isbn`, `jumlah`) VALUES
(1, 'Twilight', 'Meyer', '1996', 1025);
--
-- Indexes for dumped tables
--
--
-- Indexes for table `buku`
--
ALTER TABLE `buku`
ADD PRIMARY KEY (`idBuku`);
--
-- AUTO_INCREMENT for dumped tables
--
--
-- AUTO_INCREMENT for table `buku`
--
ALTER TABLE `buku`
MODIFY `idBuku` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=2;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
package com.perpus.controller;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
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 com.perpus.dao.BukuDAO;
import com.perpus.dao.BukuDAOImplementation;
import com.perpus.model.Buku;
@WebServlet("/BukuController")
public class BukuController extends HttpServlet
{
private BukuDAO dao;
private static final long serialVersionUID = 1L;
public static final String LIST_BUKU = "/listBuku.jsp";
public static final String INSERT_OR_EDIT = "/buku.jsp";
public BukuController() {
dao = new BukuDAOImplementation();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
String forward = "";
String action = request.getParameter( "action" );
if( action.equalsIgnoreCase( "delete" ) ) {
forward = LIST_BUKU;
int idBuku = Integer.parseInt( request.getParameter("idBuku") );
dao.deleteBuku(idBuku);
request.setAttribute("bukus", dao.getAllBukus() );
}
else if( action.equalsIgnoreCase( "edit" ) ) {
forward = INSERT_OR_EDIT;
int idBuku = Integer.parseInt( request.getParameter("idBuku") );
Buku buku = dao.getBukuById(idBuku);
request.setAttribute("buku", buku);
}
else if( action.equalsIgnoreCase("insert") ) {
forward = INSERT_OR_EDIT;
}
else {
forward = LIST_BUKU;
request.setAttribute("bukus", dao.getAllBukus());
}
RequestDispatcher view = request.getRequestDispatcher( forward );
view.forward(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
Buku buku = new Buku();
buku.setJudulBuku(request.getParameter("judulBuku"));
buku.setPengarangBuku(request.getParameter("pengarangBuku"));
buku.setIsbn(request.getParameter("isbn"));
buku.setJumlah(Integer.parseInt(request.getParameter("jumlah")));
String idBuku = request.getParameter("idBuku");
if(idBuku == null || idBuku.isEmpty() )
{
dao.addBuku(buku);
}
else
{
buku.setIdBuku(Integer.parseInt(idBuku));
dao.updateBuku(buku);
}
RequestDispatcher view = request.getRequestDispatcher(LIST_BUKU);
request.setAttribute("students", dao.getAllBukus());
response.sendRedirect("index.jsp");
}
}
package com.perpus.dao;
import java.util.List;
import com.perpus.model.Buku;
public interface BukuDAO
{
public void addBuku(Buku buku);
public void deleteBuku(int idBuku);
public void updateBuku(Buku buku);
public List<Buku> getAllBukus();
public Buku getBukuById(int idBuku);
}
package com.perpus.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import com.perpus.model.Buku;
import com.perpus.util.DBUtil;
public class BukuDAOImplementation implements BukuDAO
{
private Connection conn;
public BukuDAOImplementation()
{
conn = DBUtil.getConnection();
}
@Override
public void addBuku(Buku buku)
{
try
{
String query = "insert into buku (judulBuku, pengarangBuku, isbn, jumlah) values (?,?,?,?)";
PreparedStatement preparedStatement = conn.prepareStatement(query);
preparedStatement.setString(1, buku.getJudulBuku());
preparedStatement.setString(2, buku.getPengarangBuku());
preparedStatement.setString(3, buku.getIsbn());
preparedStatement.setInt(4, buku.getJumlah());
preparedStatement.executeUpdate();
preparedStatement.close();
} catch (SQLException e)
{
e.printStackTrace();
}
}
@Override
public void deleteBuku(int idBuku)
{
try
{
String query = "delete from buku where idBuku=?";
PreparedStatement preparedStatement = conn.prepareStatement(query);
preparedStatement.setInt(1, idBuku);
preparedStatement.executeUpdate();
preparedStatement.close();
} catch (SQLException e)
{
e.printStackTrace();
}
}
@Override
public void updateBuku(Buku buku)
{
try
{
String query = "update buku set judulBuku=?, pengarangBuku=?, isbn=?, jumlah=? where idBuku=?";
PreparedStatement preparedStatement = conn.prepareStatement(query);
preparedStatement.setString(1, buku.getJudulBuku());
preparedStatement.setString(2, buku.getPengarangBuku());
preparedStatement.setString(3, buku.getIsbn());
preparedStatement.setInt(4, buku.getJumlah());
preparedStatement.setInt(5, buku.getIdBuku());
preparedStatement.executeUpdate();
preparedStatement.close();
} catch (SQLException e)
{
e.printStackTrace();
}
}
@Override
public List<Buku> getAllBukus()
{
List<Buku> bukus = new ArrayList<Buku>();
try
{
Statement statement = conn.createStatement();
ResultSet resultSet = statement.executeQuery("select * from buku");
while( resultSet.next() )
{
Buku buku = new Buku();
buku.setIdBuku( resultSet.getInt( "idBuku" ) );
buku.setJudulBuku( resultSet.getString( "judulBuku" ) );
buku.setPengarangBuku( resultSet.getString( "pengarangBuku" ) );
buku.setIsbn( resultSet.getString( "isbn" ) );
buku.setJumlah( resultSet.getInt( "jumlah" ) );
bukus.add(buku);
}
resultSet.close();
statement.close();
} catch (SQLException e)
{
e.printStackTrace();
}
return bukus;
}
@Override
public Buku getBukuById(int idBuku)
{
Buku buku = new Buku();
try
{
String query = "select * from buku where idBuku=?";
PreparedStatement preparedStatement = conn.prepareStatement(query);
preparedStatement.setInt(1, idBuku);
ResultSet resultSet = preparedStatement.executeQuery();
while(resultSet.next())
{
buku.setIdBuku(resultSet.getInt("idBuku"));
buku.setJudulBuku(resultSet.getString("judulBuku"));
buku.setPengarangBuku(resultSet.getString("pengarangBuku"));
buku.setIsbn(resultSet.getString("isbn"));
buku.setJumlah( resultSet.getInt("jumlah"));
}
resultSet.close();
preparedStatement.close();
} catch (SQLException e)
{
e.printStackTrace();
}
return buku;
}
}
package com.perpus.model;
public class Buku
{
private int idBuku;
private String judulBuku;
private String pengarangBuku;
private String isbn;
private int jumlah;
public int getIdBuku() {
return idBuku;
}
public void setIdBuku(int idBuku) {
this.idBuku = idBuku;
}
public String getJudulBuku() {
return judulBuku;
}
public void setJudulBuku(String judulBuku) {
this.judulBuku = judulBuku;
}
public String getPengarangBuku() {
return pengarangBuku;
}
public void setPengarangBuku(String pengarangBuku) {
this.pengarangBuku = pengarangBuku;
}
public String getIsbn() {
return isbn;
}
public void setIsbn(String isbn) {
this.isbn = isbn;
}
public int getJumlah() {
return jumlah;
}
public void setJumlah(int jumlah) {
this.jumlah = jumlah;
}
@Override
public String toString() {
return "Buku [idBuku=" + idBuku + ", judulBuku=" + judulBuku + ", pengarangBuku=" + pengarangBuku + ", isbn="
+ isbn + ", jumlah=" + jumlah + "]";
}
}
package com.perpus.util;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;
public class DBUtil {
private static Connection conn;
public static Connection getConnection() {
if( conn != null )
return conn;
InputStream inputStream = DBUtil.class.getClassLoader().getResourceAsStream( "/db.properties" );
Properties properties = new Properties();
try {
properties.load( inputStream );
String driver = properties.getProperty( "driver" );
String url = properties.getProperty( "url" );
String user = properties.getProperty( "user" );
String password = properties.getProperty( "password" );
Class.forName( driver );
conn = DriverManager.getConnection( url, user, password );
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
public static void closeConnection( Connection toBeClosed ) {
if( toBeClosed == null )
return;
try {
toBeClosed.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
\ No newline at end of file
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/perpustakaan
user=root
password=
\ 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