JSP 오라클 Statement

JAVA/JSP 2015. 8. 21. 12:19

<%@ page language="java" contentType="text/html; charset=EUC-KR"

pageEncoding="EUC-KR"%>

<%@page import="java.sql.*"%>

<%@page import="javax.sql.*"%>

<%@page import="javax.naming.*"%>

<!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=EUC-KR">

<title>Insert title here</title>

</head>

<body>

<%

Connection con = null;

String sql = "insert into student(num,name) values(6,'king')";

String[] arr = { "홈길동", "뽀로로", "배트맨" };

try {


Context init = new InitialContext();

DataSource ds = (DataSource) init.lookup("java:comp/env/jdbc/OracleDB");

con = ds.getConnection();


Statement pstmt = con.createStatement();

int re = pstmt.executeUpdate(sql);

if(re!=0){

out.println("<h2>연결되었습니다.</h2>");

}

                   //모든 자원 close();


} catch (Exception e) {

out.println("<h2>연결실패하였습니다..</h2>");

e.printStackTrace();


}

%>


</body>

</html>

'JAVA > JSP' 카테고리의 다른 글

JSP 오라클 트랜잭션  (0) 2015.08.24
JSP 오라클 ResultSet  (0) 2015.08.24
JSP 오라클 PreparedStatement  (0) 2015.08.21
JSP 커넥션풀 속성  (0) 2015.08.21
JSP 커넥션풀 연결 하기  (0) 2015.08.21
블로그 이미지

왕왕왕왕

,

<%@ page language="java" contentType="text/html; charset=EUC-KR"

pageEncoding="EUC-KR"%>

<%@page import="java.sql.*"%>

<%@page import="javax.sql.*"%>

<%@page import="javax.naming.*"%>

<!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=EUC-KR">

<title>Insert title here</title>

</head>

<body>

<%

Connection con = null;

String sql = "insert into student(num,name) values(6,'king')";

String[] arr = { "홈길동", "뽀로로", "배트맨" };

try {


Context init = new InitialContext();

DataSource ds = (DataSource) init.lookup("java:comp/env/jdbc/OracleDB");

con = ds.getConnection();


PreparedStatement pstmt = con.prepareStatement("insert into                                        student(num,name) values(?,?)");


for (int i = 0; i < 3; i++) {


pstmt.setInt(1, i);

pstmt.setString(2, arr[i]);

pstmt.executeUpdate();

//한번 입력했을때마다 executeUpdate()를 해줘야 전송한다.

}


//모든 자원 close();


out.println("<h2>연결되었습니다.</h2>");


} catch (Exception e) {

out.println("<h2>연결실패하였습니다..</h2>");

e.printStackTrace();


}

%>


</body>

</html>

'JAVA > JSP' 카테고리의 다른 글

JSP 오라클 ResultSet  (0) 2015.08.24
JSP 오라클 Statement  (0) 2015.08.21
JSP 커넥션풀 속성  (0) 2015.08.21
JSP 커넥션풀 연결 하기  (0) 2015.08.21
JSP 커넥션풀 web.xml 내용추가  (0) 2015.08.21
블로그 이미지

왕왕왕왕

,

JSP 커넥션풀 속성

JAVA/JSP 2015. 8. 21. 11:41

 커넥션 풀의 속성 사용

: 커넥션 풀은 여러 속성을 지정할 수 있다.

 속성

설명

maxActive 

커넥션 풀이 제공할 최대 커넥션 갯수 

whenExhaustedAction

커넥션 풀에서 가져올 수 있는 커넥션이 없을 때 어떻게 동작할지를 지정.

0일 경우 에러 발생, 1일 경우 maxWait 속성에서 지정한 

시간만큼 커넥션을 구할때까지 기다림 2일 경우 

일시적으로 커넥션을 생성해서 사용

 maxWait

whenExhaustedAction 속성의 값이 1일 때 사용되는 대기 시간.

단위는 1/1000초, 0보다 작을 경우 무한히 대기

maxIdle 

사용되지 않고 풀에 저장될 수 있는 최대 커넥션 갯수.

음수일 경우 제한이 없음 

minIdle 

사용되지 않고 풀에 저장될 수 있는 최소 커넥션 갯수. 

testOnBorrow 

true일 경우 커넥션 풀에서 커넥션을 가져올 때 커넥션이 유효한지의 여부를 검사 

testOnReturn 

true일 경우 커넥션 풀에 커넥션을 반환할 때 커넥션이 유효한지의 여부를 검사 

timeBetweenEvctionRunsMillis 

사용되지 않는 커넥션을 추출하는 쓰레드의 실행 주기 지정.

양수가 아닐 경우 실행되지 않는다.

시간 단위는 1/1000초. 

numTestsPerEvictionRun 

사용되지 않는 커넥션을 몇 개 검사할 지 지정 

minEvictableIdleTimeMillis 

사용되지 않는 커넥션을 추출할 때 이 속석에서 지정한 시간 이상 비활성화 상태인 커넥션만 추출.
양수가 아닌 경우 비활성화된 시간으로는 풀에서 제거되지 않음.
시간 단위는 1/1000초 

testWhileIdle 

true일 경우 비활성화 커넥션을 추출할 때 커넥션이 유효한지의 여부를 검사해서 유효하지 않은 커넥션은 풀에서 제거 



출처 http://arihong218.tistory.com/13

'JAVA > JSP' 카테고리의 다른 글

JSP 오라클 Statement  (0) 2015.08.21
JSP 오라클 PreparedStatement  (0) 2015.08.21
JSP 커넥션풀 연결 하기  (0) 2015.08.21
JSP 커넥션풀 web.xml 내용추가  (0) 2015.08.21
JSP 커넥션풀 context.xml 내용추가  (0) 2015.08.21
블로그 이미지

왕왕왕왕

,

<%@ page language="java" contentType="text/html; charset=EUC-KR"

pageEncoding="EUC-KR"%>


<!-- import -->

<%@page import="java.sql.*"%>

<%@page import="javax.sql.*"%>

<%@page import="javax.naming.*"%>

<!-- /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=EUC-KR">

<title>Insert title here</title>

</head>

<body>


<!-- Connection -->

<%

Connection con = null;

try {


Context init = new InitialContext();

//JNDI를 이용하기 위한 객체 생성


DataSource ds = (DataSource) init.lookup("java:comp/env/jdbc/OracleDB");

                 // lookup(): 등록된 naming 서비스로부터 자원을 찾고자할 때 사용하는 메서드

                 //("jdbc/myconn"): JNDI 서비스에 접근하기 위한 기본 이름(이 자원을 찾겠다.

                         -->  web.xml의<res-ref-name>

    //JNDI의 모든 이름은 기본적으로 java:comp/env에 등록되어 있다.

                    //해당 영역에서 jdbc/myconn으로 설정된 이름을 획득한다.


con = ds.getConnection();

                   //source로 부터 Connection 객체를 획득한다. 

  //이 객체는 이제 Container의 DBCP에 의해 관리된다. 


out.println("<h2>연결되었습니다.</h2>");


} catch (Exception e) {

out.println("<h2>연결실패하였습니다..</h2>");


e.printStackTrace();


}

%>

<!-- /Connection -->

</body>

</html>





'JAVA > JSP' 카테고리의 다른 글

JSP 오라클 PreparedStatement  (0) 2015.08.21
JSP 커넥션풀 속성  (0) 2015.08.21
JSP 커넥션풀 web.xml 내용추가  (0) 2015.08.21
JSP 커넥션풀 context.xml 내용추가  (0) 2015.08.21
JSP 커넥션풀라이브러리 다운로드  (0) 2015.08.21
블로그 이미지

왕왕왕왕

,

<?xml version="1.0" encoding="ISO-8859-1"?>

<!-- Licensed to the Apache Software Foundation (ASF) under one or more contributor 

license agreements. See the NOTICE file distributed with this work for additional 

information regarding copyright ownership. The ASF licenses this file to 

You under the Apache License, Version 2.0 (the "License"); you may not use 

this file except in compliance with the License. You may obtain a copy of 

the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required 

by applicable law or agreed to in writing, software distributed under the 

License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS 

OF ANY KIND, either express or implied. See the License for the specific 

language governing permissions and limitations under the License. -->


<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://java.sun.com/xml/ns/javaee

                      http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"

version="3.0" metadata-complete="true">


<display-name>Welcome to Tomcat</display-name>

<description>

     Welcome to Tomcat

  </description>


<!-- 커넥션풀 추가 -->

<resource-ref>

<description>Connection</description>

<res-ref-name>jdbc/OracleDB</res-ref-name>

<res-type>javax.sql.DataSource</res-type>

<res-auth>Container</res-auth>

</resource-ref>

<!-- /커넥션풀 추가 끝 -->

</web-app>



'JAVA > JSP' 카테고리의 다른 글

JSP 커넥션풀 속성  (0) 2015.08.21
JSP 커넥션풀 연결 하기  (0) 2015.08.21
JSP 커넥션풀 context.xml 내용추가  (0) 2015.08.21
JSP 커넥션풀라이브러리 다운로드  (0) 2015.08.21
JSP 커넥션 풀이란?  (0) 2015.08.21
블로그 이미지

왕왕왕왕

,

<?xml version="1.0" encoding="UTF-8"?>

<!-- Licensed to the Apache Software Foundation (ASF) under one or more contributor 

license agreements. See the NOTICE file distributed with this work for additional 

information regarding copyright ownership. The ASF licenses this file to 

You under the Apache License, Version 2.0 (the "License"); you may not use 

this file except in compliance with the License. You may obtain a copy of 

the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required 

by applicable law or agreed to in writing, software distributed under the 

License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS 

OF ANY KIND, either express or implied. See the License for the specific 

language governing permissions and limitations under the License. --><!-- The contents of this file will be loaded for each web application -->

<Context>


<!-- Default set of monitored resources -->

<WatchedResource>WEB-INF/web.xml</WatchedResource>


<!-- Uncomment this to disable session persistence across Tomcat restarts -->

<!-- <Manager pathname="" /> -->


<!-- Uncomment this to enable Comet connection tacking (provides events 

on session expiration as well as webapp lifecycle) -->

<!-- <Valve className="org.apache.catalina.valves.CometConnectionManagerValve" 

/> -->


<!-- 커넥션풀 추가한부분 -->

<Resource 

             name="jdbc/OracleDB" auth="Container"

driverClassName="oracle.jdbc.driver.OracleDriver" 

type="javax.sql.DataSource"

url="jdbc:oracle:thin:@localhost:1521:orcl" 

            username="scott" password="tiger"

maxActive="20" maxIdle="10" maxWait="-1" />

       <!-- /커넥션풀 추가한부분 끝 -->


</Context>






             1. name : JNDI로 호출될 이름을 설정한다. (접근 -> java:comp/env/jdbc/myconn)

   2. auth : DBCP를 관리할 관리자 (Container or Application)

   3. type : 해당 resource의 return type 

      (DataSource는 Connection 객체를 반환할 수 있다.)

   4. factory : dbcp를 유용하는 관리 클래스 (Tomcat 5.x에 기본으로 존재하는 클래스)

      (직접 DBCP 클래스를 지정해도 동작하는데 문제가 없다.)

      (그러나, Factory 클래스를 이용하면 좀더 안정적으로 관리할 수 있다.)

   5. driverClassName : JDBC를 이용하기 위한 드라이버 클래스

   6. url : DB의 접속 URL (속성으로 자동 재 접속을 선택했다.)

   7. username : DB의 계정 명

   8. password : 계정에 대한 비밀번호

   9. maxActive : 최대 접속 허용 개수

   10. maxIdle : DB Pool에 여분으로 남겨질 최대 Connection 개수

   11. maxWait : DB 연결이 반환되는 Timeout의 최대 시간 (-1은 무한 대기)

   12. removeAbandoned : Connection이 잘못 관리되어 버려진 연결을 찾아 재활용할 것                     인지의 여부 설정

       (true 설정일 때 현재 DB 연결이 적으면 버려진 연결을 찾아 재활용)

   13. removeAbandonedTimeout : 버려진 연결로 인식할 기본 시간 설정

       (초 단위로 해당 시간이 지나면 버려진 연결로 인식한다.)

'JAVA > JSP' 카테고리의 다른 글

JSP 커넥션풀 연결 하기  (0) 2015.08.21
JSP 커넥션풀 web.xml 내용추가  (0) 2015.08.21
JSP 커넥션풀라이브러리 다운로드  (0) 2015.08.21
JSP 커넥션 풀이란?  (0) 2015.08.21
Jsp page 지시어  (0) 2015.08.19
블로그 이미지

왕왕왕왕

,

http://commons.apache.org/ 1.주소로이동




2. 아래 보이는 3가지 모두 zip파일 다운로드








3. 압축풀고 난 후


C:\Users\User\commons-dbcp-1.3-bin\commons-dbcp-1.3

C:\Users\User\commons-collections-3.2.1-bin\commons-collections-3.2.1

C:\Users\User\commons-pool-1.6-bin\commons-pool-1.6


경로에 jar파일들을 복사한다.




4. 톰캣 라이브러리에 붙여넣기해준다.


C:\Users\User\apache-tomcat-7.0.63-windows-x86\apache-tomcat-7.0.63\lib




5. 개발환경 라이브러리에다 3가지 모두넣어준다.


C:\Users\User\workspace\dbconnection\WebContent\WEB-INF\lib




6. 서버폴더에보면 context,xml하고 web.xml이있는데

context.xml은 META-INF에 복사하고

web.xml은 WEB-INF에 복사한다.


C:\Users\User\workspace\Servers\Tomcat v7.0 Server at localhost-config





'JAVA > JSP' 카테고리의 다른 글

JSP 커넥션풀 web.xml 내용추가  (0) 2015.08.21
JSP 커넥션풀 context.xml 내용추가  (0) 2015.08.21
JSP 커넥션 풀이란?  (0) 2015.08.21
Jsp page 지시어  (0) 2015.08.19
Jsp a태그로 값넘기기(페이지 전환)  (1) 2015.08.19
블로그 이미지

왕왕왕왕

,

JSP 커넥션 풀이란?

JAVA/JSP 2015. 8. 21. 11:18

1) 커넥션 풀

: 데이터 베이스와 연결된 커넥션을 미리 만들어서 풀(pool) 속에 저장해 두고 있다가 필요할 때에 커넥션을 풀에서 가져다 쓰고 다시 풀에 반환하는 기법을 의미한다.



2) 커넥션 풀의 특징

 - 풀 속에 미리 커넥션이 생성되어 있기 때문에 커넥션을 생성하는 데 드는 연결 시간이 소비되지 

    않는다.

 - 커넥션을 계속해서 재사용하기 때문에 생성되는 커넥션 수가 많지 않다.



☞ 커넥션 풀을 사용하면 커넥션을 생성하고 닫는 시간이 소모되지 않기 때문에 그만큼 어플리케이션의 실행 속도가 빨라지며, 또한 한 번에 생성될 수 있는 커넥션 수를 제어하기 때문에 동시 접속자 수가 몰려도 웹 어플리케이션이 쉽게 다운되지 않는다.



3) 그렇다면 동시 접속자 처리는..?

: 커넥션 풀에서 생성되어 있는 커넥션의 갯수는 한정적이다. 그렇다면 동시 접속자가 많아지면 어떻게 될까?

커넥션 풀은 누군자 접속하면 커넥션 풀에 남아 있는 커넥션을 제공하는 식이다. 하지만 남아있는 커넥션이 없을 경우 해당 클라이언트는 대기 상태로 전환이 되고, 커넥션이 반환되면 대기하고 있는 순서대로 커넥션이 제공된다.




출처 http://arihong218.tistory.com/13

블로그 이미지

왕왕왕왕

,