CH 16 - Angelfire

thickbugΛογισμικό & κατασκευή λογ/κού

28 Οκτ 2013 (πριν από 3 χρόνια και 11 μήνες)

99 εμφανίσεις


© 2002 by Prentice Hall


1


David M. Kroenke


Database Processing

Eighth Edition


Chapter 16

JDBC, Java
Server Pages,
and MySQL


© 2002 by Prentice Hall


2

Open
-
Source Software


Each of the following are open
-
source
programs that will run in open
environments (e.g., Unix and Linux)
and many proprietary systems (e.g.,
Windows):


JDBC (not an acronym)


Java Server Pages (JSP) using
Apache/Tomcat


MySQL (an open
-
source DBMS
developed by Mark Mathews)



© 2002 by Prentice Hall


3

JDBC


JDBC allows you to connect to many
different databases


For a listing of drivers see
http://java.sun.com/products/jdbc



The MySQL open
-
source driver
(developed by Mark Mathews) and
may be downloaded from:
http://worldserver.com/mm.mysql


© 2002 by Prentice Hall


4

JDBC Driver Types


Type 1


JDBC
-
ODBC bridge drivers


Type 2


Connects to the native API of the DBMS


Type 3


Translates JDBC into DBMS
-
independent network protocol


Type 4


Translates JDBC into DBMS
-
specific
network protocol


© 2002 by Prentice Hall


5

Machine
-
Independent
Bytecode


Java was designed to be portable.


Java is
not

compiled into machine
language


It is compiled into machine
-
independent bytecode.


Bytecode is interpreted by the
Java virtual machine (a program
that is needed to run Java
programs)


© 2002 by Prentice Hall


6

Java Performance


While bytecode provides portability,
since it must be interpreted, it is not
as fast as code that has been
translated into machine language


© 2002 by Prentice Hall


7

Java Applet


An
applet

is Java bytecode that is
designed to run on a client


Applet bytecode is sent to the client
machine using HTTP. It is read and
interpreted by the Web browser.
Most, if not all, Web browsers now
run the Java virtual machine


© 2002 by Prentice Hall


8

Java Servlet


A
servlet

is a Java program invoked
via HTTP on the Web server.


The servlet uses the Java virtual
machine running on the server.



© 2002 by Prentice Hall


9

JDBC Driver Types mappings

Applet

Servlet

Type 1

Use ODBC, single
-
tier
architectures only.

Use ODBC, single
-
tier
architectures only.

Type 2

Generally, only supports
single
-
tier architectures

Generally, only supports
single
-
tier architectures

Type 3

Yes, supports n
-
tier
architectures

Yes, supports n
-
tier
architectures

Type 4

Yes, supports n
-
tier
architectures (faster
than Type 3)

Yes, supports n
-
tier
architectures (faster
than Type 3)


© 2002 by Prentice Hall


10

Using JDBC


The connection string information for
JDBC is embedded in the Java code
via the JDBC driver.


To successfully connect the bytecode
must…


Load the driver


Establish a connection to the database


Create a statement


Processing the statement


© 2002 by Prentice Hall


11

Loading the Driver


The directory containing the driver
library must be part of the
CLASSPATH for the Java compiler
and the Java virtual machine.


While there are several ways to load
the drivers, the following is one of
the most reliable…


Class.forName(“
nameofdriver
”).newInstance();


© 2002 by Prentice Hall


12

Establishing a Connection to
the database


Create an object that has a
connection to the database…


Connection Conn =


DriverManager.getConnection(“connectionString”);


e.g., DriverManager.getConnection


(“jdbc:mysql://localhost/db1?
user=abc&password=pwd”)


© 2002 by Prentice Hall


13

Details Regarding the JDBC
Connection String

connectionString has three parts (each part
delimited with a colon)…


Part 1:
jdbc:


Part 2: a keyword that identifies the DBMS

e.g.,

mysql:


Part 3: a URL for the database

e.g.,
//localhost/db1?user=abc&password=pwd




© 2002 by Prentice Hall


14

Creating a Statement


A statement is created by
instantiating a new Statement object


Statement Stmt = Conn.createStatement();


© 2002 by Prentice Hall


15

Processing the Statement


The driver’s API documentation will
provide details on what statements
are available. However, the core
statements are standard.


Note:

a driver’s API is a set of
functions that the program may call
to receive services.


© 2002 by Prentice Hall


16

Processing the Statement

Examples

ResultSet RS = Stmt.executeQuery


(“select * from tab”);


int result = Stmt.executUpdate

(“update tab set name=‘abc’ where id
= 1”);



© 2002 by Prentice Hall


17

Java Server Pages (JSP)


Java Server Pages (JSP)

provide a
means of creating dynamic Web pages
using HTTP, XML, and Java.


JSP only allows for Java coding, ASP
(as you may recall) only allows for
JavaScript or VBScript coding.


© 2002 by Prentice Hall


18

JSP Inherits the Power and
Portability of Java


Since Java is powerful and platform
-
independent so is JSP.


JSP Specifications are located at:
http://java.sun.com/products/jsp



© 2002 by Prentice Hall


19

JSP and Servlets


JSP are transformed into standard
Java code, then they are compiled
into bytecode (just like a regular
Java program)


© 2002 by Prentice Hall


20

JSP

subclasses of
HttpServlet class


JSP are subclasses of the
HttpServlet class


Consequently, due to inheritance, you
may embed Java code enclosed by <%
and %> directly into JSP.



© 2002 by Prentice Hall


21

Web Server Applications


To run JSP, the Web server must
implement:


Java servlet 2.1+ and


Java Server Pages 1.0+


© 2002 by Prentice Hall


22

Apache Tomcat


A common Web server application
which includes these is Apache
Tomcat:


http://jakarta.apache.org


Tomcat may be run in conjunction
with Apache (in a production
environment) or standalone (in a
development environment)


© 2002 by Prentice Hall


23

MySQL


MySQL

is an open
-
source DBMS
product that runs on Unix, Linux, and
Windows.


While MySQL is extremely
inexpensive and provides many
capabilities, it is not as powerful as
commercial products, such as Oracle
and SQL Server.


© 2002 by Prentice Hall


24

MySQL


MySQL

provides quick and efficient
query handling.


The product allows you to create
users, databases, tables, auto
incrementing fields, etc.



© 2002 by Prentice Hall


25

JDBC Connections


In some situations (like when using
MySQL) the localhost host
designator may not work as intended.


The issue is rooted in how the DBMS
interpret the localhost parameter. If
it attempts to use sockets rather
than TCP/IP (as JDBC expects), the
connection will not be established.


© 2002 by Prentice Hall


26

JDBC localhost Workaround


A work
-
around allows you to grant
access to the database@’%’ for the
specific account


The wildcard % states that the
database may be accessed from any
host


Alternatively, you may supply the
specific IP address for your local
machine

this provides better
security


© 2002 by Prentice Hall


27


David M. Kroenke


Database Processing

Eighth Edition


Chapter 16

JDBC, Java
Server Pages,
and MySQL