Sie sind auf Seite 1von 5

Connecting to a JDBC Data Source

To use the JSTL SQL tags, you have to identify a data source. Each of the JSTL SQL tags
accepts a dataSource attribute that lets the SQL tag know what data source to use. Once
the data source is specified, the JSTL tags will collaborate with that source to access the
data reuested.
JSTL provides three pri!ary ways for setting up this collaboration. Let"s take a look at
each !ethod.
Transparent Collaboration
#or this approach, you !ust provide initiali$ation code in the application logic of a server
or other related Java code. #or e%a!ple, you could do this by using the application event
listener of a server. The servlet !ust then store the application"s default &ataSource
ob'ect in the 'ava%.servlet.'sp.'stl.sl.dataSource application or session(scoped variable.
This approach is advantageous because it !akes the selection of the data source
co!pletely transparent to the page progra!!er. The JS) progra!!er does not need to
specify a dataSource attribute to any of the JSTL tags being used, as shown here*
<sql:query ...>
Explicit Collaboration via Application Logic
+t is also possible for servlet Java code to store a data source in a scoped variable for the
page author to use. This variable !ust be stored at either session or application scope.
,ou !ay use any scoped variable na!e. ,ou !ust co!!unicate the scoped variable
na!e and intended scope to the JS) page progra!!er, who is then free to use this scoped
variable in SQL tags. - typical SQL tag using this !ethod would look like this*
<sql:query dataSource="${dataSource}" ...>
Explicit Collaboration via the <sql:setDataSource> Tag
.oth of the previous two approaches reuire access to !ore than 'ust the JS) code. +t is
also possible to specify a data source using only JSTL tags fro! JS). To use this !ethod,
you !ust use the /sl*set&ataSource0 tag to create a &ataSource ob'ect i!ple!ented as
a wrapper around J&.1"s &river2anager class.
The following code loads the driver with the specified classna!e, and establishes a
connection to the database associated with the given J&.1 34L*
<sql:setDataSource var="dataSource"
driver="org.gjt.mm.mysql.Driver"
url="jdbc:mysql://localost/!orum"user=!orumuser"/>
<sql:query dataSource="${dataSource}" .../>
sing the <sql:setDataSource> Tag
,ou use the /sl*set&ataSource0 tag to create a data source that is contained in a scoped
variable. ,ou can use this tag in calls to the other JSTL SQL tags. The
/sl*set&ataSource0 tag has one for!*
<sql:setDataSource var="var#ame"
$sco%e="{%age&request&sessio'&a%%licatio'}"(
$driver="driver)lass#ame"(
$url="jdbc*rl"(
$user="user#ame"(/>
The /sl*set&ataSource0 tag accepts these attributes*
Attribute !equire" #urpose
driver 5 The classna!e of the J&.1 data source that is to be used.
scope 5 The scope of the scoped variable specified by the attribute var. This
attribute defaults to page.
url 5 The 34L of the data source.
user 5 The user that is to be used to log into the data source.
var 5 The scoped variable that will hold the newly created data source.
-s we !entioned earlier, you can use the /sl*set&ataSource0 tag to create ad hoc
connections to a data source fro! JS) pages. This new data source will be assigned to the
variable specified by the var attribute. ,ou specify the scope of this variable with the
scope attribute. +f no scope is specified, then page scope is assu!ed.
J&.1 reuires two para!eters to connect to a data source. #irst, you !ust specify the
J&.1 driver you are going to use with the driver attribute. Second, you !ust specify the
34L of the database that you are connecting to by using the url attribute. That 34L is not
the 6TT)(type 34L that you !ight be fa!iliar with. The e%act for!at of this 34L is
specified by the driver you are using. #or !ore infor!ation on the for!at, consult the
docu!entation associated with the driver you are using.
To show how to use the driver, we"ll now look at the driver tag you !ust use to connect to
2ySQL and 2icrosoft -ccess. +n the following sections, we assu!e that you have
already set up either your 2icrosoft -ccess or your 2ySQL environ!ent for the foru!
application. #irst, we"ll show you how to connect to a 2ySQL data source.
Connecting to $%S&L
The current version of 2ySQL does not co!e with a J&.1 driver. To use 2ySQL with
Java, you !ust obtain a driver elsewhere. One of the !ost co!!on drivers used to allow
Java to access the 2ySQL database is known as the 22 driver. ,ou can obtain this
driver fro! http*77!!!ysl.sourceforge.net7.
+nstalling the driver is easy. ,ou !ust copy the 22 driver"s J-4 file8!!.!ysl(9.:.;9(
bin.'ar8to the lib directory of your <eb application =!ost likely 1*>)rogra!
#iles>-pache To!cat ?.:>webapps>4OOT><E.(+5#>lib@. Once you"ve done this, you
will be ready to use 2ySQL fro! your <eb applications. +f your To!cat <eb server was
already running, you !ust restart it.
5ow let"s look at how you use the set&ataSource tag to open a connection to the foru!
2ySQL database. - <eb page that perfor!s a si!ple uery is shown in Listing A.9.
Listing '() Connecting to $%S&L *quer%(+sp,
<+, taglib uri="tt%://java.su'.com/jstl/core" %re!i-="c" +>
<+, taglib uri="tt%://java.su'.com/jstl/sql" %re!i-="sql" +>
<sql:setDataSource var="dataSource"
driver="org.gjt.mm.mysql.Driver"
url="jdbc:mysql://localost/!orum"user=!orumuser"/>
<tml>
<ead>
<title>.uery /-am%le</title>
</ead>
<body>
<sql:query var = "users" dataSource="${dataSource}">
select c0uid1c0%2d1c0accesses1c0!irst1c0last1c0bad1c0%osted1c0ty%e
!rom t0users
</sql:query>
<table border=3>
<c:!or/ac var="ro2" items="${users.ro2s}">
<tr>
<td><c:out value="${ro2.c0uid}"/></td>
<td><c:out value="${ro2.c0%2d}"/></td>
<td><c:out value="${ro2.c0accesses}"/></td>
<td><c:out value="${ro2.c0!irst}"/></td>
<td><c:out value="${ro2.c0last}"/></td>
<td><c:out value="${ro2.c0bad}"/></td>
<td><c:out value="${ro2.c0%osted}"/></td>
<td><c:out value="${ro2.c0ty%e}"/></td>
</tr>
</c:!or/ac>
</table>
</body>
</tml>
The progra! in Listing A.9 begins by opening a connection to a 2ySQL database that
resides on the local co!puter. The following set&ataSource co!!and acco!plishes this*
<sql:setDataSource var="dataSource"
driver="org.gjt.mm.mysql.Driver"
url="jdbc:mysql://localost/!orum"user=!orumuser"/>
+f you want to connect to a 2ySQL database on a different co!puter, you !ust specify
its hostna!e in place of localhost. Once you"ve e%ecuted this co!!and, this !eans the
scoped variable dataSource now contains a connection to the foru! 2ySQL database. <e
did not specify a scope attribute, so this scoped variable will have page scope.
<ith the data connection open, you !ay now issue ueries against it. This page perfor!s
a uery against the tBusers table and lists all registered users. ,ou !ust specify the na!e
of the data source in the /sl*uery0 tag so that the progra! finds the source*
<sql:query var = "users" dataSource="${dataSource}">
select c0uid1c0%2d1c0accesses1c0!irst1c0last1c0bad1c0%osted1c0ty%e !rom
t0users
</sql:query>
5ow that we"ve seen how to use the /sl*set&ataSource0 tag with 2ySQl, let"s see how
to use it in 2icrosoft -ccess.
Connecting to $icroso-t Access
+t is not difficult to connect to a 2icrosoft -ccess database using JSTL. The
/sl*set&ataSource0 tag !ust specify the O&.1 bridge and a 34L that will enable it to
access a 2icrosoft -ccess database. <e"ll take the sa!e basic uery that we 'ust used
with 2ySQL and !odify the JS) page so that it works with -ccess. This page is shown
in Listing A.C.
Listing '(. Connecting to $icroso-t Access
<+, taglib uri="tt%://java.su'.com/jstl/core" %re!i-="c" +>
<+, taglib uri="tt%://java.su'.com/jstl/sql" %re!i-="sql" +>
<sql:setDataSource var="dataSource"
driver="su'.jdbc.odbc.4dbc5dbcDriver"
url="jdbc:odbc:!orum"/>
<tml>
<ead>
<title>5ut /-am%les</title>
</ead>
<body>
<sql:query var = "users" dataSource="${dataSource}">
select c0uid1c0%2d1c0accesses1c0!irst1c0last1c0bad1c0%osted1c0ty%e
!rom t0users
</sql:query>
<table border=3>
<c:!or/ac var="ro2" items="${users.ro2s}">
<tr>
<td><c:out value="${ro2.c0uid}"/></td>
<td><c:out value="${ro2.c0%2d}"/></td>
<td><c:out value="${ro2.c0accesses}"/></td>
<td><c:out value="${ro2.c0!irst}"/></td>
<td><c:out value="${ro2.c0last}"/></td>
<td><c:out value="${ro2.c0bad}"/></td>
<td><c:out value="${ro2.c0%osted}"/></td>
<td><c:out value="${ro2.c0ty%e}"/></td>
</tr>
</c:!or/ac>
</table>
</body>
</tml>
Listing A.C is nearly identical to Listing A.9, with the e%ception of the driver tag. The
driver tag used in Listing A.C is designed to work with -ccess. 6ere"s the set&ataSource
tag*
<sql:setDataSource var="dataSource"
driver="su'.jdbc.odbc.4dbc5dbcDriver"
url="jdbc:odbc:!orum"/>
-s you can see, the O&.1 bridge driver is specified in the driver attribute. The 34L that
we"ve specified gives the &S58foru!8of the database we are trying to access. This
driver tag will create a new driver in the scoped variable dataSource that holds the
connection to the -ccess database. Once this connection is !ade, the re!aining
co!!ands are the sa!e ones used for the 2ySQL connection.
5OTE
There are so!e subtle differences between the synta% of SQL under 2ySQL and
2icrosoft -ccess. -ll of the sa!ple SQL given in this chapter stays generic enough so
that it will work on either. This is not always an easy task. +f you want to ensure
co!patibility between two databases with the sa!e SQL, you !ust test the SQL on both
databases to be sure that it will work as you intend.

Das könnte Ihnen auch gefallen