Set JSTL datasource
August 16, 2010 20:42:56 Last update: August 16, 2010 20:47:24
Of course you'll need a datasource to run your SQL statements. It can be set in
In
where the parameter value is a relative JNDI path, or parameters for a JDBC connection. In the above example, the real JNDI name for the data source would be:
In the case of JDBC connection parameters, the expected format is:
For example:
Exports a data source either as a scoped variable or as the data source configuration
variable (
Syntax:
Attributes:
web.xml, or with the <sql:setDataSource> tag.
In
web.xml, the declaration is something like this:
<context-param> <param-name>javax.servlet.jsp.jstl.sql.dataSource</param-name> <param-value>jdbc/myDatabase</param-value> </context-param>
where the parameter value is a relative JNDI path, or parameters for a JDBC connection. In the above example, the real JNDI name for the data source would be:
java:comp/env/jdbc/myDataSource. You are out of luck if the JNDI path for the data source does not fall under the java:comp/env/ namespace.
In the case of JDBC connection parameters, the expected format is:
url[,[driver][,[user][,password]]]
For example:
jdbc:mysql://localhost/,org.gjt.mm.mysql.Driver, where no user name or password is used.
<sql:setDataSource>
Exports a data source either as a scoped variable or as the data source configuration
variable (
javax.servlet.jsp.jstl.sql.dataSource).
Syntax:
<sql:setDataSource { dataSource="dataSource" | url="jdbcUrl" [driver="driverClassName"] [user="userName"] [password="password"] } [var="varName"] [scope="{page|request|session|application}"]/>
Attributes:
| Name | Dynamic? | Type | Description |
|---|---|---|---|
| dataSource | true | String or javax.sql.DataSource | Data source. If specified as a string, it can either be a relative path to a JNDI resource, or a JDBC parameters string. |
| driver | true | String | JDBC parameter: driver class name. |
| url | true | String | JDBC parameter: URL associated with the database. |
| user | true | String | JDBC parameter: database user name to use for the JDBC connection. |
| password | true | String | JDBC parameter: database user password |
| var | false | String | Name of the exported scoped variable for the data source specified. Type can be String or DataSource. |
| scope | false | String | If var is specified, scope of the exported variable. Otherwise, scope of the data source configuration variable. |