데이터베이스 사용하기
DESCRIPTION
10. 데이터베이스 사용하기. 학습 목표 데이터베이스를 이용하면 파일보다 훨씬 더 체계적이고 구조적인 방법으로 데이터를 저장하고 관리할 수 있다 . 그래서 웹 프로그래밍에서도 데이터베이스를 사용해야 할 경우가 많이 있는데 이번 장에서는 그 방법을 배워보자 . 내 용 데이터베이스에 대하여 MySQL 설치하기 Connector/J 설치하기 웹 컴포넌트에서 데이터베이스를 사용하는 방법 데이터베이스 커넥션 풀의 설치와 사용. 1. 데이터베이스 사용. 데이터베이스 ( database ) - PowerPoint PPT PresentationTRANSCRIPT
JSP & Servlet
10 JSP & Servlet#/90 . .
MySQL Connector/J #/80Contents#/90(database) , , MySQL MySQL JDBC .1.
[ 12-1] JDBC #/90MySQL http://dev.mysql.com/downloads/ URL .2. MySQL
#/90MySQL MySQL 5.1 .2. MySQL
#/90MySQL Download .2. MySQL
#/90MySQL Windows Windows MySQL .2. MySQL
#/90MySQL Pick a mirror .2. MySQL
#/90MySQL No Thanks MySQL .2. MySQL
#/90MySQL HTTP FTP , .2. MySQL
#/90MySQL , Next .2. MySQL
#/90MySQL Typical Next , Install .2. MySQL
#/90MySQL , Next . Finish MySQL .2. MySQL
#/90MySQL MySQL , Next .2. MySQL
#/90MySQL Next , MySQL .2. MySQL
#/90MySQL Next , root Next .
[] .2. MySQL
#/90MySQL Execute , .
Finish MySQL .2. MySQL
#/90MySQL .
MySQL . MySQL MySQL .2. MySQL
[ 12-9] MySQL#/90MySQL MySQL , , , 4 MySQL .2. MySQL
[ 12-10] MySQL #/90 MySQL mysqladmin.exe create SQL 2. MySQL mysqladmin -u root -p create webdb IDroot webdb
[ 12-11] #/90 .
(Table) () 2. MySQL
[ 12-12] #/90 , , , .
2. MySQL
#/90 .
root , mysql> .mysql> use .
mysql> quit .2. MySQL mysql -u root -p IDroot use webdb #/90 2. MySQL [ 12-12]
1) mysql.exe .2) .3) use .4) quit mysql.exe .#/90 create .
, . not null 2. MySQL create table goodsinfo (code char(5), title varchar(50), writer varchar(20), price int(8)) ;create , , create table goodsinfo ( code char(5) not null, title varchar(50) not null, writer varchar(20), price int(8) not null); not null .#/90 create , primary key .
: . .2. MySQL create table goodsinfo ( code char(5) not null, title varchar(50) not null, writer varchar(20), price int(8) not null, primary key(code));code #/90 create .2. MySQL
[ 12-13] webdb create . .[ 12-14] #/90 desc 2. MySQL
[ 12-15] #/90 insert .
.2. MySQL insert into goodsinfo (code, title, writer, price) values ( 10001 , Java , , 27000);
[ 12-16] insert #/90 select
(*) .
where 2. MySQL select name, price from goodsinfo;select select * from goodsinfo; select * from goodsinfo where price > 20000; #/90 2. MySQL
[ 12-17] #/90 update 2. MySQL update goodsinfo set writer:= 3 , price:=33600 where code = 10005;update := where
[ 12-18] #/90 delete 2. MySQL delete from goodsinfo where code = 10005 ;delete where
[ 12-19] #/90JDBC : http://dev.mysql.com/downloads/3. Connector/J
#/90JDBC Connector/J .3. Connector/J
#/90JDBC Connector/J .3. Connector/J
#/90JDBC 3. Connector/J
#/904. JDBC JDBC(Java Database Connectivity) - API
DBMSMySql DBMS DBMSJDBC JDBC MySql JDBC JDBC #/904. JDBC JDBC DBMSDBMSDBMSDBMSDBMSNative-Protocol ODBC JDBC JDBC-ODBC JDBC APIJDBC API
Net-Protocol Native-API #/904. JDBC JDBC JDBC JDBC JDBCc:\dev\oraclexe\app\oracle\product\10.2.0\server\jdbc\lib\ojdbc14.jar ( )JDK\jre\lib\ext\ .\common\lib WebContent\WEB-INF\lib WebContent\WEB-INF\lib #/904.
JDBC (1)
#/904. JDBC (2)
#/90JDBC & 4.
#/90JDBC & JDBC .
, , URL .
DBMS MySQL DBMS .4. jdbc:mysql://219.153.12.14:3306/webdb(protocol)(subprotocol)(subname)IP:/DBClass.forName(com.mysql.jdbc.Driver);JDBC #/90JDBC & java.sql.DriverManager getConnection .
java.sql.Connection . Connection close .4. Connection conn = DriverManager.getConnection(jdbc:mysql://219.153.12.14:3306/webdb , root , 1234 ); URL IDconn.close() #/90JDBC & 4. [ 12-1] JDBC ,
[ 12-26] 12-1 #/90 Connection createStatement java.sql.Statement .
Statement executeQuery .4. Statement stmt = conn.createStatement();getConnection Connection Statement ResultSet rs = stmt.executeQuery( select * from goodsinfo where code=10002; );select #/90 executeQuery ResultSet next .
true, false .next ResultSet getInt, getString, getFloat .4. boolean exists = rs.next(); / String code = rs.getString( code ); int price = rs.getInt( price ); #/90 ResultSet close .
Statement close .4. rs.close();ResultSet stmt.close();Statement #/90 4. [ 12-2] JSP
#/90 4. [ 12-3] JSP
: ${CODE}
: ${TITLE}
: ${WRITER}
: ${PRICE}
[ 12-4] JSP
:
[ 12-27] 12-2 ~ 12-4 #/90 Statement .
Statement executeUpdate .
executeUpdate Statement close .4. Statement stmt = conn.createStatement();Statement int rowNum = stmt.executeUpdate( insert goodsinfo (code, title, writer, price) values(10001, Java , , 27000););insert #/90 4.
[ 12-13] webdb create mysql.exe [ 12-28] #/90 .4.
[ 12-29] DB . #/90 .
HTML JSP URL .4.
http://localhost:8080/brain12/SubscriptionForm.html HTML URLhttp://localhost:8080/brain12/Subscription.jsp DB JSP URLhttp://localhost:8080/brain12/SubscriptionResult.jsp JSP URL#/90 4. [ 12-5] HTML
. :
:
:
#/90 4. [ 12-6] JSP
#/90 4. [ 12-7] JSP
.
[ 12-31] 12-5 ~ 12-7 #/90 4. [ 12-32] 12-5 ~ 12-7
3) .1) webdb .2) select .#/90 executeUpdate update .
executeUpdate delete .4. int rowNum = stmt.executeUpdate( update userinfo set password :=dalek where id = rose ; ); update int rowNum = stmt.executeUpdate( delete from userinfo where id = rose ; ); delete .#/90 4.
[ 12-33] #/90 5 .4.
http://localhost:8080/brain12/GIM/InitForm.html HTML URLhttp://localhost:8080/brain12/GIM/Reader.jsp DB JSP URLhttp://localhost:8080/brain12/GIM/EditForm.jsp JSP URLhttp://localhost:8080/brain12/GIM/Updater.jsp DB JSP URLhttp://localhost:8080/brain12/GIM/UpdateResult.jsp JSP URL#/90 4. [ 12-8] HTML
. :
#/90 4. [ 12-9] JSP
#/90 4. [ 12-10] JSP
. :
:
:
:
#/90 4. [ 12-11] JSP
#/90 4. [ 12-12] JSP
. .
#/90 4.
#/90 , , . (Database Connection Pool) , .5.
#/90DBCP, Pool, Collections : http://www.apache.org/.5.
#/90DBCP, Pool, Collections Commons ..5.
#/90DBCP, Pool, Collections DBCP ..5.
#/90DBCP, Pool, Collections Pool ..5.
#/90DBCP, Pool, Collections Collections ..5.
#/90DBCP, Pool, Collections .5.
#/90 . Class.forName DriverManager, getConnection .5. [ 12-44]
#/90 org.apache.commons.pool.impl GenericObjectPool StackObjectPool
GenericObjectPool . DBCP org.apache.commons.dbcp DriverManagerConnectionFactory .5. GenericObjectPool objectPool = new GenericObjectPool();GenericObjectPool .DriverManagerConnectionFactory connectionFactory = new DriverManagerConnectionFactory( jdbc:mysql://localhost:3306/webdb , root , 1234 ); URL#/90 org.apache.commons.dbcp PoolableConnectionFactory .
PoolingDriver GenericObjectPool .5. new PoolableConnectionFactory(connectionFactory, objectPool, null, null, false, true);DriverManagerConnectionFactory GenericObjectPool PoolingDriver driver = new PoolingDriver();PoolingDriver driver.registerPool( /webdb_pool , objectPool); GenericObjectPool #/90 5. [ 12-13] JSP
.
: /webdb_pool
[ 12-45] 12-13 #/90 5. [ 12-14] ( 1 )
[ 12-46] 12-14 - [ 12-47] 12-14 - #/90 5. [ 12-15] JSP -
#/90 5. [ 12-48] 12-15 - [ 12-49] 12-15 -
#/90 5. [ 12-16] jspInit JSP
[ 12-50] JSP #/90JOCL JOCL DBCP .
DBCP .JOCL WEB-INF/classes .5.
[ 12-51] JOCL #/90JOCL 5.
#/90
JOCL JOCL JDBC , DBCP, Pool, Collections JOCL WEB-INF/lib ..5. #/90JOCL 5. [ 12-17] (2)
[ 12-54] 12-17 #/90JOCL 5. [ 12-18] JSP
#/905. JOCL [ 12-55] 12-15, 12-18, 12-7
#/90Thank You !JSP & Servlet#/90