์ค๋์ JSP๋ฅผ ์ด์ฉํ์ฌ ๋ฐ์ดํ๋ฒ ์ด์ค์ ์ ์ํ๊ณ SELECT, INSERT, DELETE, ๊ทธ๋ฆฌ๊ณ UPDATE ํ๋ ๋ฐฉ๋ฒ์ ๋ํด์ ์์๋ณด๋๋ก ํ๊ฒ ์ต๋๋ค.
์ฐ์ ๋ฐ์ดํ๋ฒ ์ด์ค๋ฅผ ์ค์น๊ฐ๋์ด์๊ณ emp๊ณ์ ์ด ๋ง๋ค์ด์ ธ ์์ผ๋ฉฐ Employeesํ ์ด๋ธ์ด id, first, last, age ์ปฌ๋ผ์ผ๋ก ์ด๋ฃจ์ด์ ธ ์๋ค๊ณ ๊ฐ์ ์ ํ์ต๋๋ค.
SELECT Operation:
JTSL์ ์ด์ฉํ์ฌ JSP ํ์ด์ง๋ฅผ ์์ฑํฉ๋๋ค.
<%@ page import="java.io.*,java.util.*,java.sql.*"%> <%@ page import="javax.servlet.http.*,javax.servlet.*" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql"%> <html>
<head>
<title>SELECT Operation</title>
</head>
<body>
<sql:setDataSource var="snapshot" driver="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost/TEST" user="emp"
password="emp" />
<sql:query dataSource="${snapshot}" var="result"> SELECT * from Employees; </sql:query>
<table border="1" width="100%">
<tr>
<th>Emp ID</th>
<th>First Name</th>
<th>Last Name</th>
<th>Age</th>
</tr>
<c:forEach var="row" items="${result.rows}">
<tr>
<td>
<c:out value="${row.id}" />
</td>
<td>
<c:out value="${row.first}" />
</td>
<td>
<c:out value="${row.last}" />
</td>
<td>
<c:out value="${row.age}" />
</td>
</tr>
</c:forEach>
</table>
</body>
</html>
์ JSPํ์ด์ง์ ์ ์ํ๋ฉด ์๋์ ๊ฐ์ ๊ฒฐ๊ณผ๊ฐ ๋์ค๊ฒ ์ฃ ?
|
INSERT Operation:
์ญ์๋ JSTL์ ์ด์ฉํ JSP ํ์ด์ง์ ๋๋ค.
<%@ page import="java.io.*,java.util.*,java.sql.*"%> <%@ page import="javax.servlet.http.*,javax.servlet.*" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql"%> <html>
<head>
<title>JINSERT Operation</title>
</head>
<body>
<sql:setDataSource var="snapshot" driver="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost/TEST" user="emp"
password="emp" />
<sql:update dataSource="${snapshot}" var="result"> INSERT INTO Employees VALUES (104, 2, 'Nuha', 'Ali');
</sql:update>
<sql:query dataSource="${snapshot}" var="result"> SELECT * from Employees; </sql:query>
<table border="1" width="100%">
<tr>
<th>Emp ID</th>
<th>First Name</th>
<th>Last Name</th>
<th>Age</th>
</tr>
<c:forEach var="row" items="${result.rows}">
<tr>
<td>
<c:out value="${row.id}" />
</td>
<td>
<c:out value="${row.first}" />
</td>
<td>
<c:out value="${row.last}" />
</td>
<td>
<c:out value="${row.age}" />
</td>
</tr>
</c:forEach>
</table>
</body>
</html>
๊ฒฐ๊ณผ๋ ์๋์ฒ๋ผ ํ์ค์ด ์ถ๊ฐ๊ฐ ๋์ด ๋์ค๊ฒ ์ฃ .
|
DELETE Operation:
<%@ page import="java.io.*,java.util.*,java.sql.*"%> <%@ page import="javax.servlet.http.*,javax.servlet.*" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql"%> <html>
<head>
<title>DELETE Operation</title>
</head>
<body>
<sql:setDataSource var="snapshot" driver="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost/TEST" user="emp"
password="emp" />
<c:set var="empId" value="103" />
<sql:update dataSource="${snapshot}" var="count"> DELETE FROM Employees WHERE Id = ?
<sql:param value="${empId}" />
</sql:update>
<sql:query dataSource="${snapshot}" var="result"> SELECT * from Employees; </sql:query>
<table border="1" width="100%">
<tr>
<th>Emp ID</th>
<th>First Name</th>
<th>Last Name</th>
<th>Age</th>
</tr>
<c:forEach var="row" items="${result.rows}">
<tr>
<td>
<c:out value="${row.id}" />
</td>
<td>
<c:out value="${row.first}" />
</td>
<td>
<c:out value="${row.last}" />
</td>
<td>
<c:out value="${row.age}" />
</td>
</tr>
</c:forEach>
</table>
</body>
</html>
๊ฒฐ๊ณผ๋ ์๋์ ๊ฐ์ต๋๋ค.
|
UPDATE Operation:
<%@ page import="java.io.*,java.util.*,java.sql.*"%> <%@ page import="javax.servlet.http.*,javax.servlet.*" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql"%> <html>
<head>
<title>DELETE Operation</title>
</head>
<body>
<sql:setDataSource var="snapshot" driver="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost/TEST" user="emp"
password="emp" />
<c:set var="empId" value="102" />
<sql:update dataSource="${snapshot}" var="count"> UPDATE Employees SET last = 'Ali'
<sql:param value="${empId}" />
</sql:update>
<sql:query dataSource="${snapshot}" var="result"> SELECT * from Employees; </sql:query>
<table border="1" width="100%">
<tr>
<th>Emp ID</th>
<th>First Name</th>
<th>Last Name</th>
<th>Age</th>
</tr>
<c:forEach var="row" items="${result.rows}">
<tr>
<td>
<c:out value="${row.id}" />
</td>
<td>
<c:out value="${row.first}" />
</td>
<td>
<c:out value="${row.last}" />
</td>
<td>
<c:out value="${row.age}" />
</td>
</tr>
</c:forEach>
</table>
</body>
</html>
๊ฒฐ๊ณผ๋ ์๋์ฒ๋ผ ๋์ค๊ฒ ์ฃ ?
Emp ID |
First Name |
Last Name |
Age |
---|---|---|---|
100 |
Zara |
Ali |
18 |
101 |
Mahnaz |
Fatma |
25 |
102 |
Zaid |
Ali |
30 |
104 |
Nuha |
Ali |
2 |
'๐ป Programming > JSP' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
JSP vs Javascript (Difference between JSP and Javascript) (0) | 2019.06.28 |
---|---|
[JSP] Security ( ๋ณด์ ) (0) | 2019.02.15 |
[JSP] JSTL ( JSP Standard Tag Library ) (0) | 2019.02.15 |
[JSP] Page Redirection ( ํ์ด์ง ๋ฆฌ๋๋ ์ ) (0) | 2019.02.15 |
[JSP] File Upload ( ํ์ผ ์ ๋ก๋ ) (0) | 2019.02.15 |