Click here to load reader

11 장 데이터베이스와 JSP 의 연동

  • Upload
    kostya

  • View
    163

  • Download
    7

Embed Size (px)

DESCRIPTION

이장에서 배울 내용 : JSP 페이지와 데이터베이스와의 연동을 위한 데이터베이스 연결 기술인 JDBC 의 개념과 JSP 페이지에서 JDBC 를 사용하여 데이터베이스를 연동한 웹 어플리케이션을 작성을 학습한다 . 또한 DBCP API 를 사용한 커넥션 풀도 설정한다. 11 장 데이터베이스와 JSP 의 연동. 김은옥 ([email protected]). 데이터베이스의 개요 및 설치 이클립스에서 [Data Source Explorer] 뷰를 사용한 데이터베이스 직접 제어 - PowerPoint PPT Presentation

Citation preview

1

11 JSP ([email protected]) : JSP JDBC JSP JDBC . DBCP API . [Data Source Explorer] SQL(Structured Query Language) JDBC JSP DBCP API (connection pools) DBMS(Database Management System) , DBMS . : //, , , , . MySQL MySQL MySQL DBMShttp://dev.mysql.com/downloads/ MySQL MySQL http://dev.mysql.com/downloads/ MySQL MySQL MySQL MySQL JDBC DB , DBMS DB , [WebContent]-[WEB-INF]-[lib] MySQL MySQL MySQL (: basicjsp)C:\Program Files\MySQL\MySQL Server 5.5\bin>mysqladmin -u root -p create basicjsp MySQL MySQL (localhost)

(%)

grant select, insert, update, delete, create, drop, alteron basicjsp.* to 'jspid'@'localhost'identified by 'jsppass';grant select, insert, update, delete, create, drop, alteron basicjsp.* to 'jspid'@'%'identified by 'jsppass'; [Data Source Explorer] [Data Source Explorer] [Data Source Explorer] . [Data Source Explorer] [Database Connections] [New...] [New Connection Profile] , [Connection Profile Type] [MySQL] [Name] "mysqlconn" [Next] [Data Source Explorer] [Specify a Driver and Connection Details] , [Drivers] [New Driver Definition] [New Driver Definition] , [Name/Type], [JAR List], [Properties] [Finish] [Data Source Explorer] [Data Source Explorer] (scrapbook) [Data Source Explorer] [mysqlconn(MySQL v.5.5.30)] [Open SQL Scrapbook] [Data Source Explorer] [Connection profile] [Type] [MySql_5.1] , [Name] [mysqlconn] [Database] [basicjsp] SQL . [Execute Selected Text] [Alt] +[x]

SQL(Structured Query Language) SQL Structured Query Language . .SQL (Data Definition Language, DDL), , (Data Control Language), (Query), (Transaction) SQL(Structured Query Language) (DDL)- CREATE, ALTER, DROP (DCL) - GRANT, REVOKE (DML) - SELECT, INSERT, DELETE, (Query) - SELECT(Transaction) - COMMIT, ROLLBACKSQL(Structured Query Language) (Data Type) TINYINT1 byte-128~127UNSIGNED 0~255 SMALLINT2 bytes-32768~32767UNSIGNED 0~65535 MEDIUMINT3 bytes-8388608~8388607UNSIGNED 0~16777215 INT, INTEGER 4 bytes-2147483648~2147483647UNSIGNED 0~4294967295 BIGINT8 bytes-9223372036854775808~9223372036854775807UNSIGNED 18446744073709551615SQL(Structured Query Language) (Data Type) DATE3 bytesDATETIME8 bytesTIMESTAMP4 bytes ( )CHAR1~255VARCHAR1~255BLOB(Binary Large Object), TEXT 1~65535 MEDIUMBLOB, MEDIUMTEXT 1~1677215LONGBLOB, LONGTEXT 1~4294967295SQL(Structured Query Language) - CREATE

- DROP

CREATE TABLE table_name (col_name1 type [PRIMARY KEY] [NOT NULL/NULL],col_name2 type,....col_name3 type ); DROP TABLE table_name;SQL(Structured Query Language) - Insert

- SELECT

INSERT INTO table_name (col_name1,col_name2...) VALUES (col_value1, col_value2...)SELECT col_name1,col_name2 FROM table_name;SQL(Structured Query Language) - UPDATE

- DELETE

DELETE FROM table_nameWHERE condition;UPDATE table_name SET col_name = value,.....WHERE condition;JDBC JSP JSP , JDBC(Java Database Connectivity) JDBC(Java Database Connectivity) (JSP) (, ...) JDBC java.sql JDBC JSP JDBC JSP JDBC 1(JDBC Load) : (interface driver) (implements) , Class forName() .MySQL Class.forName ("com.mysql.jdbc.Driver");Oracle 10g 11g thin Class.forName ("oracle.jdbc.driver.OracleDriver");

JDBC JSP 2(Connection ) : Connection DriverManager getConnection(String url) .MySQL Connection Connection conn= DriverManage.getConnection("jdbc:mysql://localhost:3306/basicjsp","jspid","jsppass");Oracle Connection Connection conn= DriverManager.getConnection ("jdbc:oracle:thin:@localhost:1521:orcl", "scott", "tiger");JDBC JSP 3(Statement/PrepardStatement/CallableStatement ) : sql , . 3 JDBC .Statement stmt = con.createStatement();

JDBC JSP 4(Query ) : Statement/PrepardStatement/CallableStatement , executeQuery() executeUpdate() stmt.executeQuery() :recordSet => Select

stmt.executeUpdate(): row => Insert , Update , Delete

ResultSet rs = stmt.executeQuery ("select * from ");String sql="update member set passwd='3579' where id='abc'";stmt.executeUpdate(sql);JDBC JSP 5(ResultSet ) : executeQuery() ResultSet . ResultSet . ResultSet next() getXxx() . rs.getString("name") rs.getString(1) . ResultSet 1 .rs.getString("name") . JDBC JSP JDBC DriverManager : Class.forName() Class.forName("com.mysql.jdbc.Driver") "com.mysql.jdbc.Driver" JDBC JSP Connection : , Connection . DriverManger getConnection() , Connection conn

Connection conn = DriverManger.getConnection("jdbc:mysql://localhost:3306/basicjsp","jspid","jsppass"); JDBC JSP Statement JDBC JSP PreparedStatement Connection prepareStatement() prepareStatement() SQLException , try { PreparedStatement pstmt=conn.prepareStatement(sql);}catch(SQLException e) { e.printStackTrace();}JDBC JSP PreparedStatement (placeholder) SQL . (?) try { String sql= "insert into member values (?,?,?,?)"; PreparedStatement pstmt=conn.prepareStatement(sql); pstmt.setString(1,id); pstmt.setString(2,passwd); ...}catch(SQLException e) { e.printStackTrace();}JDBC JSP PreparedStatement PreparedStatement SQL setXxx() . Xxx . setString() , int setInt() . setXxx(num, var) . num: (?) . (?) 1, (?) 1 . var: . .JDBC JSP CallableStatement Connection prepareCall() . prepareCall() SQLException , . (Stored Procedure)

try { CallableStatement cstmt= connection. prepareCall()}catch(SQLException e) { e.printStackTrace();}JDBC JSP ResultSet Select executeQuery() , , ResultSet . ResultSet (, ) .

JDBC JSP ResultSet ResultSet (cursor)' , ResultSet . () ,

JDBC JSP ResultSet ResultSet getXxx() . Xxx . getString() , int getInt() . DBCP API (connection pools) Connection . , , . (connection pools) . DBCP API (connection pools) DBCP API DBCP API jar DBCP - server.xml JNDI - web.xml JSP DBCP API (connection pools) DBCP API jar commons-collections-3.2.1.jar, commons-dbcp-1.4.jar , commons-pool-1.6.jar \lib []-[WebContent]-[WEB-INF]-[lib] . JDBC mysql-connector-java-5.1.23-bin.jar \common\lib DBCP API (connection pools) DBCP - server.xml \conf server.xml [Servers]-[Tomcat v7.0 Server ~] server.xml .

DBCP API (connection pools) DBCP - server.xml

DBCP API (connection pools) JNDI - web.xml server.xml JNDI web.xml basicjsp db jdbc/basicjsp javax.sql.DataSource Container DBCP API (connection pools) JSP InitialContex Context initCtx = new InitialContext(); (Context) initCtx.lookup("java:comp/env") lookup() Context envCtx = (Context) initCtx.lookup("java:comp/env"); DBCP API (connection pools) JSP "java:comp/env" Context (DataSource)envCtx.lookup("jdbc/basicjsp"); "jdbc/basicjsp" DataSource .DataSource ds = (DataSource)envCtx.lookup("jdbc/basicjsp"); ds.getConnection() .Connection conn = ds.getConnection();