1 / 27

제 7 장 데이터베이스

제 7 장 데이터베이스. 장안대학교 인터넷정보통신과. 데이터베이스. 데이터베이스 데이터를 조직적으로 통합해 구조화시켜 놓은 데이터의 집합체 데이터베이스 관리 시스템 (DBMS) 데이터베이스를 생성하고 검색하며 , 추가 , 삭제를 원활히 하려는 프로그램의 집합 특징 데이터 구조 정의 데이터 검색과 갱신 데이터 추가와 삭제 복수의 사용자로부터의 데이터 처리 동시 실행 제어 정보보호. 관계형 데이터베이스. MySQL

niel
Download Presentation

제 7 장 데이터베이스

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 제7장 데이터베이스 장안대학교인터넷정보통신과

  2. JSP Programming with a Workbook 데이터베이스 • 데이터베이스 • 데이터를 조직적으로 통합해 구조화시켜 놓은 데이터의 집합체 • 데이터베이스 관리 시스템 (DBMS) • 데이터베이스를 생성하고 검색하며, 추가, 삭제를 원활히 하려는 프로그램의 집합 • 특징 • 데이터 구조 정의 • 데이터 검색과 갱신 • 데이터 추가와 삭제 • 복수의 사용자로부터의 데이터 처리 동시 실행 제어 • 정보보호

  3. 관계형데이터베이스 • MySQL • 1998년 MySQL AB사의 David Axmark, Allan Larsson에 의해 설립 • GNUGPL (General Public License) 개방형 소스 시스템. • 썬 마이크로시스템즈에2008년 인수 썬 마이크로시스템즈가오라클에 인수 • 현재 비즈니스용을 위한 MySQL네트웍 가입 등 두 개의 라이선스 적용 • http://www.mysqlkorea.co.kr/ • 168.126.146.37 • MS-SQL • 마이크로소프트 가 1993년 사이베이스(Sybase)를 기반으로 개발한 관계형 데이터베이스 • 168.126.146.33 • Oracle • 1979년 미국의 오라클(ORACLE)사의 관계형 데이터베이스 관리 시스템 - 최초의 상용 데이터베이스 • 현재 유닉스 환경에서 가장 널리 사용되는 RDBMS • 168.126.146.42

  4. JDBC(Java DataBaseConectivity)-206p • 자바 응용 프로그램에서 데이터베이스에 접속할 수 있도록 하는 자바 API. • 데이터베이스에서 자료를 쿼리하거나 업데이트하는 방법을 제공.

  5. JDBC(Java DataBaseConectivity)-206p Oracle JDBC 설치 http://www.mysql.com/downloads/connector/j/ http://www.oracle.com/downloads/

  6. deptNo deptname floor 1 영업 8 2 기획 10 3 개발 9 4 총부 7 5 연구 8 SQL 데이터 조작어202-205 p Select deptname, floor fromdept WheredeptNo=3 Updatedept Setfloor=10 WheredeptNo=1 Delete fromdept WheredeptNo=4 Insert intodept Values(5, ’연구’,9) dept

  7. 테이블 구조 sabun name title address phone deptNo 2106 김창섭 대리 수원시 123-2457 2 3426 박영권 과장 과천시 435-5432 1 3011 이수민 부장 서울시 546-7653 3 1003 조민희 과장 군포시 657-9874 2 3427 최종철 사원 안양시 334-7789 3 1366 김상원 사원 성남시 867-1243 1 4377 이성래 이사 서울시 636-7657 2 열(Column), Field, Attribute Primary key … Sawon 테이블 명 • • • Row, Tuple , Record • • •

  8. SQL 기본(select 연산자 ) • ORDER BY • 질의: 영업부 사원의 모든 정보를 급여가 높은 낮은 순으로 검색하라 • select * • from sawon • Where dept = ‘영업’ • Order by pay asc

  9. JDBC 프로그래밍 절차-214p

  10. JDBC 프로그래밍 절차-214p • JDBC 드라이버로드 • Class.forName("oracle.jdbc.driver.OracleDriver") • JDBC 프로그래밍을 위해서 3개의 클래스가 제공된다. • 데이터베이스 연결 : Connection • SQL 명령처리 : Statement, PreparedStatement • 실행 결과처리 : ResultSet • 생성된인스턴스(객체)는 반드시 변수(인스턴스 변수)에 넣어 처리한다. Connection Conn=null; Statement stmt = null; ResultSetrs = null; Conn=DriverManager.getConnection("jdbc:oracle:thin:@168.126.146.33:1521:orcl","jjin","jjinpang"); stmt = Conn.createStatement(); rs=stmt.executeQuery("select * from test”); Connection Conn = DriverManager.getConnection ("jdbc:oracle:thin:@168.126.146.33:1521:orcl","jjin","jjinpang"); Statement stmt = Conn.createStatement(); ResultSetrs = stmt.executeQuery("select * from test”);

  11. Try ~ catch ~ finally try { DB 관련처리 명령 ------- } catch( SQLExceptione ) { out.println( e ); } catch( Exception e1) { out.println( e1 ); } finally { rs.close(); //결과집합 제거(반환) stmt.close(); //명령객체 제거(반환) Conn.close(); //DB 연결해제 }

  12. JDBC 프로그래밍 절차(읽기) -214p • try • { • Class.forName("oracle.jdbc.driver.OracleDriver"); //JDBC 드라이버로드 • Connection Conn=DriverManager.getConnection("jdbc:oracle:thin:@168.126.146.33:1521:orcl","jjin","jjinpang"); //연결 • Statement stmt = Conn.createStatement(); //연결된 DB에 명령객체 생성 • ResultSetrs = stmt.executeQuery("select * from test”); //명령을 실행하여 결과를 rs에 저장 • while(rs.next()) //결과집합 처리 • { • String str1= rs.getString(0); • String str2 = rs.getString(1); • } • rs.close(); //결과집합 제거(반환) • stmt.close(); //명령객체 제거(반환) • Conn.close(); //DB 연결해제 • } • catch(SQLException e) { out.println(e);} • catch(Exception e1) {out.println(e1);} DB 서버 Insert , delete, update Web 서버 select

  13. JDBC 프로그래밍 형식의 다른 예

  14. JDBC 프로그래밍 절차(쓰기)jsp-214p Table:test <% request.setCharacterEncoding("utf-8"); %> <% String name = request.getParameter("name"); String pass = request.getParameter("pass"); Connection Conn=null; try { Class.forName("oracle.jdbc.driver.OracleDriver"); Conn=DriverManager.getConnection("jdbc:oracle:thin:@168.126.146.33:1521:orcl","jjin","jjinpang"); String strSQL ="INSERT INTO test(id, pwd) VALUES (?, ?)"; PreparedStatementpstmt = Conn.prepareStatement(strSQL); pstmt.setString(1, name); pstmt.setString(2, pass); pstmt.executeUpdate(); pstmt.close(); Conn.close(); response.sendRedirect("listest.jsp"); } catch(SQLException e) { out.println(e);} catch(Exception e1) {out.println(e1);} %> DB 서버 Insert , delete, update Web 서버 select

  15. 명령 객체의 생성과쿼리 실행 방법 //Statement 명령 객체 생성 JDBC 드라이버 로드 ① ② 연결(Connection) 객체생성 * 실행시Cname변수값이 결정되면 SQL 문이만들어져 분석되고 실행된다. 명령(Statement) 객체생성 ③ //PreparedStatement명령 객체 생성 ④ Query 실행 ResultSet 객체에서데이터 추출 및 처리 ⑤ • 명령객체를 만들때SQL 문이 미리 컴파일되고실행시cusName변수의 값이 전달되어 SQL문이 실행 • pstmt.setString(1, cusName) 명령으로 ?에 대신하는변수 설정 ResultSet객체 Close Statement 객체 close Connection 객체 close

  16. 데이터 베이스 활용기본 예제 Request.getparameter() DB_Save.jsp insert login.html DB_List.jsp select ResultSet

  17. 테이블생성 CREATE TABLE `test` ( `id` varchar2(20) NOT NULL , `pwd` varchar2(20) NOT NULL , PRIMARY KEY (`id`) ) * Sample 데이터 3개입력

  18. Oracle 데이터베이스 관리 시작

  19. oracle 테이블 생성

  20. Oracle sample 데이터 입력

  21. <% • while(rs.next()) • { • %> • <tr> • <td> <%=rs.getString(1)%> </td> • <td> <%=rs.getString(2)%> </td> • </tr> • <% • } //end of while • }//end of try • catch(SQLException e) • { • out.println(e); • } • catch(Exception e1) • { • out.println(e1); • } • finally • { • rs.close(); //결과집합 제거(반환) • stmt.close(); //명령객체 제거(반환) • Conn.close(); //DB 연결해제 • } • %> • </TABLE> • </body> • </HTML> DB_List.jsp <%@ page language="java” contentType= "text/html;charset=UTF-8" pageEncoding="UTF-8"%> <%@ page import="java.sql.*" %> <HTML> <HEAD> <TITLE> 데이터베이스 테이블 읽기</TITLE> </HEAD> <body> <% //Connection객체, 명령객체, 결과집합을 가지고 오는 변수 선언 Connection Conn=null; Statement stmt=null; ResultSetrs =null; String sql="select * from test"; // test 테이블에서모든 내용을 가져오는 쿼리 생성 및 저장 try { Class.forName("oracle.jdbc.driver.OracleDriver"); //oracle jdbc드라이버의 로드 Conn =DriverManager.getConnection("jdbc:oracle:thin:@168.126.146.33:1521:orcl",”학번",”주민번호"); stmt = Conn.createStatement(); rs = stmt.executeQuery(sql); //sql 쿼리(질의) 실행 %> <TABLE border="1" cellspacing ="0" > <TR> <TD>아이디</TD> <TD>패스워드</TD> </TR> 아이디 아이디 패스워드 패스워드

  22. login.jsp <HEAD> <HTML> <head><TITLE> ID,패스워드스입력폼저장 폼</TITLE> </head> <body> <form method="post" action="DB_Save.jsp"> <table border="1" cellspacing="0" bordercolor="blue"> <tr> <td><p align="center">아이디</p></td> <td><input type="text" name="strID" ></td> </tr> <tr> <td><p align="center">비밀번호</td> <td><input type="text" name="strPWD" ></td> </tr> <tr> <td colspan="2" > <p align="center">&nbsp; <input type="submit" name="sam" value="완료"> <input type="reset" name="hoo" value="취소"></p></td> </tr> </table> </form> </body> </html> DB_Save.jsp login.html DB_List.jsp

  23. DB_Save.jsp catch(SQLException e) { out.println(e); } catch(Exception e1) { out.println(e1); } finally { pstmt.close(); Conn.close(); } %> <%@ page language="java" contentType= "text/html;charset = utf-8"%> <%@ page import="java.sql.*, java.lang.*, java.util.* " %> <% request.setCharacterEncoding("utf-8"); %> <% //아이디,암호 읽기 String strid=request.getParameter(“strID"); String strpwd=request.getParameter(“strPWD"); Connection Conn=null; PreparedStatementpstmt=null; String sql="insert into test (id,pwd) values (?,?)"; try { Class.forName("oracle.jdbc.driver.OracleDriver"); //oracle jdbc드라이버의 로드 Conn=DriverManager.getConnection("jdbc:oracle:thin:@168.126.146.33:1521:orcl","jjin","jjinpang"); pstmt = Conn.prepareStatement(sql); //파라메터 설정 pstmt.setString(1,strid); pstmt.setString(2,strpwd); pstmt.executeUpdate(); response.sendRedirect("DB_List.jsp"); } Request.getparameter() DB_Save.jsp insert Login.html DB_list.jsp select

  24. 게시판 구조도 글쓰기 (write_form.html) 글보기 (write_view.jsp) 글답변 수정 삭제 비밀번호입력 (mpass_form.jsp) 답변쓰기 (reply_form.jsp) 비밀번호입력 (dpass_form.jsp) 글저장 (write_save.jsp) No No 비밀번호확인 (mpass_ok.jsp) 확인및 글삭제 (dpass_del.jsp) 답변글저장 (reply_save.jsp) Ok 게시판목록 게시판목록 글수정(modify_form.jsp)) 게시판목록 수정글 저장 (modify_save.jsp) 게시판목록

  25. 데이터베이스 설계-232p CREATE TABLE bbs+학번( num NUMBER NOT NULL, title varchar2(50) NOT NULL, name varchar2(20) NOT NULL, wdate varchar2(10) NOT NULL, cnt NUMBER NOT NULL, contents varchar2(2000) NOT NULL, pass varchar2(10) DEFAULT NULL, email varchar2(30) DEFAULT NULL, refgrp NUMBER NOT NULL, refseq NUMBER NOT NULL, PRIMARY KEY (num) )

  26. Oracle 항목자동증가 설정방법 • Sequence 작성 • CREATE SEQUENCE bbs_seqINCREMENT BY 1 START WITH 1; • DROP SEQUENCE bbs_seq • Trigger 작성 • Table에서 활성화

  27. Tablesql

More Related