Set JSTL datasource 

Joined:
08/13/2009
Posts:
164

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 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:
NameDynamic?TypeDescription
dataSourcetrueString or javax.sql.DataSourceData source. If specified as a string, it can either be a relative path to a JNDI resource, or a JDBC parameters string.
drivertrueStringJDBC parameter: driver class name.
urltrueStringJDBC parameter: URL associated with the database.
usertrueStringJDBC parameter: database user name to use for the JDBC connection.
passwordtrueStringJDBC parameter: database user password
varfalseStringName of the exported scoped variable for the data source specified. Type can be String or DataSource.
scopefalseStringIf var is specified, scope of the exported variable. Otherwise, scope of the data source configuration variable.
Share |
| Comment  | Tags