DBConnetcion은 java로 만든다.
tomcat 기준이며 필요 라이브러리는 이클립스 으로 DynamicWeb 생성 했을때 기준이며 기본 라이브러리기준에
tomcat-dbcp.jar , ojdbc5.jar 가 필수이다.
기본 src 밑에 DBConnetcionl.java를 생성해준다(패키지구조를 상단에 test로 만듬)
----DBConnetcionl.java----
package test;
import java.sql.Connection;
import java.sql.SQLException;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
public class DBConnection {
public static Connection getConnection() throws SQLException, NamingException, ClassNotFoundException {
Context initCtx = new InitialContext();
//initCtx의 lookup메서드를 이용해서 "java:comp/env" 에 해당하는 객체를 찾아서 evnCtx에 삽입
Context envCtx = (Context) initCtx.lookup("java:comp/env");
//envCtx의 lookup메서드를 이용해서 "선언된JNDI명칭"에 해당하는 객체를 찾아서 ds에 삽입
DataSource ds = (DataSource) envCtx.lookup("선언된JNDI명칭");
//getConnection메서드를 이용해서 커넥션 풀로 부터 커넥션 객체를 얻어내어 conn변수에 저장
Connection conn = ds.getConnection();
return conn;
}
}
그리고 tomcat의 context.xml에 JNDI 선언을 </Context> 안에 해준다.
-----context.xml-----
<Resource auth="Container"
name="선언하고싶은이름"
driverClassName="oracle.jdbc.driver.OracleDriver"
type="javax.sql.DataSource"
url="jdbc:oracle:thin:@ip주소:포트번호:전역 데이터베이스 이름"
username="접속계정"
password="계정 비밀번호"
loginTimeout="10"
maxActive="10"
maxIdle="5"
maxWait="5000"
testOnBorrow="true" />
그리고 소스 WEB-INF 하단의 web.xml에 해당 내용에 대한 선언(<resource-ref>요부분)을 해준다.
---web.xml----
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>DbConTest</display-name>
<resource-ref>
<description>connection</description>
<res-ref-name>선언하고싶은 명칭</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
list.jsp 생성 데이터읽어올 페이지
----list.jsp---
</html>
pageEncoding="UTF-8"%>
<%@ page import="java.sql.*"%>
<%@ page import="test.DBConnection"%> <!-- DB연결 클래스를 import한다. -->
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<center>
<table border="3" bordercolor="skyblue">
<tr bgcolor="skyblue"><td>TEST1</td><td>TEST2</td></tr>
<%
// 쿼리문
String query="SELECT TEST1,TEST2 FROM TEST";
// 커넥션 연결
Connection conn = DBConnection.getConnection();
// DB에 쿼리문을 보낸다.
PreparedStatement pstmt = conn.prepareStatement(query);
// 쿼리문의 결과값을 rs에 담는다.
ResultSet rs = pstmt.executeQuery();
// 결과값을 출력한다.
while(rs.next()){
out.println("<tr>");
out.println("<td>"+rs.getString("TEST1")+"</td>");
out.println("<td>"+rs.getString("TEST2")+"</td>");
out.println("</tr>");
}
%>
</table>
</center>
</body>
</html>
----insert.jsp(입력페이지)----
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.sql.*"%>
<%@ page import="test.DBConnection"%> <!-- DB연결 클래스를 import한다. -->
<%
// 커넥션 연결
Connection conn = DBConnection.getConnection();
PreparedStatement psmt = null;
StringBuffer sql = new StringBuffer(" INSERT INTO TEST(TEST1, TEST2 ,REG_DT ) VALUES (?, ?, SYSDATE) ");
psmt = conn.prepareStatement(sql.toString());
//반복횟수
for(int i = 0; i<=500; i++) {
psmt.setString(1, "AAAAAA");
psmt.setString(2, "test"+i);
psmt.addBatch();
psmt.clearParameters();
//메모리풀을 방지하기 위한 배치 실행
if(i % 100 == 0){
System.out.println("batch-excute:"+i);
psmt.executeBatch();
}
}
//최종남았을때 한번더해줌
psmt.executeBatch();
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<center>
들어갔음
</center>
</body>
</html>
'JSP·script·jquery' 카테고리의 다른 글
JSP DB 이미지 BLOB 이미지 업로드 및 페이지 출력 (0) | 2017.11.22 |
---|---|
jquery 엔터키 이벤트 (0) | 2017.04.25 |
ie 브라우저 묻지 않고 창닫기 (0) | 2017.03.16 |
크롬은 원본창 window.close 안되고 자식창만 닫을수 있게 변경되었음 (0) | 2017.03.16 |
css 선택자를 통한 특정 이미지 교체 (0) | 2017.02.21 |