오늘은 JSP를 이용하여 데이타베이스에 접속하고 SELECT, INSERT, DELETE, 그리고 UPDATE 하는 방법에 대해서 알아보도록 하겠습니다.
우선 데이타베이스를 설치가되어있고 emp계정이 만들어져 있으며 Employees테이블이 id, first, last, age 컬럼으로 이루어져 있다고 가정을 했습니다.
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페이지에 접속하면 아래와 같은 결과가 나오겠죠?
|
역시나 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>
결과는 아래처럼 한줄이 추가가 되어 나오겠죠.
|
<%@ 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>
결과는 아래와 같습니다.
|
<%@ 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 |
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 |
아래 예제는 AJAX를 이용해서 데이터베이스에서 읽어온 자료를 처리하는 것을 보여줍니다.
사용자가 드랍다운 리스트에서 고객을 선택하면, "showCustomer()" 함수가 호출되어 실행됩니다. 이 함수는 "onchange" 이벤트가 발생하면 호출되도록 되어있습니다.
function showCustomer(str) {
var xhttp;
if (str == "") {
document.getElementById("txtHint").innerHTML = "";
return;
}
xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (xhttp.readyState == 4 && xhttp.status == 200) {
document.getElementById("txtHint").innerHTML = xhttp.responseText;
}
};
xhttp.open("GET", "getcustomer.asp?q="+str, true);
xhttp.send();
}
showCustomer() 함수는 아래와 같은 작업을 합니다.
위 예제에서 사용된 서버페이지는 "getcustomer.asp"입니다.
서버 페이지 파일은 PHP, JSP와 같은 다른 서버 언어로도 구현할 수 있습니다.
"getcustomer.asp" 는 데이터베이스의 정보를 조회하고 읽어온 정보를 HTML 테이블에 넣는 기능을 합니다.
<%
response.expires=-1
sql="SELECT * FROM CUSTOMERS WHERE CUSTOMERID="
sql=sql & "'" & request.querystring("q") & "'"
set conn=Server.CreateObject("ADODB.Connection")
conn.Provider="Microsoft.Jet.OLEDB.4.0"
conn.Open(Server.Mappath("/datafolder/northwind.mdb"))
set rs=Server.CreateObject("ADODB.recordset")
rs.Open sql,conn
response.write("<table>")
do until rs.EOF
for each x in rs.Fields
response.write("<tr><td><b>" & x.name & "</b></td>")
response.write("<td>" & x.value & "</td></tr>")
next
rs.MoveNext
loop
response.write("</table>")
%>
[jQuery] 4. DOM Attributes ( DOM 속성 ) (0) | 2016.06.12 |
---|---|
[jQuery] 3. Selectors ( 셀렉터 ) (0) | 2016.06.12 |
AJAX란? - 이벤트 종류 (0) | 2016.06.12 |
AJAX란? - 서버로부터 응답받기 (0) | 2016.06.12 |
AJAX란? - 서버로 요청하기 (1) | 2016.06.12 |