JSP 에서 JDBC 로 Oracle DB 연동 후, 데이터 조회하기 과정을 진행한 것으로 가정하고 작성했다.
<%@page import="javax.sql.*"%>
<%@page import="javax.naming.*"%>
<%@page import="java.sql.*"%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<style>
th {
color: blue;
}
</style>
</head>
<body>
중략...
<%
Connection con = null;
Boolean connect = false;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
Context init = new InitialContext();
DataSource ds = (DataSource) init.lookup("java:comp/env/Oracle_park");
con = ds.getConnection();
connect = true;
if(connect = true) { %>
<h3>DB 연결 성공</h3>
<% } else { %>
<h3>DB 연결 실패</h3>
<% }
중략...
%>
중략...
<% }
중략...
%>
</table>
</body>
</html>
alert 창을 띄우기 위해서는 script 태그 내부에 alert 를 사용하면 된다.
그러나 sctipt 태그의 위치가 고민이었다.
head 태그 내부(style 태그 하단)에 위치하는 경우
body 태그 최하단에 위치하는 경우
body 태그 중간에 위치하는 경우
body 태그 중간에 script를 위치시켜 alert창을 띄우도록 했다.
(정확히는, if문 반환값으로)
<%@page import="javax.sql.*"%>
<%@page import="javax.naming.*"%>
<%@page import="java.sql.*"%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<style>
th {
color: blue;
}
</style>
</head>
<body>
<table border="1">
<tr>
<th>번호</th>
<th>이름</th>
<th>나이</th>
<th>성별</th>
</tr>
<%
Connection con = null;
Boolean connect = false;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
Context init = new InitialContext();
DataSource ds = (DataSource) init.lookup("java:comp/env/Oracle_park");
con = ds.getConnection();
connect = true;
if(connect = true) { %>
<script>alert("DB 연결 성공");</script>
<% } else { %>
<script>alert("DB 연결 실패");</script>
<% }
pstmt = con.prepareStatement("SELECT * FROM MEMBER");
rs = pstmt.executeQuery();
while(rs.next()) {
Long num = rs.getLong("NUM");
String name = rs.getString("NAME");
String age = rs.getString("AGE");
String sex = rs.getString("SEX");
%>
<tr>
<td><%=num%></td>
<td><%=name%></td>
<td><%=age%></td>
<td><%=sex%></td>
</tr>
<% }
} catch(Exception ex) {
connect = false;
ex.printStackTrace();
} finally {
if(rs != null) rs.close();
if(pstmt != null) pstmt.close();
if(con != null) con.close();
}
%>
</table>
</body>
</html>
중략...
<!DOCTYPE html>
<html>
<head>
중략...
</head>
<body>
중략...
<%
중략...
try {
Context init = new InitialContext();
DataSource ds = (DataSource) init.lookup("java:comp/env/Oracle_park");
con = ds.getConnection();
connect = true;
if(connect = true) {
out.println("<script>alert('DB 연결 성공');</script>");
} else {
out.println("<script>alert('DB 연결 실패');</script>");
}
중략...

참고: script 태그는 어느 곳에 위치하는 것이 좋을까?
참고: TIL 14 | script 태그 위치에 따른 차이점