1. Trang chủ
  2. » Công Nghệ Thông Tin

OReilly java database best practices may 2003 ISBN 0596005229

448 65 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 448
Dung lượng 2,83 MB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

container provider, EJB container-managed persistence model [See CMP model] container-managed relationships [See CMR] contains method JDO content generation layer, distributed archite

Trang 1

[ SYMBOL ] [A] [ B ] [ C ] [ D ] [ E ] [ F ] [ G ] [ H ] [ I ] [ J ] [ K ] [ L ] [ M ] [ N ] [ O ] [ P ] [ Q ] [ R ] [ S ] [ T ] [ U ] [ V ] [ W ] [ X ]

Trang 2

Brought to You by

Trang 3

Like the book? Buy it!

Trang 4

[SYMBOL] [ A ] [ B ] [ C ] [ D ] [ E ] [ F ] [ G ] [ H ] [ I ] [ J ] [ K ] [ L ] [ M ] [ N ] [ O ] [ P ] [ Q ] [ R ] [ S ] [ T ] [ U ] [ V ] [ W ] [ X ]

Trang 5

[ SYMBOL ] [ A ] [B] [ C ] [ D ] [ E ] [ F ] [ G ] [ H ] [ I ] [ J ] [ K ] [ L ] [ M ] [ N ] [ O ] [ P ] [ Q ] [ R ] [ S ] [ T ] [ U ] [ V ] [ W ] [ X ]

Trang 6

[ SYMBOL ] [ A ] [ B ] [C] [ D ] [ E ] [ F ] [ G ] [ H ] [ I ] [ J ] [ K ] [ L ] [ M ] [ N ] [ O ] [ P ] [ Q ] [ R ] [ S ] [ T ] [ U ] [ V ] [ W ] [ X ]

Trang 7

container provider, EJB

container-managed persistence model [See CMP model] container-managed relationships [See CMR]

contains( ) method (JDO)

content generation layer, distributed architectures content management layer, distributed architectures control logic

as part of application logic

for Guest Book application

conventions used in this book

CORBA (Common Object Request Broker Architecture) CREATE DATABASE statement (SQL)

Trang 8

[ SYMBOL ] [ A ] [ B ] [ C ] [D] [ E ] [ F ] [ G ] [ H ] [ I ] [ J ] [ K ] [ L ] [ M ] [ N ] [ O ] [ P ] [ Q ] [ R ] [ S ] [ T ] [ U ] [ V ] [ W ] [ X ]

Trang 10

[ SYMBOL ] [ A ] [ B ] [ C ] [ D ] [E] [ F ] [ G ] [ H ] [ I ] [ J ] [ K ] [ L ] [ M ] [ N ] [ O ] [ P ] [ Q ] [ R ] [ S ] [ T ] [ U ] [ V ] [ W ] [ X ]

Trang 11

executeWithArray( ) method (JDO) executeWithMap( ) method (JDO) 2nd EXPLAIN command (SQL)

Extent class (JDO)

extents, JDO

[ Team LiB ]

Trang 12

[ SYMBOL ] [ A ] [ B ] [ C ] [ D ] [ E ] [F] [ G ] [ H ] [ I ] [ J ] [ K ] [ L ] [ M ] [ N ] [ O ] [ P ] [ Q ] [ R ] [ S ] [ T ] [ U ] [ V ] [ W ] [ X ]

Trang 13

[ SYMBOL ] [ A ] [ B ] [ C ] [ D ] [ E ] [ F ] [G] [ H ] [ I ] [ J ] [ K ] [ L ] [ M ] [ N ] [ O ] [ P ] [ Q ] [ R ] [ S ] [ T ] [ U ] [ V ] [ W ] [ X ]

Trang 14

[ SYMBOL ] [ A ] [ B ] [ C ] [ D ] [ E ] [ F ] [ G ] [H] [ I ] [ J ] [ K ] [ L ] [ M ] [ N ] [ O ] [ P ] [ Q ] [ R ] [ S ] [ T ] [ U ] [ V ] [ W ] [ X ]

Trang 15

[ SYMBOL ] [ A ] [ B ] [ C ] [ D ] [ E ] [ F ] [ G ] [ H ] [I] [ J ] [ K ] [ L ] [ M ] [ N ] [ O ] [ P ] [ Q ] [ R ] [ S ] [ T ] [ U ] [ V ] [ W ] [ X ]

Trang 16

[ SYMBOL ] [ A ] [ B ] [ C ] [ D ] [ E ] [ F ] [ G ] [ H ] [ I ] [J] [ K ] [ L ] [ M ] [ N ] [ O ] [ P ] [ Q ] [ R ] [ S ] [ T ] [ U ] [ V ] [ W ] [ X ]

Trang 17

managed and nonmanaged environments metadata for business object

Trang 18

[ SYMBOL ] [ A ] [ B ] [ C ] [ D ] [ E ] [ F ] [ G ] [ H ] [ I ] [ J ] [K] [ L ] [ M ] [ N ] [ O ] [ P ] [ Q ] [ R ] [ S ] [ T ] [ U ] [ V ] [ W ] [ X ]

Kant, Immanuel (philosopher) 2nd

key generation [See sequence generation]

Kolb, David (philosopher) 2nd

[ Team LiB ]

Trang 19

[ SYMBOL ] [ A ] [ B ] [ C ] [ D ] [ E ] [ F ] [ G ] [ H ] [ I ] [ J ] [ K ] [L] [ M ] [ N ] [ O ] [ P ] [ Q ] [ R ] [ S ] [ T ] [ U ] [ V ] [ W ] [ X ]

Trang 20

[ SYMBOL ] [ A ] [ B ] [ C ] [ D ] [ E ] [ F ] [ G ] [ H ] [ I ] [ J ] [ K ] [ L ] [M] [ N ] [ O ] [ P ] [ Q ] [ R ] [ S ] [ T ] [ U ] [ V ] [ W ] [ X ]

Trang 21

[ SYMBOL ] [ A ] [ B ] [ C ] [ D ] [ E ] [ F ] [ G ] [ H ] [ I ] [ J ] [ K ] [ L ] [ M ] [N] [ O ] [ P ] [ Q ] [ R ] [ S ] [ T ] [ U ] [ V ] [ W ] [ X ]

Trang 22

[ SYMBOL ] [ A ] [ B ] [ C ] [ D ] [ E ] [ F ] [ G ] [ H ] [ I ] [ J ] [ K ] [ L ] [ M ] [ N ] [O] [ P ] [ Q ] [ R ] [ S ] [ T ] [ U ] [ V ] [ W ] [ X ]

Trang 23

[ SYMBOL ] [ A ] [ B ] [ C ] [ D ] [ E ] [ F ] [ G ] [ H ] [ I ] [ J ] [ K ] [ L ] [ M ] [ N ] [ O ] [P] [ Q ] [ R ] [ S ] [ T ] [ U ] [ V ] [ W ] [ X ]

Trang 24

unique identifiers for [See sequence generation] properties file, for JDBC database connection provider, EJB

Trang 25

[ SYMBOL ] [ A ] [ B ] [ C ] [ D ] [ E ] [ F ] [ G ] [ H ] [ I ] [ J ] [ K ] [ L ] [ M ] [ N ] [ O ] [ P ] [Q] [ R ] [ S ] [ T ] [ U ] [ V ] [ W ] [ X ]

queries [See EJBQL OQL searches SQL]

Query class (JDO)

QueryResults class

[ Team LiB ]

Trang 26

[ SYMBOL ] [ A ] [ B ] [ C ] [ D ] [ E ] [ F ] [ G ] [ H ] [ I ] [ J ] [ K ] [ L ] [ M ] [ N ] [ O ] [ P ] [ Q ] [R] [ S ] [ T ] [ U ] [ V ] [ W ] [ X ]

Trang 27

ROUND( ) function (SQL) rows

[ Team LiB ]

Trang 28

[ SYMBOL ] [ A ] [ B ] [ C ] [ D ] [ E ] [ F ] [ G ] [ H ] [ I ] [ J ] [ K ] [ L ] [ M ] [ N ] [ O ] [ P ] [ Q ] [ R ] [S] [ T ] [ U ] [ V ] [ W ] [ X ]

Trang 30

[ SYMBOL ] [ A ] [ B ] [ C ] [ D ] [ E ] [ F ] [ G ] [ H ] [ I ] [ J ] [ K ] [ L ] [ M ] [ N ] [ O ] [ P ] [ Q ] [ R ] [ S ] [T] [ U ] [ V ] [ W ] [ X ]

Trang 31

[ Team LiB ]

Trang 32

[ SYMBOL ] [ A ] [ B ] [ C ] [ D ] [ E ] [ F ] [ G ] [ H ] [ I ] [ J ] [ K ] [ L ] [ M ] [ N ] [ O ] [ P ] [ Q ] [ R ] [ S ] [ T ] [U] [ V ] [ W ] [ X ]

Trang 33

[ SYMBOL ] [ A ] [ B ] [ C ] [ D ] [ E ] [ F ] [ G ] [ H ] [ I ] [ J ] [ K ] [ L ] [ M ] [ N ] [ O ] [ P ] [ Q ] [ R ] [ S ] [ T ] [ U ] [V] [ W ] [ X ]

Trang 34

[ SYMBOL ] [ A ] [ B ] [ C ] [ D ] [ E ] [ F ] [ G ] [ H ] [ I ] [ J ] [ K ] [ L ] [ M ] [ N ] [ O ] [ P ] [ Q ] [ R ] [ S ] [ T ] [ U ] [ V ] [W] [ X ]

Trang 35

[ SYMBOL ] [ A ] [ B ] [ C ] [ D ] [ E ] [ F ] [ G ] [ H ] [ I ] [ J ] [ K ] [ L ] [ M ] [ N ] [ O ] [ P ] [ Q ] [ R ] [ S ] [ T ] [ U ] [ V ] [ W ] [X]

Trang 36

At this point, you have the basics for connecting to a database

Example 11-1, however, did not really do much It showed youhow to make a database connection and perform simplistic

database connection:

Through the JDBC DataSource (as shown in Example 11-1)

Using the JDBC DriverManager

The data source method is the preferred approach to databaseconnectivity Data sources, however, tend to be usable only inapplication server contexts You should therefore understandboth forms of connectivity since you can rely on DriverManager

connectivity no matter what environment you are working in

Trang 37

DataSource ds = (DataSource)ctx.lookup("jdbc/dsn");Connection conn = ds.getConnection( );

Trang 39

is the class DriverManager It maintains a list of

implementations of the java.sql.Driver interface and

provides you with connections based on a JDBC URL that you

provide This JDBC URL comes in the form

Trang 40

which database engine you wish to connect to and provides the

DriverManager with enough information to make a connection

JDBC uses the word "driver" in multiple contexts When lowercase, a JDBC driver is the collection of classes that together implement the JDBC specification When uppercase, the Driver is the class that implements java.sql.Driver Finally, JDBC provides a DriverManager

database to connect to It is also common to specify a host andeven a port number in the subprotocol

Each driver's JDBC URL is different, and so I cannot say

anything more explicit that will tell you what the proper URL isfor your driver Your driver documentation, however, shouldhave easy-to-find documentation describing the exact form ofits JDBC URL Whatever the format of the URL, the primaryfunction of the URL is to uniquely identify the driver needed bythe application and pass that driver any information it needs tomake a connection to the proper database

Before you can use a URL to get a connection from the

DriverManager, you first need to register your Driver

implementation with the DriverManager You have two mainoptions for registering a Driver:

Specify the names of the Driver implementation classesyou want to register on the command line of your

Trang 41

a Connection by calling the getConnection( ) method in the

Trang 43

Java is a language based on the concept of portability To mostpeople, portability means that you do not write code that willrun on only one platform In the Java world, however,

portability means no proprietary dependenciesand that means

no database dependencies

I touched earlier on how the JDBC URL and Driver

implementation classes are driver-specific Because both valuesare simple strings, you can pass them as command-line

arguments or applet parameters Unfortunately, this approach ishardly elegant since it requires users to remember long

command lines or to pass authentication credentials as HTML to

an applet tag

You could, of course, prompt the user for this information

However, this approach demands that the user know a JDBCURL and driver name The elegant approach is the use of

properties files Java supports the concept of properties-basedapplication configuration through java.util.ResourceBundle

and its subclasses

Using a properties file, you can store all configuration

Trang 45

p.setProperty(prop, val);

}

Class.forName(bld.getString("driver")).newInstance( ); conn = DriverManager.getConnection(url, p);

The most basic element of communication over a Connection is

the Statement Your application encapsulates SQL queries into

Trang 46

}

The query in Example 11-5 retrieves every row from the table

test The JDBC code loops through those rows and displays the

values for each row's columns

Trang 47

SQL and Java do not match up the way they treat the absence of valuenull.

Specifically, any SQL value can be NULL In Java, however, only object types can

have null values After retrieving a value from a ResultSet , your Java

application needs to ask the ResultSet if the value retrieved is a driver

representation of NULL For example, a call to rs.getInt( ) might return 0 even

though the underlying database value for the column is NULL To find out if the

value is actually 0 or NULL , you should call rs.wasNull( )

Until the first call to next( ), the result set does not point toany row returned by the query The first call makes the resultset point to the first row Until the next call to next( ), anyoperations you perform on the result set act on that row

Subsequent calls to next( ) move the result set forward

either the number of the columnstarting with 1or the columnname You should, however, avoid retrieving columns by namebecause it is generally much slower than retrieving them bynumber

11.2.2.2 Scrollable result sets

By default, you are limited to simple forward navigation through

Trang 49

In order for the database to properly manage your transactions,your application needs to tell it what operations constitute a

" WHERE id = " + account.getId( ));

Trang 50

Rarely are transactions as simplistic as the previous one Agiven transaction can make numerous modifications that need

to occur together or not at all The classic example of such atransaction is a transfer of funds from your savings account toyour checking account This transaction includes the followingsteps:

1 Debit the savings account.

Trang 51

" WHERE id = " + achecking.getId( )); success = true;

Trang 52

11.2.3.2 Savepoints

As described earlier, transactions work well when you have avery straightforward beginning state with only one possibleconsistent end state In other words, the database starts withone set of values and should end up with another specific set ofvalues when the transaction completes Some situations,

however, allow for multiple possible consistent end states

dependent on the events that occur during the course of the

Trang 54

in the database

This example begs the question: why not simply commit afterthe first statement? One reason is performance You do not

need to release and reacquire any locks to execute the

transaction The other reason, however, would be in more

complex transactions in which some branches of logic after thesavepoint is set need to roll back completely while others need

to roll back only to the savepoint

To illustrate the kind of logic to which savepoints apply, consider

a game in which you toss marbles into a jar The goal of thegame is to end up with the most marbles in the jar You startthe game with no marbles and continue tossing marbles intothe jar until your fourth miss or you call it quits Your score isthe number of marbles in the jar after your fourth miss

The trick behind scoring, however, is that missing two in a rowreturns your marble count to its number after your first miss Ifyou miss three in a row, the jar is emptied and your final score

Trang 55

All JDBC method calls can throw SQLException or one of its

Trang 56

statements and stored procedures Prepared SQL provides anadvantage over the simple SQL statements you have covered sofar; a database can get the SQL ahead of time and create a

query plan while you are doing other application logic Your SQLshould therefore execute faster Furthermore, you have a

generic reference to the statement for later reuse instead ofrepeatedly re-creating similar SQL statements

Trang 57

database of what SQL you intend to be calling without providingany specific values For example, the SQL to update accountbalances looks like this as a prepared statement:

UPDATE account SET balance = ? WHERE id = ?

Instead of sending this SQL as an argument to executeUpdate(), you pass it to the connection when you create the statement.You then assign values to the two placeholders and finish bycalling executeUpdate( ) If you want to make further calls toupdate other accounts, you can reassign the statement newvalues and call executeUpdate( ) again

11.2.5.1 Prepared statements

The PreparedStatement interface extends the Statement

Trang 59

time

Before each execution of the prepared statement, you tell JDBCwhich values to use as input for that execution of the

statement In order to bind the input parameters,

PreparedStatement provides setter methodslike setFloat( )

and setInt( )that mirror the getter methods you saw in

ResultSet Just as the getter methods read results according tothe order in which you constructed your SQL, the setter

methods bind parameters from left to right in the order you

placed them in the prepared statement In the previous

example, I bound parameter 1 as a float to the account balancethat I retrieved from the account object The first ? was thusassociated with parameter 1

11.2.5.2 Stored procedures

While prepared statements enable you to access similar

database queries through a single PreparedStatement object,stored procedures attempt to take the "black box" concept fordatabase access one step further A stored procedure is builtinside the database before you run your application You accessthat stored procedure by name at runtime In other words, astored procedure is almost like a method you call in the

database Stored procedures have the following advantages:

Because the procedure is precompiled in the database formost database engines, it executes much faster than

dynamic SQL Even if your database does not compile thestored procedure before it runs, it will be precompiled forsubsequent runs just like prepared statements

Trang 61

instance to expect the second parameter as output of type

float Once this procedure is set up, you can bind the ID using

setInt( ) and then get the output using getFloat( )

Trang 62

EJB 2.0 is a significant, revolutionary improvement over theoriginal EJB specificationespecially when it comes to componentpersistence Persistence changes include a more solid CMP

architecture and a specialized EJB query language (EJBQL) forperforming bean searches Beyond persistence, EJB 2.0

provides for exciting features such as message-driven beans

5.3.1 Container-Managed Relationships

Under EJB 1.x, container-managed persistence meant the

container managed keeping all bean attributes in sync with thedata store This job became problematic for any attributes

representing relationships to other beans The general solutionwas to store a primary key for the relationship and let clientsworry about referencing the other bean

EJB 2.0 introduces a concept called container-managed

relationships, or CMR A CMR field is a bean attribute that is a

relationship to another bean CMR relationships can take any ofthe following forms:

instances but the Book bean has no reference to its author Ofcourse, it would be more appropriate for this particular

Ngày đăng: 26/03/2019, 17:10

TỪ KHÓA LIÊN QUAN

TÀI LIỆU CÙNG NGƯỜI DÙNG

  • Đang cập nhật ...

TÀI LIỆU LIÊN QUAN