Tugas ServletJSP 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>PerpusITDel</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<description></description>
<display-name>BookController</display-name>
<servlet-name>BookController</servlet-name>
<servlet-class>org.perpus.controller.BookController</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>BookController</servlet-name>
<url-pattern>/BookController</url-pattern>
</servlet-mapping>
</web-app>
\ 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>Add New Book</title>
</head>
<body>
<form action="BookController.do" method="post">
<fieldset>
<div>
<label for="bookId">Book ID</label> <input type="text"
name="bookId" value="<c:out value="${book.bookId}" />"
readonly="readonly" placeholder="Book ID" />
</div>
<div>
<label for="pengarang">Pengarang</label> <input type="text"
name="pengarang" value="<c:out value="${book.pengarang}" />"
placeholder="Pengarang" />
</div>
<div>
<label for="judul">Judul</label> <input type="text"
name="judul" value="<c:out value="${book.judul}" />"
placeholder="Judul" />
</div>
<div>
<label for="jenis">Jenis</label> <input type="text" name="jenis"
value="<c:out value="${book.jenis}" />" placeholder="Jenis" />
</div>
<div>
<label for="isbn">Isbn</label> <input type="text" name="isbn"
value="<c:out value="${book.isbn}" />" placeholder="Isbn" />
</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>Welcome</title>
</head>
<body>
<jsp:forward page="/BookController?action=listBook"></jsp:forward>
</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>Show All Books</title>
</head>
<body>
<table>
<thead>
<tr>
<th>Book ID</th>
<th>Pengarang</th>
<th>Judul</th>
<th>Jenis</th>
<th>Isbn</th>
<th colspan="2">Action</th>
</tr>
</thead>
<tbody>
<c:forEach items="${books}" var="book">
<tr>
<td><c:out value="${book.bookId}" /></td>
<td><c:out value="${book.pengarang}" /></td>
<td><c:out value="${book.judul}" /></td>
<td><c:out value="${book.jenis}" /></td>
<td><c:out value="${book.isbn}" /></td>
<td><a
href="BookController.do?action=edit&booktId=<c:out value="${book.bookId }"/>">Update</a></td>
<td><a
href="BookController.do?action=delete&bookId=<c:out value="${book.bookId }"/>">Delete</a></td>
</tr>
</c:forEach>
</tbody>
</table>
<p>
<a href="BookController.do?action=insert">Add Book</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
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/perpustakaan
user=root
password=""
\ No newline at end of file
package org.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 org.perpus.dao.BookDAO;
import org.perpus.dao.BookDAOImplementation;
import org.perpus.model.Book;
@WebServlet("/BookController")
public class BookController extends HttpServlet {
private BookDAO dao;
private static final long serialVersionUID = 1L;
public static final String lIST_BOOK = "/listBook.jsp";
public static final String INSERT_OR_EDIT = "/book.jsp";
public BookController() {
dao = new BookDAOImplementation();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String forward = "";
String action = request.getParameter( "action" );
if( action.equalsIgnoreCase( "delete" ) ) {
forward = lIST_BOOK;
int bookId = Integer.parseInt( request.getParameter("bookId") );
dao.deleteBook(bookId);
request.setAttribute("books", dao.getAllBooks() );
}
else if( action.equalsIgnoreCase( "edit" ) ) {
forward = INSERT_OR_EDIT;
int bookId = Integer.parseInt( request.getParameter("bookId") );
Book book = dao.getBookByIsbn(bookId);
request.setAttribute("book", book);
}
else if( action.equalsIgnoreCase( "insert" ) ) {
forward = INSERT_OR_EDIT;
}
else {
forward = lIST_BOOK;
request.setAttribute("books", dao.getAllBooks() );
}
RequestDispatcher view = request.getRequestDispatcher( forward );
view.forward(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Book book = new Book();
book.setPengarang( request.getParameter( "pengarang" ) );
book.setJudul( request.getParameter( "judul" ) );
book.setJenis( request.getParameter( "jenis" ) );
book.setIsbn( request.getParameter( "isbn" ) );
String bookId = request.getParameter("bookId");
if( bookId == null || bookId.isEmpty() )
dao.addBook(book);
else {
book.setBookId( Integer.parseInt(bookId) );
dao.updateBook(book);
}
RequestDispatcher view = request.getRequestDispatcher( lIST_BOOK);
request.setAttribute("books", dao.getAllBooks());
view.forward(request, response);
}
}
package org.perpus.dao;
import java.util.List;
import org.perpus.model.Book;
public interface BookDAO {
public void addBook( Book book );
public void deleteBook( int bookId );
public void updateBook( Book book );
public List<Book> getAllBooks();
public Book getBookByIsbn( int bookId );
}
package org.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 org.perpus.model.Book;
import org.perpus.util.DBUtil;
public class BookDAOImplementation implements BookDAO {
private Connection conn;
public BookDAOImplementation() {
conn = DBUtil.getConnection();
}
@Override
public void addBook( Book book ) {
try {
String query = "insert into book (pengarang,judul,jenis) values (?,?,?)";
PreparedStatement preparedStatement = conn.prepareStatement( query );
preparedStatement.setString( 1, book.getPengarang() );
preparedStatement.setString( 2, book.getJudul() );
preparedStatement.setString( 3, book.getJenis() );
preparedStatement.setString( 4, book.getIsbn() );
preparedStatement.executeUpdate();
preparedStatement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
@Override
public void deleteBook( int bookId ) {
try {
String query = "delete from book where bookId=?";
PreparedStatement preparedStatement = conn.prepareStatement(query);
preparedStatement.setInt(1, bookId);
preparedStatement.executeUpdate();
preparedStatement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
@Override
public void updateBook( Book book ) {
try {
String query = "update book set pengarang=?, judul=?, jenis=?, isbn=? where bookId=?";
PreparedStatement preparedStatement = conn.prepareStatement( query );
preparedStatement.setString( 1, book.getPengarang() );
preparedStatement.setString( 2, book.getJudul() );
preparedStatement.setString( 3, book.getJenis() );
preparedStatement.setString( 4, book.getIsbn() );
preparedStatement.setInt(5, book.getBookId());
preparedStatement.executeUpdate();
preparedStatement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
@Override
public List<Book> getAllBooks() {
List<Book> books = new ArrayList<Book>();
try {
Statement statement = conn.createStatement();
ResultSet resultSet = statement.executeQuery( "select * from book" );
while( resultSet.next() ) {
Book book = new Book();
book.setBookId( resultSet.getInt( "bookId" ) );
book.setPengarang( resultSet.getString( "pengarang" ) );
book.setJudul( resultSet.getString( "judul" ) );
book.setJenis( resultSet.getString( "jenis" ) );
book.setIsbn( resultSet.getString( "isbn" ) );
books.add(book);
}
resultSet.close();
statement.close();
} catch (SQLException e) {
e.printStackTrace();
}
return books;
}
@Override
public Book getBookByIsbn( int bookId ) {
Book book = new Book();
try {
String query = "select * from book where bookId=?";
PreparedStatement preparedStatement = conn.prepareStatement( query );
preparedStatement.setInt(1, bookId);
ResultSet resultSet = preparedStatement.executeQuery();
while( resultSet.next() ) {
book.setBookId( resultSet.getInt( "bookId" ) );
book.setPengarang( resultSet.getString( "pengarang" ) );
book.setJudul( resultSet.getString( "judul" ) );
book.setJenis( resultSet.getString( "jenis" ) );
book.setIsbn( resultSet.getString( "isbn" ) );
}
resultSet.close();
preparedStatement.close();
} catch (SQLException e) {
e.printStackTrace();
}
return book;
}
}
package org.perpus.model;
public class Book {
private int bookId;
private String pengarang;
private String judul;
private String jenis;
private String isbn;
public int getBookId() {
return bookId;
}
public void setBookId(int bookId) {
this.bookId = bookId;
}
public String getPengarang() {
return pengarang;
}
public void setPengarang(String pengarang) {
this.pengarang = pengarang;
}
public String getJudul() {
return judul;
}
public void setJudul(String judul) {
this.judul = judul;
}
public String getJenis() {
return jenis;
}
public void setJenis(String jenis) {
this.jenis = jenis;
}
public String getIsbn() {
return isbn;
}
public void setIsbn(String isbn) {
this.isbn = isbn;
}
@Override
public String toString() {
return "Book [bookId=" + bookId + ", pengarang=" + pengarang
+ ", judul=" + judul + ", jenis=" + jenis + ", isbn=" + isbn + "]";
}
}
package org.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();
}
}
}
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