Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
L
LatihanJavaServlet_dioharvandy
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Dio Harvandy
LatihanJavaServlet_dioharvandy
Commits
1f624fb1
Commit
1f624fb1
authored
Mar 23, 2022
by
Dio Harvandy
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Latihan 5
Pembuatan koneksi dan CRUD database
parent
a46f98ed
Changes
10
Show whitespace changes
Inline
Side-by-side
Showing
10 changed files
with
429 additions
and
1 deletion
+429
-1
Latihan5_Data.java
src/main/java/com/latihan/web/Latihan5_Data.java
+28
-0
Latihan5_conncectDatabase.java
src/main/java/com/latihan/web/Latihan5_conncectDatabase.java
+21
-0
Latihan5_deleteData.java
src/main/java/com/latihan/web/Latihan5_deleteData.java
+56
-0
Latihan5_editData.java
src/main/java/com/latihan/web/Latihan5_editData.java
+72
-0
Latihan5_tambahData.java
src/main/java/com/latihan/web/Latihan5_tambahData.java
+57
-0
Latihan5_tampilData.java
src/main/java/com/latihan/web/Latihan5_tampilData.java
+67
-0
Latihan4_login.jsp
src/main/webapp/Latihan4_login.jsp
+1
-1
Latihan5_editData.jsp
src/main/webapp/Latihan5_editData.jsp
+43
-0
Latihan5_tambahData.jsp
src/main/webapp/Latihan5_tambahData.jsp
+42
-0
Latihan5_tampilData.jsp
src/main/webapp/Latihan5_tampilData.jsp
+42
-0
No files found.
src/main/java/com/latihan/web/Latihan5_Data.java
0 → 100644
View file @
1f624fb1
package
com
.
latihan
.
web
;
public
class
Latihan5_Data
{
private
int
id_bank
;
private
String
kode
;
private
String
nama
;
public
int
getId_bank
()
{
return
id_bank
;
}
public
void
setId_bank
(
int
id_bank
)
{
this
.
id_bank
=
id_bank
;
}
public
String
getKode
()
{
return
kode
;
}
public
void
setKode
(
String
kode
)
{
this
.
kode
=
kode
;
}
public
String
getNama
()
{
return
nama
;
}
public
void
setNama
(
String
nama
)
{
this
.
nama
=
nama
;
}
}
src/main/java/com/latihan/web/Latihan5_conncectDatabase.java
0 → 100644
View file @
1f624fb1
package
com
.
latihan
.
web
;
import
java.sql.Connection
;
import
java.sql.DriverManager
;
import
java.sql.SQLException
;
public
class
Latihan5_conncectDatabase
{
protected
static
Connection
initializeDatabase
()
throws
SQLException
,
ClassNotFoundException
{
String
dbDriver
=
"com.mysql.cj.jdbc.Driver"
;
String
dbUrl
=
"jdbc:mysql://localhost/"
;
String
dbName
=
"java_minggu_ketiga"
;
String
dbUsername
=
"root"
;
String
dbPassword
=
"root"
;
Class
.
forName
(
dbDriver
);
Connection
con
=
DriverManager
.
getConnection
(
dbUrl
+
dbName
,
dbUsername
,
dbPassword
);
return
con
;
}
}
src/main/java/com/latihan/web/Latihan5_deleteData.java
0 → 100644
View file @
1f624fb1
package
com
.
latihan
.
web
;
import
java.io.IOException
;
import
java.sql.Connection
;
import
java.sql.PreparedStatement
;
import
java.sql.SQLException
;
import
javax.servlet.ServletException
;
import
javax.servlet.annotation.WebServlet
;
import
javax.servlet.http.HttpServlet
;
import
javax.servlet.http.HttpServletRequest
;
import
javax.servlet.http.HttpServletResponse
;
/**
* Servlet implementation class Latihan5_deleteData
*/
@WebServlet
(
"/latihan5-deletedata"
)
public
class
Latihan5_deleteData
extends
HttpServlet
{
private
static
final
long
serialVersionUID
=
1L
;
/**
* @see HttpServlet#HttpServlet()
*/
public
Latihan5_deleteData
()
{
super
();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected
void
doGet
(
HttpServletRequest
request
,
HttpServletResponse
response
)
throws
ServletException
,
IOException
{
try
{
Connection
con
=
Latihan5_conncectDatabase
.
initializeDatabase
();
PreparedStatement
st
=
con
.
prepareStatement
(
"delete from bank where id_bank = ?"
);
st
.
setString
(
1
,
request
.
getParameter
(
"id_bank"
));
st
.
execute
();
st
.
close
();
con
.
close
();
response
.
sendRedirect
(
request
.
getContextPath
()+
"/latihan5-tampildata"
);
}
catch
(
ClassNotFoundException
|
SQLException
e
)
{
e
.
printStackTrace
();
}
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected
void
doPost
(
HttpServletRequest
request
,
HttpServletResponse
response
)
throws
ServletException
,
IOException
{
// TODO Auto-generated method stub
doGet
(
request
,
response
);
}
}
src/main/java/com/latihan/web/Latihan5_editData.java
0 → 100644
View file @
1f624fb1
package
com
.
latihan
.
web
;
import
java.io.IOException
;
import
java.sql.Connection
;
import
java.sql.PreparedStatement
;
import
java.sql.ResultSet
;
import
java.sql.SQLException
;
import
java.sql.Statement
;
import
javax.servlet.ServletException
;
import
javax.servlet.annotation.WebServlet
;
import
javax.servlet.http.HttpServlet
;
import
javax.servlet.http.HttpServletRequest
;
import
javax.servlet.http.HttpServletResponse
;
/**
* Servlet implementation class Latihan5_editData
*/
@WebServlet
(
"/latihan5-editdata"
)
public
class
Latihan5_editData
extends
HttpServlet
{
private
static
final
long
serialVersionUID
=
1L
;
/**
* @see HttpServlet#HttpServlet()
*/
public
Latihan5_editData
()
{
super
();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected
void
doGet
(
HttpServletRequest
request
,
HttpServletResponse
response
)
throws
ServletException
,
IOException
{
try
{
Connection
con
=
Latihan5_conncectDatabase
.
initializeDatabase
();
Statement
stmt
=
(
Statement
)
con
.
createStatement
();
ResultSet
rs
=
stmt
.
executeQuery
(
"select * from bank where id_bank = "
+
Integer
.
valueOf
(
request
.
getParameter
(
"id_bank"
)));
rs
.
next
();
request
.
setAttribute
(
"id_bank"
,
rs
.
getInt
(
"id_bank"
));
request
.
setAttribute
(
"kode"
,
rs
.
getString
(
"kode"
));
request
.
setAttribute
(
"nama"
,
rs
.
getString
(
"nama"
));
request
.
getRequestDispatcher
(
"Latihan5_editData.jsp"
).
forward
(
request
,
response
);
}
catch
(
ClassNotFoundException
|
SQLException
e
)
{
e
.
printStackTrace
();
}
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected
void
doPost
(
HttpServletRequest
request
,
HttpServletResponse
response
)
throws
ServletException
,
IOException
{
try
{
Connection
con
=
Latihan5_conncectDatabase
.
initializeDatabase
();
PreparedStatement
st
=
con
.
prepareStatement
(
"update bank set kode = ?, nama = ? where id_bank = ?"
);
st
.
setString
(
1
,
request
.
getParameter
(
"kode"
));
st
.
setString
(
2
,
request
.
getParameter
(
"nama"
));
st
.
setString
(
3
,
request
.
getParameter
(
"id_bank"
));
st
.
executeUpdate
();
st
.
close
();
con
.
close
();
response
.
sendRedirect
(
request
.
getContextPath
()+
"/latihan5-tampildata"
);
}
catch
(
ClassNotFoundException
|
SQLException
e
)
{
e
.
printStackTrace
();
}
}
}
src/main/java/com/latihan/web/Latihan5_tambahData.java
0 → 100644
View file @
1f624fb1
package
com
.
latihan
.
web
;
import
java.io.IOException
;
import
java.sql.Connection
;
import
java.sql.PreparedStatement
;
import
java.sql.SQLException
;
import
javax.servlet.ServletException
;
import
javax.servlet.annotation.WebServlet
;
import
javax.servlet.http.HttpServlet
;
import
javax.servlet.http.HttpServletRequest
;
import
javax.servlet.http.HttpServletResponse
;
/**
* Servlet implementation class Latihan5_tambahData
*/
@WebServlet
(
"/latihan5-tambahdata"
)
public
class
Latihan5_tambahData
extends
HttpServlet
{
private
static
final
long
serialVersionUID
=
1L
;
/**
* @see HttpServlet#HttpServlet()
*/
public
Latihan5_tambahData
()
{
super
();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected
void
doGet
(
HttpServletRequest
request
,
HttpServletResponse
response
)
throws
ServletException
,
IOException
{
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected
void
doPost
(
HttpServletRequest
request
,
HttpServletResponse
response
)
throws
ServletException
,
IOException
{
try
{
Connection
con
=
Latihan5_conncectDatabase
.
initializeDatabase
();
PreparedStatement
st
=
con
.
prepareStatement
(
"insert into bank(kode,nama) values(?,?)"
);
st
.
setString
(
1
,
request
.
getParameter
(
"kode"
));
st
.
setString
(
2
,
request
.
getParameter
(
"nama"
));
st
.
executeUpdate
();
st
.
close
();
con
.
close
();
response
.
sendRedirect
(
request
.
getContextPath
()+
"/latihan5-tampildata"
);
}
catch
(
ClassNotFoundException
|
SQLException
e
)
{
e
.
printStackTrace
();
}
}
}
src/main/java/com/latihan/web/Latihan5_tampilData.java
0 → 100644
View file @
1f624fb1
package
com
.
latihan
.
web
;
import
java.io.IOException
;
import
java.sql.Connection
;
import
java.sql.ResultSet
;
import
java.sql.SQLException
;
import
java.sql.Statement
;
import
java.util.ArrayList
;
import
java.util.List
;
import
javax.servlet.ServletException
;
import
javax.servlet.annotation.WebServlet
;
import
javax.servlet.http.HttpServlet
;
import
javax.servlet.http.HttpServletRequest
;
import
javax.servlet.http.HttpServletResponse
;
/**
* Servlet implementation class Latihan5_tampilData
*/
@WebServlet
(
"/latihan5-tampildata"
)
public
class
Latihan5_tampilData
extends
HttpServlet
{
private
static
final
long
serialVersionUID
=
1L
;
/**
* @see HttpServlet#HttpServlet()
*/
public
Latihan5_tampilData
()
{
super
();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected
void
doGet
(
HttpServletRequest
request
,
HttpServletResponse
response
)
throws
ServletException
,
IOException
{
try
{
Connection
con
=
Latihan5_conncectDatabase
.
initializeDatabase
();
Statement
stmt
=
(
Statement
)
con
.
createStatement
();
ResultSet
rs
=
stmt
.
executeQuery
(
"select * from bank"
);
List
<
Latihan5_Data
>
bankList
=
new
ArrayList
<
Latihan5_Data
>();
while
(
rs
.
next
())
{
Latihan5_Data
bank
=
new
Latihan5_Data
();
bank
.
setId_bank
(
rs
.
getInt
(
"id_bank"
));
bank
.
setKode
(
rs
.
getString
(
"kode"
));
bank
.
setNama
(
rs
.
getString
(
"nama"
));
bankList
.
add
(
bank
);
}
request
.
setAttribute
(
"bankList"
,
bankList
);
request
.
getRequestDispatcher
(
"Latihan5_tampilData.jsp"
).
forward
(
request
,
response
);
}
catch
(
ClassNotFoundException
|
SQLException
e
)
{
e
.
printStackTrace
();
}
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected
void
doPost
(
HttpServletRequest
request
,
HttpServletResponse
response
)
throws
ServletException
,
IOException
{
// TODO Auto-generated method stub
doGet
(
request
,
response
);
}
}
src/main/webapp/Latihan4_login.jsp
View file @
1f624fb1
...
...
@@ -16,7 +16,7 @@
color
:
white
;
}
</style>
<title>
Insert title here
</title>
<title>
Login
</title>
</head>
<body>
<form
action=
"latihan4-login"
method=
"post"
>
...
...
src/main/webapp/Latihan5_editData.jsp
0 → 100644
View file @
1f624fb1
<%@ page
language=
"java"
contentType=
"text/html; charset=ISO-8859-1"
pageEncoding=
"ISO-8859-1"
%>
<!DOCTYPE html>
<html>
<head>
<meta
charset=
"ISO-8859-1"
>
<style
type=
"text/css"
>
.button
{
padding
:
10px
27px
;
text-align
:
center
;
font-size
:
10px
;
cursor
:
pointer
;
}
.yellow
{
background-color
:
#e9f018
;
color
:
black
;
}
</style>
<title>
Edit Data
</title>
</head>
<body>
<form
action=
"latihan5-editdata"
method=
"post"
>
<input
name=
"id_bank"
type=
"hidden"
value=
"${id_bank}"
>
<table
style=
"border: 1px solid; margin: auto;"
>
<tr>
<th
colspan=
"2"
style=
"padding: 10px"
>
Edit Data
</th>
</tr>
<tr>
<td><label>
Kode
</label></td>
<td>
:
<input
name =
"kode"
type =
"text"
value=
"${kode}"
></td>
</tr>
<tr>
<td><label>
Nama
</label></td>
<td>
:
<input
name =
"nama"
type =
"text"
value =
"${nama}"
></td>
</tr>
<tr>
<th
colspan=
"2"
style=
"padding: 10px"
><button
class=
"button yellow"
type =
"submit"
>
Edit
</button></th>
</tr>
</table>
</form>
</body>
</html>
\ No newline at end of file
src/main/webapp/Latihan5_tambahData.jsp
0 → 100644
View file @
1f624fb1
<%@ page
language=
"java"
contentType=
"text/html; charset=ISO-8859-1"
pageEncoding=
"ISO-8859-1"
%>
<!DOCTYPE html>
<html>
<head>
<meta
charset=
"ISO-8859-1"
>
<style
type=
"text/css"
>
.button
{
padding
:
10px
27px
;
text-align
:
center
;
font-size
:
10px
;
cursor
:
pointer
;
}
.blue
{
background-color
:
#188bf0
;
color
:
white
;
}
</style>
<title>
Tambah Data
</title>
</head>
<body>
<form
action=
"latihan5-tambahdata"
method=
"post"
>
<table
style=
"border: 1px solid; margin: auto;"
>
<tr>
<th
colspan=
"2"
style=
"padding: 10px"
>
Tambah Data
</th>
</tr>
<tr>
<td><label>
Kode
</label></td>
<td>
:
<input
name =
"kode"
type =
"text"
></td>
</tr>
<tr>
<td><label>
Nama
</label></td>
<td>
:
<input
name =
"nama"
type =
"text"
></td>
</tr>
<tr>
<th
colspan=
"2"
style=
"padding: 10px"
><button
class=
"button blue"
type =
"submit"
>
Tambah
</button></th>
</tr>
</table>
</form>
</body>
</html>
\ No newline at end of file
src/main/webapp/Latihan5_tampilData.jsp
0 → 100644
View file @
1f624fb1
<%@ page
language=
"java"
contentType=
"text/html; charset=ISO-8859-1"
pageEncoding=
"ISO-8859-1"
%>
<%@ page
import =
"java.util.ArrayList"
%>
<%@ page
import =
"java.util.List"
%>
<%@ page
import =
"com.latihan.web.Latihan5_Data"
%>
<!DOCTYPE html>
<html>
<head>
<meta
charset=
"ISO-8859-1"
>
<style>
table
,
td
,
th
{
border
:
1px
solid
;
padding
:
5px
;
}
</style>
<title>
Data Bank
</title>
</head>
<body>
<table
style=
"margin: auto; border-collapse: collapse;"
>
<tr>
<th
colspan=
"5"
style=
"padding: 10px"
>
Data Bank |
<a
href=
"Latihan5_tambahData.jsp"
>
Tambah Data
</a></th>
</tr>
<tr>
<th>
ID BANK
</th>
<th>
KODE
</th>
<th>
NAMA
</th>
<th>
ACTION
</th>
</tr>
<%
List
<
Latihan5_Data
>
bankList
=
(
ArrayList
<
Latihan5_Data
>
)
request
.
getAttribute
(
"bankList"
);
for
(
Latihan5_Data
bank
:
bankList
){
%>
<tr>
<td><%=
bank
.
getId_bank
()
%></td>
<td><%=
bank
.
getKode
()
%></td>
<td><%=
bank
.
getNama
()
%></td>
<td><a
href=
"http://localhost:8080/LatihanJavaServlet_dioharvandy/latihan5-editdata?id_bank=
<%=
bank
.
getId_bank
()
%>
"
>
Edit
</a>
|
<a
href=
"http://localhost:8080/LatihanJavaServlet_dioharvandy/latihan5-deletedata?id_bank=
<%=
bank
.
getId_bank
()
%>
"
onclick=
"return confirm('Delete This Item?');"
>
Delete
</a></td>
</tr>
<%
}
%>
</table>
</body>
</html>
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment