JSP 스크립트릿에서 DBCP+Bean 연동
Bean_Test.jsp
<%@page import="pack_Sample.DataBean"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"
import="java.sql.*, java.util.Vector, pack_Sample.DataBean"
%>
<jsp:useBean class="pack_Sample.Test" id="testObj" scope="page" />
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>JSP에서 DB연동(출력, Select)</title>
<link rel="stylesheet" href="style/style.css">
<style>
table, th, td {
font-size: 17px;
border: 1px solid #000;
}
</style>
</head>
<body>
<div id="wrap">
<h1>JSP 스크립트릿에서 DBCP+Bean 연동입니다.</h1>
<h3>학생정보</h3>
<table>
<tbody>
<tr>
<th>번호(num)</th>
<th>이름(uName)</th>
<th>학번(hakbeon)</th>
<th>주소(address)</th>
</tr>
<%
Vector vecList = testObj.mtdDBCP();
int cnt = vecList.size(); // 전체 데이터 갯수
for (int i=0; i<vecList.size(); i++) {
DataBean dataBean = (DataBean)vecList.get(i);
%>
<tr>
<td><%=dataBean.getNum() %></td>
<td><%=dataBean.getuName() %></td>
<td><%=dataBean.getHakbeon() %></td>
<td><%=dataBean.getAddress() %></td>
</tr>
<%
}
%>
</tbody>
</table>
<br><br>
<h2>Total Records : <%= cnt %></h2>
</div>
<!-- div#wrap -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<script src="script/script.js"></script>
</body>
</html>
Test.java
package pack_Sample;
import java.sql.*;
import java.util.Vector;
public class Test {
public Vector mtdDBCP() {
Connection objConn = null;
Statement objStmt = null;
ResultSet objRs = null;
Vector vector = new Vector();
try {
Class.forName("com.mysql.cj.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/school?"; // DB_URL
url += "useSSL=false&"; // 인증방식설정
url += "serverTimezone=Asia/Seoul&"; // 서버시간설정
url += "useUnicode=true&"; // 유니코드사용여부설정
url += "charact4erEncoding=UTF-8";
//url += "allowPublicKeyRetrieval=true";
String user = "root";
String password = "1234";
objConn = DriverManager.getConnection(url, user, password);
// DB 접속 끝
objStmt = objConn.createStatement();
String sql = "select * from studentlist order by num desc";
objRs = objStmt.executeQuery(sql);
// objRs는 ResultSet 인터페이스의 객체화된 참조변수
if (objRs != null) {
while (objRs.next()) {
DataBean dataBean = new DataBean();
dataBean.setNum(objRs.getInt("num"));
dataBean.setuName(objRs.getString("uname"));
dataBean.setHakbeon(objRs.getString("hakbeon"));
dataBean.setAddress(objRs.getString("address"));
// 클래스에서 출력하면 안됨
// 클래스에서는 데이터를 JSP로 반환해야 함.
vector.add(dataBean);
}
}
objRs.close();
objStmt.close();
objConn.close();
} catch (ClassNotFoundException e) {
System.out.println("DB연동 Class 이슈 : " + e.getMessage());
} catch (SQLException e) {
System.out.println("SQL 이슈 : " + e.getMessage());
}
return vector;
} // end of method mtdDBCP
}
DataBean.java
package pack_Sample;
public class DataBean {
private int num;
private String uName;
private String hakbeon;
private String address;
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public String getuName() {
return uName;
}
public void setuName(String uName) {
this.uName = uName;
}
public String getHakbeon() {
return hakbeon;
}
public void setHakbeon(String hakbeon) {
this.hakbeon = hakbeon;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
Leave a comment