4 v 12 2 JSP page content Type

  • Slides: 86
Download presentation

4. 웹 컴포넌트에서 데이터베이스를 사용하는 방법 v 데이터베이스의 데이터를 읽어오는 방법 [예제 12 -2]

4. 웹 컴포넌트에서 데이터베이스를 사용하는 방법 v 데이터베이스의 데이터를 읽어오는 방법 [예제 12 -2] 상품 정보 테이블을 읽는 JSP 페이지 <%@page content. Type= “text/html; charset=euc-kr ” error. Page= “DBError. jsp ” %> <%@page import= “java. sql. * ”%> <% String code = request. get. Parameter( “code ”); Connection conn = null; Statement stmt = null; try { Class. for. Name( “com. mysql. jdbc. Driver ”); conn = Driver. Manager. get. Connection(“jdbc: mysql: //localhost: 3306/webdb”, “root ”, “ 1234”); if (conn == null) throw new Exception( “데이터베이스에 연결할 수 없습니다. <BR> ”); stmt = conn. create. Statement(); Result. Set rs = stmt. execute. Query( “select * from goodsinfo where code = ‘” + code + “’; ”); if (rs. next()) { String title = rs. get. String( “title ”); String writer = rs. get. String( “writer ”); int price = rs. get. Int( “price ”); request. set. Attribute( “CODE ”, code); <%! request. set. Attribute( “TITLE ”, to. Unicode(title)); // ISO-8859 -1 문자열을 Unicode 문자열로 바꾸는 메서드 request. set. Attribute( “WRITER ”, to. Unicode(writer)); private String to. Unicode(String str) { request. set. Attribute( “PRICE ”, new Integer(price)); try { } } byte[] b = str. get. Bytes( “ISO-8859 -1 ”); finally { return new String(b); try { } stmt. close(); catch (java. io. Unsupported. Encoding. Exception uee) { } System. out. println(uee. get. Message()); catch (Exception ignored) { } return null; try { } conn. close(); } } %> catch (Exception ignored) { } } Request. Dispatcher dispatcher = request. get. Request. Dispatcher(“Goods. Info. Viewer. jsp”); dispatcher. forward(request, response); %> 46/85

4. 웹 컴포넌트에서 데이터베이스를 사용하는 방법 v 데이터베이스에 데이터를 입력하는 방법 [예제 12 -5]

4. 웹 컴포넌트에서 데이터베이스를 사용하는 방법 v 데이터베이스에 데이터를 입력하는 방법 [예제 12 -5] 회원 정보를 입력받는 HTML 문서 <HTML> <HEAD> <META http-equiv= “Content-Type ” content= “text/html; charset=euc-kr ”> <TITLE>회원 가입</TITLE> </HEAD> <BODY> <H 4>회원 정보를 입력하세요. </H 4> <FORM ACTION=Subscription. jsp METHOD=POST> 이름: <INPUT TYPE=TEXT NAME=name SIZE=10> <BR> 아이디: <INPUT TYPE=TEXT NAME=id SIZE=8> <BR> 패스워드: <INPUT TYPE=PASSWORD NAME=password SIZE=8> <BR> <INPUT TYPE=SUBMIT VALUE= ‘확인 ’> <INPUT TYPE=RESET VALUE= ‘취소 ’> </FORM> </BODY> </HTML> 52/85

4. 웹 컴포넌트에서 데이터베이스를 사용하는 방법 v 데이터베이스에 데이터를 입력하는 방법 [예제 12 -6]

4. 웹 컴포넌트에서 데이터베이스를 사용하는 방법 v 데이터베이스에 데이터를 입력하는 방법 [예제 12 -6] 회원 정보를 데이터베이스에 입력하는 JSP 페이지 <%@page content. Type= “text/html; charset=euc-kr” error. Page= “DBError. jsp” %> <%@page import= “java. sql. * ”%> <% String name = request. get. Parameter( “name” ); String id = request. get. Parameter( “id ”); String password = request. get. Parameter( “password ”); if (name == null || id == null || password == null) throw new Exception( “데이터를 입력하세요. ”); Connection conn = null; Statement stmt = null; try { Class. for. Name( “com. mysql. jdbc. Driver ”); conn = Driver. Manager. get. Connection( “jdbc: mysql: //localhost: 3306/webdb ”, “root ”, “ 1234 ”); if (conn == null) throw new Exception( “데이터베이스에 연결할 수 없습니다. ”); stmt = conn. create. Statement(); String command = String. format( “insert into userinfo (name, id, password) values ( ‘%s ’, ‘%s ’); ”, name, id, password); int row. Num = stmt. execute. Update(command); if (row. Num < 1) throw new Exception( “데이터를 DB에 입력할 수 없습니다. ”); } finally { try { stmt. close(); } catch (Exception ignored) { } try { conn. close(); } catch (Exception ignored) { } } response. send. Redirect( “Subscription. Result. jsp ”); %> 53/85

4. 웹 컴포넌트에서 데이터베이스를 사용하는 방법 v 데이터베이스의 데이터를 수정하고 삭제하는 방법 § 앞

4. 웹 컴포넌트에서 데이터베이스를 사용하는 방법 v 데이터베이스의 데이터를 수정하고 삭제하는 방법 § 앞 애플리케이션을 다음과 같은 5개의 모듈로 구성하기로 하자. http: //localhost: 8080/brain 12/GIM/Init. Form. html http: //localhost: 8080/brain 12/GIM/Reader. jsp http: //localhost: 8080/brain 12/GIM/Edit. Form. jsp http: //localhost: 8080/brain 12/GIM/Updater. jsp http: //localhost: 8080/brain 12/GIM/Update. Result. jsp 상품코드 입력 화 면 HTML 문서의 상품 URL 정보 DB 읽 기 JSP 페이지의 상품 URL 정보 편집 화 면 JSP 페이지의 상품 URL 정보 DB 수 정 JSP 페이지의 상품 URL 정보 수정 결 과 JSP 페이지의 URL 58/85

4. 웹 컴포넌트에서 데이터베이스를 사용하는 방법 v 데이터베이스의 데이터를 수정하고 삭제하는 방법 [예제 12

4. 웹 컴포넌트에서 데이터베이스를 사용하는 방법 v 데이터베이스의 데이터를 수정하고 삭제하는 방법 [예제 12 -8] 상품코드를 입력받는 HTML 문서 <HTML> <HEAD> <META http-equiv= “Content-Type ” content= “text/html; charset=euc-kr ”> <TITLE>상품 정보 관리</TITLE> </HEAD> <BODY> <H 4>상품코드를 입력하세요. </H 4> <FORM ACTION=Reader. jsp METHOD=GET> 상품코드: <INPUT TYPE=TEXT NAME=code SIZE=5> <INPUT TYPE=SUBMIT VALUE= ‘확인 ’> </FORM> </BODY> </HTML> 59/85

4. 웹 컴포넌트에서 데이터베이스를 사용하는 방법 v 데이터베이스의 데이터를 수정하고 삭제하는 방법 [예제 12

4. 웹 컴포넌트에서 데이터베이스를 사용하는 방법 v 데이터베이스의 데이터를 수정하고 삭제하는 방법 [예제 12 -9] 상품 정보 테이블을 읽는 JSP 페이지 <%@page content. Type= “text/html; charset=euc-kr ” error. Page= “. . /DBError. jsp ” %> <%@page import= “java. sql. * ”%> <% String code = request. get. Parameter( “code ”); if (code == null) throw new Exception( “상품코드를 입력하세요. ”); Connection conn = null; Statement stmt = null; try { Class. for. Name( “com. mysql. jdbc. Driver ”); conn = Driver. Manager. get. Connection(“jdbc: mysql: //localhost: 3306/webdb ”, “root ”, “ 1234 ”); if (conn == null) throw new Exception( “데이터베이스에 연결할 수 없습니다. ”); stmt = conn. create. Statement(); Result. Set rs = stmt. execute. Query( “select * from goodsinfo where code = ‘” + code + “’; ”); if (!rs. next()) throw new Exception(“상품코드( ” + code + “)에 해당하는 데이터가 없습니다. ”); String title = rs. get. String( “title ”); String writer = rs. get. String( “writer ”); int price = rs. get. Int( “price ”); request. set. Attribute( “CODE ”, code); request. set. Attribute( “TITLE ”, to. Unicode(title)); request. set. Attribute( “WRITER ”, to. Unicode(writer)); request. set. Attribute( “PRICE ”, new Integer(price)); <%! } // ISO-8859 -1 문자열을 Unicode 문자열로 바꾸는 메서드 finally { private String to. Unicode(String str) { try { stmt. close(); byte[] b = str. get. Bytes( “ISO-8859 -1 ”); } return new String(b); catch (Exception ignored) { } } catch (java. io. Unsupported. Encoding. Exception uee) { try { System. out. println(uee. get. Message()); conn. close(); return null; } } catch (Exception ignored) { } } %> } Request. Dispatcher dispatcher = request. get. Request. Dispatcher( “Edit. Form. jsp ”); dispatcher. forward(request, response); %> 60/85

4. 웹 컴포넌트에서 데이터베이스를 사용하는 방법 v 데이터베이스의 데이터를 수정하고 삭제하는 방법 [예제 12

4. 웹 컴포넌트에서 데이터베이스를 사용하는 방법 v 데이터베이스의 데이터를 수정하고 삭제하는 방법 [예제 12 -10] 상품 정보 관리 화면을 제공하는 JSP 페이지 <%@page content. Type= “text/html; charset=euc-kr ” %> <HTML> <HEAD> <TITLE>상품 정보 관리</TITLE> </HEAD> <BODY> <H 4>상품 정보를 수정한 후 수정 버튼을 누르세요. </H 4> <FORM ACTION=Updater. jsp METHOD=POST> 코드: <INPUT TYPE=TEXT NAME=code SIZE=5 VALUE= ‘${CODE} ’ READONLY=TRUE> <BR> 제목: <INPUT TYPE=TEXT NAME=title SIZE=50 VALUE= ‘${TITLE} ’> <BR> 저자: <INPUT TYPE=TEXT NAME=writer SIZE=20 VALUE= ‘${WRITER} ’> <BR> 가격: <INPUT TYPE=TEXT NAME=price SIZE=8 VALUE= ‘${PRICE} ’>원 <BR> <INPUT TYPE=SUBMIT VALUE= ‘수정 ’> </FORM> </BODY> </HTML> 61/85

4. 웹 컴포넌트에서 데이터베이스를 사용하는 방법 v 데이터베이스의 데이터를 수정하고 삭제하는 방법 [예제 12

4. 웹 컴포넌트에서 데이터베이스를 사용하는 방법 v 데이터베이스의 데이터를 수정하고 삭제하는 방법 [예제 12 -11] 상품 정보를 수정하는 JSP 페이지 <%@page content. Type= “text/html; charset=euc-kr ” error. Page= “. . /DBError. jsp ” %> <%@page import= “java. sql. * ”%> <% reqeuest. set. Character. Encoding(“euc-kr”); String code = request. get. Parameter( “code ”); String title = request. get. Parameter( “title ”); String writer = request. get. Parameter( “writer ”); String price = request. get. Parameter( “price ”); if (code == null || title == null || writer == null || price == null) throw new Exception( “누락된 데이터가 있습니다. ”); Connection conn = null; Statement stmt = null; try { Class. for. Name( “com. mysql. jdbc. Driver ”); conn = Driver. Manager. get. Connection(“jdbc: mysql: //localhost: 3306/webdb ”, “root ”, “ 1234 ”); if (conn == null) throw new Exception( “데이터베이스에 연결할 수 없습니다. ”); stmt = conn. create. Statement(); String command = String. format( “update goodsinfo set title : = ‘%s ’, writer : = ‘%s ’, price : = %s where code = ‘%s ’; ”, title, writer, price, code); int row. Num = stmt. execute. Update(command); if (row. Num < 1) throw new Exception( “데이터를 DB에 입력할 수 없습니다. ” ); } finally { try { stmt. close(); } catch (Exception ignored) { } try { conn. close(); } catch (Exception ignored) { } } response. send. Redirect( “Update. Result. jsp? code= ” + code); %> 62/85

5. 데이터베이스 커넥션 풀의 설치와 사용 v 데이터베이스 커넥션 풀을 생성하고 등록하는 프로그램의 작성

5. 데이터베이스 커넥션 풀의 설치와 사용 v 데이터베이스 커넥션 풀을 생성하고 등록하는 프로그램의 작성 방법 § 앞에서 만든 두 객체가 함께 작동하도록 만들기 위해서는 다음과 같은 방법으로 org. apache. commons. dbcp 패키지에 속하는 Poolable. Connection. Factory 클래스의 객체를 만들어야 한다. new Poolable. Connection. Factory(connection. Factory, object. Pool, null, false, true); Driver. Manager. Connection. Factory 객체 Generic. Object. Pool 객체 § 그 다음에 할 일은 Pooling. Driver 객체를 생성해서 Generic. Object. Pool 객체를 웹 컨테이 너에 등록하는 것이다. Pooling. Driver driver = new Pooling. Driver(); Pooling. Driver 객체를 생성한다 driver. register. Pool( “/webdb_pool ”, object. Pool); 데이터베이스 커넥션 풀의 이름 Generic. Object. Pool 객체 74/85

5. 데이터베이스 커넥션 풀의 설치와 사용 v 데이터베이스 커넥션 풀을 생성하고 등록하는 프로그램의 작성

5. 데이터베이스 커넥션 풀의 설치와 사용 v 데이터베이스 커넥션 풀을 생성하고 등록하는 프로그램의 작성 방법 [예제 12 -13] 데이터베이스 커넥션 풀을 생성하고 등록하는 JSP 페이지 <%@page content. Type= “text/html; charset=euc-kr ”%> <%@page import= “org. apache. commons. dbcp. * ”%> <%@page import= “org. apache. commons. pool. impl. * ”%> <% Generic. Object. Pool object. Pool = new Generic. Object. Pool(); Driver. Manager. Connection. Factory connection. Factory = new Driver. Manager. Connection. Factory( “jdbc: mysql: //localhost: 3306/webdb ”, “root ”, “ 1234 ”); new Poolable. Connection. Factory(connection. Factory, object. Pool, null, false, true); Pooling. Driver driver = new Pooling. Driver(); driver. register. Pool( “/webdb_pool ”, object. Pool); %> <HTML> <HEAD><TITLE>데이터베이스 커넥션 풀 생성하기</TITLE></HEAD> <BODY> <H 3>데이터베이스 커넥션 풀 생성하기</H 3> 데이터베이스 커넥션 풀을 생성하고 등록했습니다. <BR> 풀 이름: /webdb_pool </BODY> </HTML> [그림 12 -45] 예제 12 -13의 실행 결과 75/85

5. 데이터베이스 커넥션 풀의 설치와 사용 v 데이터베이스 커넥션 풀을 생성하고 등록하는 프로그램의 작성

5. 데이터베이스 커넥션 풀의 설치와 사용 v 데이터베이스 커넥션 풀을 생성하고 등록하는 프로그램의 작성 방법 [예제 12 -15] 상품 정보 테이블을 읽는 JSP 페이지 - 데이터베이스 커넥션 풀 사용 <%@page content. Type= “text/html; charset=euc-kr ” error. Page= “DBError. jsp ” %> <%@page import= “java. sql. * ”%> <% String code = request. get. Parameter( “code ”); Connection conn = null; Statement stmt = null; try { Class. for. Name( “org. apache. commons. dbcp. Pooling. Driver ”); conn = Driver. Manager. get. Connection(“jdbc: apache: commons: dbcp: /webdb_pool ”); if (conn == null) throw new Exception( “데이터베이스에 연결할 수 없습니다. <BR> ”); stmt = conn. create. Statement(); Result. Set rs = stmt. execute. Query( “select * from goodsinfo where code = ‘” + code + “’; ”); if (rs. next()) { String title = rs. get. String( “title ”); String writer = rs. get. String( “writer ”); int price = rs. get. Int( “price ”); request. set. Attribute( “CODE ”, code); <%! request. set. Attribute( “TITLE ”, to. Unicode(title)); request. set. Attribute( “WRITER ”, to. Unicode(writer)); // ISO-8859 -1 문자열을 Unicode 문자열로 바꾸는 메서드 request. set. Attribute( “PRICE ”, new Integer(price)); private String to. Unicode(String str) { } try { } byte[] b = str. get. Bytes( “ISO-8859 -1 ”); finally { return new String(b); try { } stmt. close(); catch (java. io. Unsupported. Encoding. Exception uee) { } System. out. println(uee. get. Message()); catch (Exception ignored) { return null; } try { } conn. close(); } } %> catch (Exception ignored) { } } Request. Dispatcher dispatcher = request. get. Request. Dispatcher( “Goods. Info. Viewer. jsp ”); dispatcher. forward(request, response); %> 77/85

5. 데이터베이스 커넥션 풀의 설치와 사용 v 데이터베이스 커넥션 풀을 생성하고 등록하는 프로그램의 작성

5. 데이터베이스 커넥션 풀의 설치와 사용 v 데이터베이스 커넥션 풀을 생성하고 등록하는 프로그램의 작성 방법 [예제 12 -16] jsp. Init 메서드 안에서 데이터베이스 커넥션 풀을 생성하고 등록하는 JSP 페이지 <%@page content. Type= “text/html; charset=euc-kr ”%> <%@page import= “org. apache. commons. dbcp. * ”%> <%@page import= “org. apache. commons. pool. impl. * ”%> <%! public void jsp. Init() { Generic. Object. Pool object. Pool = new Generic. Object. Pool(); Driver. Manager. Connection. Factory connection. Factory = new Driver. Manager. Connection. Factory( “jdbc: mysql: //localhost: 3306/webdb ”, “root ”, “ 1234 ”); new Poolable. Connection. Factory(connection. Factory, object. Pool, null, false, true); Pooling. Driver driver = new Pooling. Driver(); driver. register. Pool( “/webdb_pool ”, object. Pool); } %> 웹 컨테이너가 시작될 때 서블릿이 초기화되도록 만드 는값 [그림 12 -50] 웹 컨테이너가 시작될 때 JSP 페이지가 초기화되도록 만드는 방법 79/85

5. 데이터베이스 커넥션 풀의 설치와 사용 v JOCL 파일을 이용한 데이터베이스 커넥션 풀 생성

5. 데이터베이스 커넥션 풀의 설치와 사용 v JOCL 파일을 이용한 데이터베이스 커넥션 풀 생성 방법 [예제 12 -17] 데이터베이스 커넥션 풀 테스트하기 (2) <%@page content. Type= “text/html; charset=euc-kr ” error. Page= “DBError. jsp ” %> <%@page import= “java. sql. * ”%> <HTML> <HEAD><TITLE>데이터베이스 커넥션 풀 테스트</TITLE></HEAD> <BODY> <H 3>데이터베이스 커넥션 풀 테스트</H 3> <% Class. for. Name( “org. apache. commons. dbcp. Pooling. Driver ”); Connection conn = Driver. Manager. get. Connection(“jdbc: apache: commons: dbcp: /wdbpool ”); if (conn != null) { out. println( “연결 취득 완료<BR> ”); conn. close(); out. println( “연결 반환 완료<BR> ”); } else { out. println( “연결 취득 실패<BR> ”); } %> </BODY> </HTML> [그림 12 -54] 예제 12 -17의 실행 결과 83/85

5. 데이터베이스 커넥션 풀의 설치와 사용 v JOCL 파일을 이용한 데이터베이스 커넥션 풀 생성

5. 데이터베이스 커넥션 풀의 설치와 사용 v JOCL 파일을 이용한 데이터베이스 커넥션 풀 생성 방법 [예제 12 -18] 회원 정보를 입력하는 JSP 페이지 <%@page content. Type= “text/html; charset=euc-kr ” error. Page= “DBError. jsp ” %> <%@page import= “java. sql. * ”%> <% String name = request. get. Parameter( “name ”); String id = request. get. Parameter( “id ”); String password = request. get. Parameter( “password ”); if (name == null || id == null || password == null) throw new Exception( “데이터를 입력하세요. ”); Connection conn = null; Statement stmt = null; try { Class. for. Name( “org. apache. commons. dbcp. Pooling. Driver ”); conn = Driver. Manager. get. Connection(“jdbc: apache: commons: dbcp: /wdbpool ”); if (conn == null) throw new Exception( “데이터베이스에 연결할 수 없습니다. ”); stmt = conn. create. Statement(); String command = String. format( “insert into userinfo ” + “(name, id, password) values ( ‘%s ’, ‘%s ’); ”, name, id, password); int row. Num = stmt. execute. Update(command); if (row. Num < 1) throw new Exception( “데이터를 DB에 입력할 수 없습니다. ”); } finally { try { stmt. close(); } catch (Exception ignored) { } try { conn. close(); } catch (Exception ignored) { } } response. send. Redirect( “Subscription. Result. jsp ”); %> 84/85