JSP 스크립트릿에서 DBCP 연동
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" import="java.sql.*"%>
<%
Connection objConn = null;
Statement objStmt = null;
ResultSet objRs = null;
int num = 0;
String uName = "";
String hakbeon = "";
String address = "";
int counter = 0;
try{
Class.forName("com.mysql.cj.jdbc.Driver");
//Class 클래스의 하위메서드 forName은 외부 클래스를 현재 파일에서
// 사용할 수 있도록 만들어준다. => 외부 클래스가 있는 곳의 경로 설정
String url = "jdbc:mysql://localhost:3306/school?"; // DB_URL
url += "useSSL=false&"; // 인증방식설정
url += "serverTimezone=Asia/Seoul&"; // 서버시간설정
url += "useUnicode=true&"; // 유니코드사용여부설정
url += "characterEncoding=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 인터페이스의 객체화된 참조변수
%>
<!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>Document</title>
<link rel="stylesheet" href="style/style.css">
<style>
table, td {
font-size: 17px;
border: 1px solid black;
}
</style>
</head>
<body>
<div id="wrap">
<h1>JSP 스크립트릿에서 DBCP 연동</h1>
<h3>학생정보</h3>
<table>
<tbody>
<tr>
<td>번호(num)</td>
<td>이름(uName)</td>
<td>학번(hakbeon)</td>
<td>주소(address)</td>
</tr>
<%
if(objRs != null) {
while(objRs.next()) {
num = objRs.getInt("num");
uName = objRs.getString("uName");
hakbeon = objRs.getString("hakbeon");
address = objRs.getString("address");
%>
<tr>
<td><%=num %></td>
<td><%=uName %></td>
<td><%=hakbeon %></td>
<td><%=address %></td>
</tr>
<%
counter++;
} // end while
} // end if
%>
</tbody>
</table>
<br><br>
<h2>Total Records : <%=counter %></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>
<%
objRs.close();
objStmt.close();
objConn.close();
} catch(ClassNotFoundException e){
out.print("DB접속오류(드라이버 오류 입니다. )<br>");
out.print(e.getMessage());
} catch(SQLException e){
out.print("SQL코드오류<br>");
out.print(e.getMessage());
}
%>
Leave a comment