JSTL SQL sql:transaction Tag

Transaction means handling set of tasks into single execution unit. It provides connection to all data base operations and executes all statements in one transaction. This tag combines both and tags into transactions. We can add and statements into to make single transaction.

<sql: setDataSource var=”ds” driver=”com.mysql.jdbc.Driver” url=”jdbc: mysql: //localhost/test” user=”root” password=”” /> tag is used to connect to the data base and specify variable name for data source and driver name, url , user name and password of the database.

<sql:transaction dataSource=”${ds}” > <sql:update var=”count” >update student set name=’vikrant’ where id=204 </sql:update> tag is used to update database table student by setting new name to id=204.

< sql:update var=”count”> insert into student values(206,’ramesh’,’1995/09/21′) </sql:update> tag is used to update table by inserting new row to database table student.

<sql:transaction dataSource=”${ds}”> tag is used to run SQL statement in a group. We can run multiple SQL statements at a time by using this tag.

<sql:query dataSource=”${ds}” var=”result”>select * from student; </sql:query> tag is used to fetch the data from the database table which takes attributes such as ,datasource attribute specifies the datasource, var attribute result of the SQL statement.

<c:forEach var=”row” items=”${result.rows}”> tag is used to produce database table from SQL query and items attribute specifies collection of items to iterate in the loop.

Steps for Execution

Save the file as TransactionExample.jsp in eclipse IDE.

Now select the jsp file, right click on mouse and select Run as -> Run on Server.

Output

Before execution of the program table would be like this: After successful execution of the program we will get the following result:

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.