Categories: JSTL

JSTL SQL Transaction<sql:transaction> Tag Example

<div dir&equals;"ltr" style&equals;"text-align&colon; left&semi;" trbidi&equals;"on">&NewLine;<div dir&equals;"ltr" style&equals;"text-align&colon; left&semi;" trbidi&equals;"on">The <i><b>&lt&semi;sql&colon;transaction&gt&semi;<&sol;b><&sol;i> tag is used to group <b><i>&lt&semi;sql&colon;query&gt&semi;<&sol;i><&sol;b> and <i><b>&lt&semi;sql&colon;update&gt&semi;<&sol;b><&sol;i> into transactions&period; You can put as many <i><b>&lt&semi;sql&colon;query&gt&semi;<&sol;b><&sol;i> and &lt&semi;sql&colon;update&gt&semi; as statements inside &lt&semi;sql&colon;transaction&gt&semi; to make them a single transaction&period;<&sol;p>&NewLine;<p> It ensures that the database modifications performed by the nested actions are either committed or rolled back if an exception is thrown by any nested action&period;<&sol;p>&NewLine;<p> JSTL Transaction Tag provides the capability to run SQL statement in a group&period; You can run multiple SQL statements at a time&period; It is called one single transaction&period; In this transaction either all statments run successfully or all fail if one statement does not run successfully&period; <&sol;p>&NewLine;<p> <b>Syntax<&sol;b><br &sol;>&NewLine;<i>&lt&semi;sql&colon;transaction dataSource&equals;&&num;8221&semi;&lt&semi;string&gt&semi;&&num;8221&semi; isolation&equals;&&num;8221&semi;&lt&semi;string&gt&semi;&&num;8221&semi;&sol;&gt&semi;<&sol;i><br &sol;>&NewLine;<b><br &sol;>&NewLine;<&sol;b> <b>JSTL Transaction Tag has following attribute&period;<&sol;b><&sol;p>&NewLine;<p> <b>1&period; <i>dataSource <&sol;i>Attribute&colon;<&sol;b> Specifies the datasource for the transaction&period;<&sol;p>&NewLine;<p> <b>2&period; <i>isolation <&sol;i>Attribute&colon; <&sol;b>Specifies the level of isolation for the transaction&period; <&sol;p>&NewLine;<p> <b>JSTL SQL Transaction Tag Example&colon;-<&sol;b><&sol;p>&NewLine;<pre class&equals;"highlight" name&equals;"code">&lt&semi;&percnt;&commat; page import&equals;"java&period;io&period;&ast;&comma;java&period;util&period;&ast;&comma;java&period;sql&period;&ast;"&percnt;&gt&semi;&NewLine;&lt&semi;&percnt;&commat; page import&equals;"javax&period;servlet&period;http&period;&ast;&comma;javax&period;servlet&period;&ast;"&percnt;&gt&semi;&NewLine;&lt&semi;&percnt;&commat; page import&equals;"java&period;util&period;Date&comma;java&period;text&period;&ast;" &percnt;&gt&semi;&NewLine;&NewLine;&lt&semi;&percnt;&commat; taglib uri&equals;"http&colon;&sol;&sol;java&period;sun&period;com&sol;jsp&sol;jstl&sol;core" prefix&equals;"c"&percnt;&gt&semi;&NewLine;&lt&semi;&percnt;&commat; taglib uri&equals;"http&colon;&sol;&sol;java&period;sun&period;com&sol;jsp&sol;jstl&sol;sql" prefix&equals;"sql"&percnt;&gt&semi;&NewLine; &NewLine;&lt&semi;html&gt&semi;&NewLine;&lt&semi;head&gt&semi;&NewLine;&lt&semi;title&gt&semi;JSTL sql&colon;transaction Tag&lt&semi;&sol;title&gt&semi;&NewLine;&lt&semi;&sol;head&gt&semi;&NewLine;&lt&semi;body&gt&semi;&NewLine; &NewLine;&lt&semi;sql&colon;setDataSource var&equals;"snapshot" driver&equals;"com&period;mysql&period;jdbc&period;Driver"&NewLine; url&equals;"jdbc&colon;mysql&colon;&sol;&sol;localhost&sol;TEST"&NewLine; user&equals;"root" password&equals;"cohondob"&sol;&gt&semi;&NewLine;&NewLine;&lt&semi;&percnt;&NewLine;Date DoB &equals; new Date&lpar;"2013&sol;10&sol;23"&rpar;&semi;&NewLine;int studentId &equals; 1111&semi;&NewLine;&percnt;&gt&semi;&NewLine;&NewLine;&lt&semi;sql&colon;transaction dataSource&equals;"&dollar;&lbrace;snapshot&rcub;"&gt&semi;&NewLine; &lt&semi;sql&colon;update var&equals;"count"&gt&semi;&NewLine; UPDATE Students SET firstName &equals; 'Sandhya' WHERE Id &equals; 2222&NewLine; &lt&semi;&sol;sql&colon;update&gt&semi;&NewLine; &lt&semi;sql&colon;update var&equals;"count"&gt&semi;&NewLine; UPDATE Students SET fistName &equals; 'Sweetu' WHERE Id &equals; 3333&NewLine; &lt&semi;&sol;sql&colon;update&gt&semi;&NewLine; &lt&semi;sql&colon;update var&equals;"count"&gt&semi;&NewLine; INSERT INTO Students &NewLine; VALUES &lpar;4444&comma;'Neha'&comma; 'Verma'&comma; '2013&sol;10&sol;23'&rpar;&semi;&NewLine; &lt&semi;&sol;sql&colon;update&gt&semi;&NewLine;&lt&semi;&sol;sql&colon;transaction&gt&semi;&NewLine;&NewLine;&lt&semi;sql&colon;query dataSource&equals;"&dollar;&lbrace;snapshot&rcub;" var&equals;"result"&gt&semi;&NewLine; SELECT &ast; from Students&semi;&NewLine;&lt&semi;&sol;sql&colon;query&gt&semi;&NewLine; &NewLine;&lt&semi;table border&equals;"1" width&equals;"100&percnt;"&gt&semi;&NewLine;&lt&semi;tr&gt&semi;&NewLine;&lt&semi;th&gt&semi;Emp ID&lt&semi;&sol;th&gt&semi;&NewLine;&lt&semi;th&gt&semi;First Name&lt&semi;&sol;th&gt&semi;&NewLine;&lt&semi;th&gt&semi;Last Name&lt&semi;&sol;th&gt&semi;&NewLine;&lt&semi;th&gt&semi;DoB&lt&semi;&sol;th&gt&semi;&NewLine;&lt&semi;&sol;tr&gt&semi;&NewLine;&lt&semi;c&colon;forEach var&equals;"row" items&equals;"&dollar;&lbrace;result&period;rows&rcub;"&gt&semi;&NewLine;&lt&semi;tr&gt&semi;&NewLine;&lt&semi;td&gt&semi;&lt&semi;c&colon;out value&equals;"&dollar;&lbrace;row&period;id&rcub;"&sol;&gt&semi;&lt&semi;&sol;td&gt&semi;&NewLine;&lt&semi;td&gt&semi;&lt&semi;c&colon;out value&equals;"&dollar;&lbrace;row&period;first&rcub;"&sol;&gt&semi;&lt&semi;&sol;td&gt&semi;&NewLine;&lt&semi;td&gt&semi;&lt&semi;c&colon;out value&equals;"&dollar;&lbrace;row&period;last&rcub;"&sol;&gt&semi;&lt&semi;&sol;td&gt&semi;&NewLine;&lt&semi;td&gt&semi;&lt&semi;c&colon;out value&equals;"&dollar;&lbrace;row&period;dob&rcub;"&sol;&gt&semi;&lt&semi;&sol;td&gt&semi;&NewLine;&lt&semi;&sol;tr&gt&semi;&NewLine;&lt&semi;&sol;c&colon;forEach&gt&semi;&NewLine;&lt&semi;&sol;table&gt&semi;&NewLine; &NewLine;&lt&semi;&sol;body&gt&semi;&NewLine;&lt&semi;&sol;html&gt&semi;&NewLine;<&sol;pre>&NewLine;<&sol;div>&NewLine;<p>&NewLine;<b>Now try to access above JSP&comma; which should display the following result&colon;<&sol;b><&sol;p>&NewLine;<table border&equals;"1" style&equals;"width&colon; 100&percnt;px&semi;">&NewLine;<tbody>&NewLine;<tr>&NewLine;<th>Emp ID<&sol;th>&NewLine;<th>First Name<&sol;th>&NewLine;<th>Last Name<&sol;th>&NewLine;<th>DOB<&sol;th>&NewLine;<&sol;tr>&NewLine;<tr>&NewLine;<td>1111<&sol;td>&NewLine;<td>Dinesh<&sol;td>&NewLine;<td>Rajput<&sol;td>&NewLine;<td>2013&sol;10&sol;23<&sol;td>&NewLine;<&sol;tr>&NewLine;<tr>&NewLine;<td>2222<&sol;td>&NewLine;<td>Anu<&sol;td>&NewLine;<td>Rajput<&sol;td>&NewLine;<td>2013&sol;10&sol;23<&sol;td>&NewLine;<&sol;tr>&NewLine;<tr>&NewLine;<td>2222<&sol;td>&NewLine;<td>Sweetu<&sol;td>&NewLine;<td>Rajput<&sol;td>&NewLine;<td>2013&sol;10&sol;23<&sol;td>&NewLine;<&sol;tr>&NewLine;<tr>&NewLine;<td>2222<&sol;td>&NewLine;<td>Sandhya<&sol;td>&NewLine;<td>Bansal<&sol;td>&NewLine;<td>2013&sol;10&sol;23<&sol;td>&NewLine;<&sol;tr>&NewLine;<&sol;tbody>&NewLine;<&sol;table>&NewLine;<div style&equals;"background-color&colon; pink&semi; border-width&colon; thin&semi; text-align&colon; center&semi;"><b>&lt&semi;&lt&semi;<a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;2013&sol;10&sol;jstl-sql-dateparam-tag-example&period;html">Previous<&sol;a> &lt&semi;&lt&semi;&nbsp&semi;&nbsp&semi; &vert;&vert; <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;2013&sol;10&sol;jsp-standard-tag-library-jstl-tutorial&period;html">Index <&sol;a>&vert;&vert; &nbsp&semi; &gt&semi;&gt&semi;<a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;2013&sol;11&sol;jstl-fncontains-function&period;html">Next<&sol;a> &gt&semi;&gt&semi;<&sol;b><&sol;div>&NewLine;<p>&NewLine;<&sol;div>&NewLine;<div class&equals;"wp-post-navigation"> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <div class&equals;"wp-post-navigation-pre"> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;jstl-sql-dateparam-tag-example&sol;">Previous<&sol;a> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <&sol;div> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <div class&equals;"wp-post-navigation-next"> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;jstl-fncontains-function&sol;">Next<&sol;a> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <&sol;div> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;<&sol;div>&NewLine;<script type&equals;"text&sol;javascript">&NewLine;jQuery&lpar;document&rpar;&period;ready&lpar;function&lpar;&dollar;&rpar; &lbrace;&NewLine; &dollar;&period;post&lpar;'https&colon;&sol;&sol;dineshonjava&period;com&sol;wp-admin&sol;admin-ajax&period;php'&comma; &lbrace;action&colon; 'mts&lowbar;view&lowbar;count'&comma; id&colon; '293'&rcub;&rpar;&semi;&NewLine;&rcub;&rpar;&semi;&NewLine;<&sol;script>

Dinesh Rajput

Dinesh Rajput is the chief editor of a website Dineshonjava, a technical blog dedicated to the Spring and Java technologies. It has a series of articles related to Java technologies. Dinesh has been a Spring enthusiast since 2008 and is a Pivotal Certified Spring Professional, an author of a book Spring 5 Design Pattern, and a blogger. He has more than 10 years of experience with different aspects of Spring and Java design and development. His core expertise lies in the latest version of Spring Framework, Spring Boot, Spring Security, creating REST APIs, Microservice Architecture, Reactive Pattern, Spring AOP, Design Patterns, Struts, Hibernate, Web Services, Spring Batch, Cassandra, MongoDB, and Web Application Design and Architecture. He is currently working as a technology manager at a leading product and web development company. He worked as a developer and tech lead at the Bennett, Coleman & Co. Ltd and was the first developer in his previous company, Paytm. Dinesh is passionate about the latest Java technologies and loves to write technical blogs related to it. He is a very active member of the Java and Spring community on different forums. When it comes to the Spring Framework and Java, Dinesh tops the list!

Share
Published by
Dinesh Rajput

Recent Posts

Strategy Design PatternsĀ using Lambda

Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…

4 years ago

Decorator Pattern using Lambda

Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…

4 years ago

Delegating pattern using lambda

Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…

4 years ago

Spring Vs Django- Know The Difference Between The Two

Technology has emerged a lot in the last decade, and now we have artificial intelligence;…

4 years ago

TOP 20 MongoDB INTERVIEW QUESTIONS 2022

Managing a database is becoming increasingly complex now due to the vast amount of data…

4 years ago

Scheduler @Scheduled Annotation Spring Boot

Overview In this article, we will explore Spring Scheduler how we could use it by…

4 years ago