Everything has an expiration date
[JSP & JDBC & Oracle] 20231212 [프로그램소스] - DBConn, Test001 본문
[JSP & JDBC & Oracle]/Program source (JSP & JDBC & Oracle)
[JSP & JDBC & Oracle] 20231212 [프로그램소스] - DBConn, Test001
Jelly-fish 2023. 12. 13. 01:19
WebApp008
DBConn.java
/*=============================================
DBConn.java
- 데이터베이스 연결 전용 객체(Singleton)
- 예외 처리 : throws
===============================================*/
package com.util;
import java.sql.Connection;
public class DBConn
{
private static Connection dbConn;
public static Connection getConnection() throws ClassNotFoundException, SQLException
{
if (dbConn == null)
{
String url = "jdbc:oracle:thin:@localhost:1521:xe";
String user = "scott";
String pwd = "tiger";
Class.forName("oracle.jdbc.driver.OracleDriver");
dbConn = DriverManager.getConnection(url, user, pwd);
}
return dbConn;
}
public static Connection getConnection(String url, String user, String pwd) throws ClassNotFoundException, SQLException
{
if (dbConn == null)
{
Class.forName("oracle.jdbc.driver.OracleDriver");
dbConn = DriverManager.getConnection(url, user, pwd);
}
return dbConn;
}
public static void close() throws SQLException
{
if (dbConn != null)
{
if (!dbConn.isClosed)
{
dbConn.close();
}
}
dbConn = null;
}
}
Test001.jsp
<%@page import="java.sql.Connection"%>
<%@page import="com.util.DBConn"%>
<%@ page contentType="text/html; charset=UTF-8"%>
<%
String str = "";
try
{
Connection conn = DBConn.getConnection();
if (conn != null)
str += "데이터베이스 연결 성공~!!!";
}
catch(Exception e)
{
//System.out.println(e.toString());
str += e.toString();
}
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Test001.jsp</title>
<link rel="stylesheet" type="text/css" href="css/main.css">
</head>
<body>
<div>
<h1>데이터베이스 연결 실습</h1>
<hr>
</div>
<div>
<!-- <h2>확인 결과 : 데이터베이스 연결 성공~!!!</h2> -->
<h2>확인 결과 : <%=str %></h2>
</div>
</body>
</html>