reffec.com

in Java Display ANSI/AIM Code 39 in Java

<fmt:setLocale> use none none printing tointegrate none in none Visual Studio 2010 <fmt:setTimeZone> <fmt:timeZone> Sets the time zone to use inside its body. SQL JSTL Tag Library The SQL JSTL none for none tag library allows us to execute SQL queries from JSP pages. As this tag library mixes presentation and database access code, it should only be used for prototyping and for writing simple "throwaway" applications. For more complex applications, it is always a good idea to follow the DAO and MVC design patterns.

. [ 158 ]. 5 . The following example illustrates the most commonly used tags in the SQL JSTL Tag Library. <%@ page l none for none anguage="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql"%> <%@taglib uri="http://java.

sun.com/jsp/jstl/core" prefix="c"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.

w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>SQL Tag Demo</title> </head> <body> <sql:setDataSource dataSource="jdbc/__CustomerDBPool" /> <sql:transaction> <sql:update> insert into CUSTOMERS (CUSTOMER_ID, FIRST_NAME, LAST_NAME) values (((select max(CUSTOMER_ID) from customers) + 1), , ) <sql:param value="${param.

firstName}" /> <sql:param value="${param.lastName}" /> </sql:update> </sql:transaction> <p>Successfully inserted the following row into the CUSTOMERS table:</ p> <sql:query var="selectedRows" sql="select FIRST_NAME, LAST_NAME from customers where FIRST_NAME = and LAST_NAME = "> <sql:param value="${param.firstName}" /> <sql:param value="${param.

lastName}" /> </sql:query> <table border="1" cellpadding="0" cellspacing="0"> <tr> <td>First Name</td> <td>Last Name</td> </tr> <c:forEach var="currentRow" items="${selectedRows.rows}"> <tr> <td><c:out value="${currentRow.FIRST_NAME}" /></td> <td><c:out value="${currentRow.

LAST_NAME}" /></td> </tr> </c:forEach> </table> </body> </html> [ 159 ]. JSP Standard Tag Library After packagi none for none ng this JSP in a WAR file, deploying the WAR file, and pointing the browser to the JSP"s URL, we should see a page like the following:. Like most of our examples, the above page is pretty simplistic, and does not necessarily represent what would be done in an actual application. The page inserts a row into the CUSTOMERS table and then queries the table for rows matching the values inserted. A real application (keeping in mind that the SQL tag library should only be used for very simple applications) would typically insert values obtained from request parameters into the database.

It would be unlikely for the same page to query the database for the data just inserted; this would probably be done in a separate page. The first JSTL SQL tag we see in the example is the <sql:setDataSource> tag. This tag sets the data source to be used for database access.

The data source can either be obtained via JNDI by using its JNDI name as the value of this tags datasource attribute or by specifying a JDBC URL, user name, and password via the url, user, and password attributes. This example uses the first approach. In order for this approach to work correctly, a <resource-ref> element must be added to the application"s web.

xml file.. <web-app x none for none mlns="http://java.sun.com/xml/ns/javaee" version="2.

5" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.

sun.com/xml/ns/javaee http://java. sun.

com/xml/ns/javaee/web-app_2_5.xsd"> <resource-ref> <res-ref-name>jdbc/__CustomerDBPool</res-ref-name> <res-type>javax.sql.

DataSource</res-type> <res-auth>Container</res-auth> </resource-ref> </web-app>. [ 160 ]. 5 . The <res-r ef-name> subelement of the <resource-ref> element contains the JNDI name of the data source. This needs to be set up in the application server. The example uses the data source we used in 4 (refer to the readme.

txt file for 4 for instructions on setting up this data source). The <res-type> subelement of the <resource-ref> element contains the fully qualified name of the resource to be obtained via JNDI; for data sources, this will always be javax.sql.

DataSource. The <res-auth> subelement of the <resource-ref> element should have a value of Container when using the <resource-ref> element to define a data source as a resource. This allows the application server to use the credentials set up in the connection pool corresponding to the data source to log into the database.

. No Suitable D none for none river SQL Exception Sometimes the <sql:setDataSource> tag will result in a java.sql.SQLException: No suitable driver exception when using its datasource attribute to locate the data source via JNDI.

This typically means that we forgot to modify the application"s web.xml as described above..

As we mention ed before, an alternative way of using the <sql:setDataSource> tag is to specify the database connection URL and credentials. Had we used this approach in the example, the <sql:setDataSource> tag would have looked like this:.
Copyright © reffec.com . All rights reserved.