Commit 0c56c077 by Rohani Napitupulu

Ini perpustakaan milik iss14022

parents
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/jdk1.8.0_74">
<attributes>
<attribute name="owner.project.facets" value="java"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jst.server.core.container/org.eclipse.jst.server.tomcat.runtimeTarget/Apache Tomcat v9.0(2)">
<attributes>
<attribute name="owner.project.facets" value="jst.web"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jst.j2ee.internal.web.container"/>
<classpathentry kind="con" path="org.eclipse.jst.j2ee.internal.module.container"/>
<classpathentry kind="output" path="build/classes"/>
</classpath>
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>PerpusJSP</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.wst.common.project.facet.core.builder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.wst.validation.validationbuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jem.workbench.JavaEMFNature</nature>
<nature>org.eclipse.wst.common.modulecore.ModuleCoreNature</nature>
<nature>org.eclipse.wst.common.project.facet.core.nature</nature>
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.eclipse.wst.jsdt.core.jsNature</nature>
</natures>
</projectDescription>
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry excluding="**/bower_components/*|**/node_modules/*|**/*.min.js" kind="src" path="WebContent"/>
<classpathentry kind="con" path="org.eclipse.wst.jsdt.launching.JRE_CONTAINER"/>
<classpathentry kind="con" path="org.eclipse.wst.jsdt.launching.WebProject">
<attributes>
<attribute name="hide" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.wst.jsdt.launching.baseBrowserLibrary"/>
<classpathentry kind="output" path=""/>
</classpath>
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
org.eclipse.jdt.core.compiler.compliance=1.8
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.source=1.8
<?xml version="1.0" encoding="UTF-8"?><project-modules id="moduleCoreId" project-version="1.5.0">
<wb-module deploy-name="PerpusJSP">
<wb-resource deploy-path="/" source-path="/WebContent" tag="defaultRootSource"/>
<wb-resource deploy-path="/WEB-INF/classes" source-path="/src"/>
<property name="context-root" value="PerpusJSP"/>
<property name="java-output-path" value="/PerpusJSP/build/classes"/>
</wb-module>
</project-modules>
<?xml version="1.0" encoding="UTF-8"?>
<faceted-project>
<runtime name="Apache Tomcat v9.0(2)"/>
<fixed facet="java"/>
<fixed facet="wst.jsdt.web"/>
<fixed facet="jst.web"/>
<installed facet="java" version="1.8"/>
<installed facet="jst.web" version="3.1"/>
<installed facet="wst.jsdt.web" version="1.0"/>
</faceted-project>
org.eclipse.wst.jsdt.launching.baseBrowserLibrary
\ No newline at end of file
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://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>CRUD Project</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>BukuController</servlet-name>
<servlet-class>org.perpus.controller.BukuController</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>BukuController</servlet-name>
<url-pattern>/BukuController.do</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"%>
<%@ 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 Student</title>
</head>
<body>
<form action="BukuController.do" method="post">
<fieldset>
<input type="hidden"
name="isbn" value="<c:out value="${buku.isbn}" />"
placeholder="isbn" />
<div>
<label for="judul">Judul</label> <input type="text"
name="judul" value="<c:out value="${buku.judul}" />"
placeholder="Judul" />
</div>
<div>
<label for="pengarang">Pengarang</label> <input type="text"
name="pengarang" value="<c:out value="${buku.pengarang}" />"
placeholder="Pengarang" />
</div>
<div>
<label for="penerbit">Penerbit</label> <input type="text"
name="penerbit" value="<c:out value="${buku.penerbit}" />"
placeholder="Penerbit" />
</div>
<div>
<label for="tahun">Tahun</label> <input type="text"
name="tahun" value="<c:out value="${buku.tahun}" />" placeholder="Tahun" />
</div>
<div>
<label for="harga">Harga</label> <input type="text" name="harga"
value="<c:out value="${buku.harga}" />" placeholder="Harga" />
</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="/BukuController?action=listBuku"></jsp:forward>
</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 Books</title>
</head>
<body>
<table>
<thead>
<tr>
<th>ISBN</th>
<th>Judul</th>
<th>Pengarang</th>
<th>Penerbit</th>
<th>Tahun</th>
<th>Harga</th>
<th colspan="2">Action</th>
</tr>
</thead>
<tbody>
<c:forEach items="${bukus}" var="buku">
<tr>
<td><c:out value="${buku.isbn}" /></td>
<td><c:out value="${buku.judul}" /></td>
<td><c:out value="${buku.pengarang}" /></td>
<td><c:out value="${buku.penerbit}" /></td>
<td><c:out value="${buku.tahun}" /></td>
<td><c:out value="${buku.harga}" /></td>
<td><a
href="BukuController.do?action=edit&isbn=<c:out value="${buku.isbn }"/>">Update</a></td>
<td><a
href="BukuController.do?action=delete&isbn=<c:out value="${buku.isbn }"/>">Delete</a></td>
</tr>
</c:forEach>
</tbody>
</table>
<p>
<a href="BukuController.do?action=insert">Add Buku</a>
</p>
</body>
</html>
\ No newline at end of file
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/praktikum
user=root
password=
\ No newline at end of file
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/praktikum
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.model.Buku;
import org.perpus.dao.BukuDAO;
import org.perpus.dao.BukuDAOImplementation;
/**
* Servlet implementation class BukuController
*/
@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";
/**
* @see HttpServlet#HttpServlet()
*/
public BukuController() {
dao = new BukuDAOImplementation();
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
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 isbn = Integer.parseInt( request.getParameter("isbn") );
dao.deleteBuku(isbn);
request.setAttribute("bukus", dao.getAllBukus() );
}
else if( action.equalsIgnoreCase( "edit" ) ) {
forward = INSERT_OR_EDIT;
int isbn = Integer.parseInt( request.getParameter("isbn") );
Buku buku = dao.getBukuById(isbn);
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);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Buku buku = new Buku();
//buku.setIsbn(0);
buku.setJudul(request.getParameter("judul"));
buku.setPengarang(request.getParameter("pengarang"));
buku.setPenerbit(request.getParameter("penerbit"));
buku.setTahun(Integer.parseInt(request.getParameter("tahun")));
buku.setHarga(Integer.parseInt(request.getParameter("harga")));
String isbn = request.getParameter("isbn");
if( isbn == null || isbn.isEmpty() )
dao.addBuku(buku);
else {
buku.setIsbn(Integer.parseInt(isbn));
dao.updateBuku(buku);
}
RequestDispatcher view = request.getRequestDispatcher( lIST_Buku );
request.setAttribute("bukus", dao.getAllBukus());
view.forward(request, response);
}
}
package org.perpus.dao;
import java.util.List;
import org.perpus.model.Buku;
public interface BukuDAO {
public void addBuku( Buku buku );
public void deleteBuku( int isbn );
public void updateBuku( Buku buku );
public List<Buku> getAllBukus();
public Buku getBukuById( int isbn );
}
\ No newline at end of file
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.Buku;
import org.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 (isbn, judul, pengarang, penerbit,tahun,harga) values (?,?,?,?,?,?)";
PreparedStatement preparedStatement = conn.prepareStatement( query );
preparedStatement.setInt( 1, buku.getIsbn() );
preparedStatement.setString( 2, buku.getJudul() );
preparedStatement.setString( 3, buku.getPengarang() );
preparedStatement.setString( 4, buku.getPenerbit() );
preparedStatement.setInt( 5, buku.getTahun() );
preparedStatement.setInt( 6, buku.getHarga() );
preparedStatement.executeUpdate();
preparedStatement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
@Override
public void deleteBuku(int isbn) {
try {
String query = "delete from buku where isbn=?";
PreparedStatement preparedStatement = conn.prepareStatement(query);
preparedStatement.setInt(1, isbn);
preparedStatement.executeUpdate();
preparedStatement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
@Override
public void updateBuku( Buku buku ) {
try {
String query = "update buku set judul=?, pengarang=?, penerbit=?, tahun=?, harga=? where isbn=?";
PreparedStatement preparedStatement = conn.prepareStatement( query );
preparedStatement.setString( 1, buku.getJudul() );
preparedStatement.setString( 2, buku.getPengarang() );
preparedStatement.setString( 3, buku.getPenerbit() );
preparedStatement.setInt( 4, buku.getTahun() );
preparedStatement.setInt(5, buku.getHarga());
preparedStatement.setInt(6, buku.getIsbn());
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.setIsbn(resultSet.getInt("isbn"));
buku.setJudul(resultSet.getString("judul"));
buku.setPengarang(resultSet.getString("pengarang"));
buku.setPenerbit(resultSet.getString("penerbit"));
buku.setTahun(resultSet.getInt("tahun"));
buku.setHarga(resultSet.getInt("harga"));
bukus.add(buku);
}
resultSet.close();
statement.close();
} catch (SQLException e) {
e.printStackTrace();
}
return bukus;
}
@Override
public Buku getBukuById(int isbn) {
Buku buku = new Buku();
try {
String query = "select * from buku where isbn=?";
PreparedStatement preparedStatement = conn.prepareStatement( query );
preparedStatement.setInt(1, isbn);
ResultSet resultSet = preparedStatement.executeQuery();
while( resultSet.next() ) {
buku.setIsbn(resultSet.getInt("isbn"));
buku.setJudul(resultSet.getString("judul"));
buku.setPengarang(resultSet.getString("pengarang"));
buku.setPenerbit(resultSet.getString("penerbit"));
buku.setTahun(resultSet.getInt("tahun"));
buku.setHarga(resultSet.getInt("harga"));
}
resultSet.close();
preparedStatement.close();
} catch (SQLException e) {
e.printStackTrace();
}
return buku;
}
}
\ No newline at end of file
package org.perpus.model;
public class Buku
{
private int isbn;
private String judul;
private String pengarang;
private String penerbit;
private int tahun;
private int harga;
public Buku() {}
public Buku(int isbn, String judul, String pengarang, String penerbit, int tahun, int harga)
{
this.isbn = isbn;
this.judul = judul;
this.pengarang = pengarang;
this.penerbit = penerbit;
this.tahun = tahun;
this.harga = harga;
}
public int getIsbn() {
return isbn;
}
public void setIsbn(int isbn) {
this.isbn = isbn;
}
public String getJudul() {
return judul;
}
public void setJudul(String judul) {
this.judul = judul;
}
public String getPengarang() {
return pengarang;
}
public void setPengarang(String pengarang) {
this.pengarang = pengarang;
}
public String getPenerbit() {
return penerbit;
}
public void setPenerbit(String penerbit) {
this.penerbit = penerbit;
}
public int getTahun() {
return tahun;
}
public void setTahun(int tahun) {
this.tahun = tahun;
}
public int getHarga() {
return harga;
}
public void setHarga(int harga) {
this.harga = harga;
}
}
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();
}
}
}
\ No newline at end of file
-- phpMyAdmin SQL Dump
-- version 4.5.1
-- http://www.phpmyadmin.net
--
-- Host: 127.0.0.1
-- Generation Time: Nov 30, 2016 at 06:01 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: `praktikum`
--
-- --------------------------------------------------------
--
-- Table structure for table `buku`
--
CREATE TABLE `buku` (
`isbn` int(11) NOT NULL,
`judul` varchar(120) NOT NULL,
`pengarang` varchar(128) NOT NULL,
`penerbit` varchar(128) NOT NULL,
`tahun` int(11) NOT NULL,
`harga` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
--
-- Dumping data for table `buku`
--
INSERT INTO `buku` (`isbn`, `judul`, `pengarang`, `penerbit`, `tahun`, `harga`) VALUES
(1, 'Laskar Pelangi', 'Anuar', 'Erlangga', 2010, 150000),
(2, 'Sang Pemimpi', 'Cico', 'Sindo', 2014, 38000);
-- --------------------------------------------------------
--
-- Table structure for table `mahasiswa`
--
CREATE TABLE `mahasiswa` (
`nim` varchar(10) NOT NULL,
`nama` varchar(20) NOT NULL,
`kelas` int(11) NOT NULL,
`ipk` float NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
--
-- Dumping data for table `mahasiswa`
--
INSERT INTO `mahasiswa` (`nim`, `nama`, `kelas`, `ipk`) VALUES
('31S14022', 'Rona', 3, 4),
('20139', 'Sifa', 1, 3.1);
--
-- Indexes for dumped tables
--
--
-- Indexes for table `buku`
--
ALTER TABLE `buku`
ADD PRIMARY KEY (`isbn`);
--
-- AUTO_INCREMENT for dumped tables
--
--
-- AUTO_INCREMENT for table `buku`
--
ALTER TABLE `buku`
MODIFY `isbn` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=3;
/*!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 */;
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