Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pengumpulan Tugas Besar #7

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions addItem.jsp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<%@ page import="java.sql.*, javax.sql.*, java.io.*, javax.naming.*" %>
<%
Connection con = null;
try {
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
}catch(ClassNotFoundException ce){out.println(ce);}

con = DriverManager.getConnection("jdbc:mysql://localhost/ruserba","root","");
String query = "SELECT jumlah FROM cart WHERE username=? AND namabarang=?";
PreparedStatement st = con.prepareStatement(query);
st.setString(1, request.getParameter("username"));
st.setString(2, request.getParameter("namabarang"));

ResultSet rs = st.executeQuery();
if (rs.next()) {
query = "UPDATE cart SET jumlah=? WHERE username=? AND namabarang=?";
PreparedStatement st2 = con.prepareStatement(query);
st2.setInt(1, Integer.parseInt(request.getParameter("jumlahBarang")) + rs.getInt(1));
st2.setString(2, request.getParameter("username"));
st2.setString(3, request.getParameter("namabarang"));

st2.executeUpdate();
st2.clearParameters();
out.println("<script>document.location.href='shoppingbag.jsp'</script>");
} else {
query = "INSERT INTO cart(namabarang, jumlah, keterangan, username) VALUES(?, ?, '', ?)";
PreparedStatement st2 = con.prepareStatement(query);
st2.setString(1, request.getParameter("namabarang"));
st2.setInt(2, Integer.parseInt(request.getParameter("jumlahBarang")));
st2.setString(3, request.getParameter("username"));

st2.executeUpdate();
st2.clearParameters();
out.println("<script>document.location.href='shoppingbag.jsp'</script>");
}

st.clearParameters();
con.close();
} catch (Exception e) {out.println(e.toString()); };
%>
34 changes: 34 additions & 0 deletions ajaxLoader.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
var xmlhttp;
function httpRequest() {
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
}

function loadXMLDoc(url,cfunc)
{
httpRequest();
xmlhttp.onreadystatechange=cfunc;
xmlhttp.open("GET",url,true);
xmlhttp.send();
}

function postForm(url, cfunc, form) { // w/o button
httpRequest();
xmlhttp.onreadystatechange=cfunc;
xmlhttp.open("POST", url, true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
var formdata = "";
for (var i=0;i<form.length;i++) {
formdata += form.elements[i].name + "=" + form.elements[i].value;
if (i < form.length-1)
formdata+="&";
}

xmlhttp.send(formdata);
}
27 changes: 27 additions & 0 deletions bayar.jsp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<%@ page import="java.sql.*, javax.sql.*, java.io.*, javax.naming.*" %>
<%
Connection con = null;

try {
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
}catch(ClassNotFoundException ce){out.println(ce);}

con = DriverManager.getConnection("jdbc:mysql://localhost/ruserba","root","");

PreparedStatement st;

st = con.prepareStatement("SELECT namabarang FROM cart");

ResultSet rs;
rs = st.executeQuery();

if (rs.next())
response.getWriter().print(rs.getString(1));
else response.getWriter().print(0);

st.clearParameters();
con.close();
} catch (Exception e) {out.println("Unable to connect to database."); };

%>
42 changes: 42 additions & 0 deletions browse.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
.itemView{
display:block;
float:left;
width:390px;
height:180px;
margin:0;
}

.itemDetail{
display:block;
position:relative;
float:left;
top:0;
left:10px;
height:160px;
width:220px;
}

.itemImage{
display:block;
float:left;
position:relative;
width:160px;
height:160px;
background:#333333;
}

.browseOption{
display:block;
width:100%;
height: 30px;
float:left;
}

#quantity{
width:40px;
right:20px;
}

.pagination{
color: #47ca86;
}
132 changes: 132 additions & 0 deletions browse.jsp
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
<%@ page import="java.sql.*, javax.sql.*, java.io.*, javax.naming.*" %>
<%
Connection con = null;
String[] data = new String[8];
String kategori = request.getParameter("kategori");
String keyword = request.getParameter("keyword");

try {
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
}catch(ClassNotFoundException ce){out.println(ce);}

con = DriverManager.getConnection("jdbc:mysql://localhost/ruserba","root","");

PreparedStatement st;
String key= request.getParameter("keyword");
String query = "SELECT * , (case when kategori like '%"+key+"%' then 1 else 0 end) + (case when nama like '%"+key+"%' then 1 else 0 end) + (case when keterangan like '%"+key+"%' then 1 else 0 end) as priority from barang where (kategori like '%"+key+"%' or nama like '%"+key+"%' or keterangan like '%"+key+"%' or harga like '%"+key+"%')";

if (!request.getParameter("kategori").equals("default")) {
query += " and kategori='" + request.getParameter("kategori")+"'";
}
query += " order by "+request.getParameter("sortBy")+" DESC";

/*
if (!request.getParameter("kategori").equals("default")) {
query += " and kategori="+request.getParameter("kategori");
}
query += "order by "+request.getParameter("sortBy")+" desc";*/

st = con.prepareStatement(query);

ResultSet rs;
rs = st.executeQuery();
%>

<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="letoy.css">
<link rel="stylesheet" type="text/css" href="browse.css">
<script src="header.js"></script>
<script src="ajaxLoader.js"></script>
<script>
var kategori = getUrlVars()['kategori'];
var keyword = getUrlVars()['keyword'];
var sort = getUrlVars()['sortBy'];

function getUrlVars() {
var vars = {};
var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) {
vars[key] = value;
});
return vars;
}

function sorting(sort) {
document.location.href = 'browse.jsp?sortBy='+sort+'&currentPage=1&keyword='+keyword+'&kategori='+kategori;
}

var page = getUrlVars()['currentPage'];

function onPrevClicked() {
if (page > 1) {
page--;
document.location.href = 'browse.jsp?sortBy='+sort+'&currentPage='+page+'&keyword='+keyword+'&kategori='+kategori;
}
}

function onNextClicked() {
page++;
document.location.href = 'browse.jsp?sortBy='+sort+'&currentPage='+page+'&keyword='+keyword+'&kategori='+kategori;
}

</script>
</head>
<body>
<div id='topBorder'></div>

<div id='contentArea'>
<div class="browseOption">
<h2>
sort by :
<select class="dropDown" name="sort" onchange='sorting(this.value)'>
<option value="default">sorting...</option>
<option value="popularitas">popularitas</option>
<option value="harga">harga</option>
<option value="kategori">jenis barang</option>
</select>
</h2>
</div>

<div id='searchedItem'>
<%
int temp = 0;
int currentIndex = Integer.parseInt(request.getParameter("currentPage"));
while (rs.next()) {
if (temp >= (currentIndex*10 - 10) && (temp < (currentIndex*10))) {
String s = "<div class='itemView'>";
s+= "<img src='resource/"+rs.getString(1)+".jpg' class='itemImage'>";
s+= "<form class='itemDetail' action='addItem.jsp'>";
s+= "<h1><a href='detail.jsp?nama="+rs.getString(1)+"'> "+rs.getString(1)+" </a></h1></br>";
s+= "<h2>harga @ Rp."+rs.getString(3)+"</br>";
s+= "jumlah pembelian: <input class='textInput' id='quantity' type='text' name='jumlahBarang' value=0></br>";
s+= "<input class='button' type='submit' value='tambahkan ke tas'></h2>";
s+= "<input id='hilang' name='namabarang' value='"+rs.getString(1)+"'>";
s+= "<input id='hilang' name='username' value='yanuararistya'>";
s+= "</form>";
s+= "</div>";
out.println(s);
}
temp++;
}
%>
</div>

<div class="browseOption">
<button class='button' onclick='onPrevClicked()'>Prev</button>
<button class='button' onclick='onNextClicked()'>Next</button>
</div>
</div>

<div id='header' >
<script> drawHeaderContent(); </script>
</div>

</body>
<%
st.clearParameters();
con.close();
} catch (Exception e) {out.println(e.toString()); };
%>
</html>
35 changes: 35 additions & 0 deletions checkBarang.jsp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<%--
Document : checkBarang
Created on : Nov 27, 2013, 5:10:11 PM
Author : Administrator
--%>

<%@ page import="java.sql.*, javax.sql.*, java.io.*, javax.naming.*" %>
<%
Connection con = null;
try {
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
}catch(ClassNotFoundException ce){out.println(ce);}

con = DriverManager.getConnection("jdbc:mysql://localhost/test","root","");

String namab = request.getParameter("nb");

PreparedStatement st;

st = con.prepareStatement("SELECT nama_barang FROM Barang WHERE nama_barang=?");
st.setString(1, namab);

ResultSet rs;
rs = st.executeQuery();

if(rs.next()) {
response.getWriter().print(rs.getString(1) + " sudah terpakai");
} else response.getWriter().print(1);

st.clearParameters();
con.close();
} catch (Exception e) {out.println("Unable to connect to database."); };

%>
30 changes: 30 additions & 0 deletions checkEmail.jsp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<%@ page import="java.sql.*, javax.sql.*, java.io.*, javax.naming.*" %>
<%
Connection con = null;

try {
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
}catch(ClassNotFoundException ce){out.println(ce);}

con = DriverManager.getConnection("jdbc:mysql://localhost/ruserba","root","");

String email = request.getParameter("e");

PreparedStatement st;

st = con.prepareStatement("SELECT email FROM user WHERE email=?");
st.setString(1, email);

ResultSet rs;
rs = st.executeQuery();

if(rs.next()) {
response.getWriter().print(rs.getString(1) + " sudah terpakai");
} else response.getWriter().print(1);

st.clearParameters();
con.close();
} catch (Exception e) {out.println("Unable to connect to database."); };

%>
30 changes: 30 additions & 0 deletions checkUsername.jsp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<%@ page import="java.sql.*, javax.sql.*, java.io.*, javax.naming.*" %>
<%
Connection con = null;

try {
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
}catch(ClassNotFoundException ce){out.println(ce);}

con = DriverManager.getConnection("jdbc:mysql://localhost/ruserba","root","");

String usr = request.getParameter("u");

PreparedStatement st;

st = con.prepareStatement("SELECT username FROM user WHERE username=?");
st.setString(1, usr);

ResultSet rs;
rs = st.executeQuery();

if(rs.next()) {
response.getWriter().print(rs.getString(1) + " sudah terpakai");
} else response.getWriter().print(1);

st.clearParameters();
con.close();
} catch (Exception e) {out.println("Unable to connect to database."); };

%>
Loading