Commit b07e6f0e by hottua gultom

Submit Servlet_JSP

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/JavaSE-1.7">
<attributes>
<attribute name="owner.project.facets" value="java"/>
</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>PerpustakaanJSP</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.7
org.eclipse.jdt.core.compiler.compliance=1.7
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.source=1.7
<?xml version="1.0" encoding="UTF-8"?><project-modules id="moduleCoreId" project-version="1.5.0">
<wb-module deploy-name="PerpustakaanJSP">
<wb-resource deploy-path="/" source-path="/WebContent" tag="defaultRootSource"/>
<wb-resource deploy-path="/WEB-INF/classes" source-path="/src"/>
<property name="context-root" value="PerpustakaanJSP"/>
<property name="java-output-path" value="/PerpustakaanJSP/build/classes"/>
</wb-module>
</project-modules>
<?xml version="1.0" encoding="UTF-8"?>
<faceted-project>
<fixed facet="wst.jsdt.web"/>
<installed facet="java" version="1.7"/>
<installed facet="jst.web" version="2.5"/>
<installed facet="wst.jsdt.web" version="1.0"/>
</faceted-project>
org.eclipse.wst.jsdt.launching.baseBrowserLibrary
\ No newline at end of file
<?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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 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>PerpustakaanJSP</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<description></description>
<display-name>BukuController</display-name>
<servlet-name>BukuController</servlet-name>
<servlet-class>com.perpus.controller.BukuController</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>BukuController</servlet-name>
<url-pattern>/BukuController</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 Buku</title>
</head>
<body>
<form action="BukuController.do" 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>
<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.do?action=edit&idBuku=<c:out value="${buku.idBuku}"/>">Update</a></td>
<td><a
href="BukuController.do?action=delete&idBuku=<c:out value="${buku.idBuku}"/>">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/perpustakaan
user=root
password=""
\ No newline at end of file
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 = "/listBku.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());
view.forward(request, response);
}
}
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 intu 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 isBuku=?";
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