Categories: Struts2Tutorial

Struts 2 Database Access

<div dir&equals;"ltr" style&equals;"text-align&colon; justify&semi;">&NewLine;<div dir&equals;"ltr" style&equals;"text-align&colon; justify&semi;">&NewLine;<div dir&equals;"ltr" style&equals;"text-align&colon; justify&semi;">&NewLine;<div dir&equals;"ltr" style&equals;"text-align&colon; justify&semi;">&NewLine;<div dir&equals;"ltr" style&equals;"text-align&colon; justify&semi;">&NewLine;<div dir&equals;"ltr" style&equals;"text-align&colon; justify&semi;">&NewLine;<div dir&equals;"ltr" style&equals;"text-align&colon; justify&semi;">&NewLine;<div dir&equals;"ltr" style&equals;"text-align&colon; justify&semi;">&NewLine;<div dir&equals;"ltr" style&equals;"text-align&colon; justify&semi;">&NewLine;<div dir&equals;"ltr" style&equals;"text-align&colon; justify&semi;">&NewLine;<div dir&equals;"ltr" style&equals;"text-align&colon; justify&semi;">&NewLine;<p>In this chapter we will teach you how to access a database using Struts 2 in simple steps&period; Struts is a MVC framework and not a database framework but it provides excellent support for JPA&sol;Hibernate integration&period; We shall look at the hibernate integration in a later chapter&comma; but in this chapter we shall use plain old JDBC to access the database&period;<&sol;p>&NewLine;<p>The first step in this chapter is to setup and prime our database&period; I am using MySQL as my database for this example&period; I have MySQL installed on my machine and I have created a new database called &&num;8220&semi;<b>DOJDB<&sol;b>&&num;8220&semi;&period; I have created a table called &&num;8220&semi;<b>USER<&sol;b>&&num;8220&semi;and populated it with some values&period; Below is the script I used to create and populate the table&period;<&sol;p>&NewLine;<&sol;div>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;<pre class&equals;"highlight">CREATE TABLE users &lpar; &NewLine; User&lowbar;ID VARCHAR&lpar;10&rpar; NOT NULL&comma; &NewLine; UserName VARCHAR&lpar;10&rpar; NOT NULL&comma; &NewLine; PASSWORD VARCHAR&lpar;10&rpar; NOT NULL&comma; &NewLine; PRIMARY KEY &lpar;User&lowbar;ID&rpar; &NewLine; &rpar; &NewLine; &NewLine; &NewLine;INSERT INTO &grave;DOJDB&grave;&period;&grave;users&grave; &NewLine; &lpar;&grave;User&lowbar;ID&grave;&comma; &NewLine; &grave;UserName&grave;&comma; &NewLine; &grave;PASSWORD&grave; &NewLine; &rpar; &NewLine; VALUES &NewLine; &lpar;'DOJ01'&comma; &NewLine; 'Dinesh'&comma; &NewLine; 'Sweety' &NewLine; &rpar;&semi; &NewLine;<&sol;pre>&NewLine;<h2><b>Create Action&colon;<&sol;b><&sol;h2>&NewLine;<p>The action class has the properties corresponding to the columns in the database table&period; We have user&comma; password and name as String attributes&period; In the action method&comma; we use the user and password parameters to check if the user exists&comma; if so &comma; we display the user name in the next screen&period; If the user has entered wrong information&comma; we send them to the login screen again&period;<br &sol;>&NewLine;Following is the content of<i><b> LoginAction&period;java<&sol;b><&sol;i> file&colon;<&sol;p>&NewLine;<&sol;div>&NewLine;<div id&equals;"ads-id" align&equals;"center"><&sol;div>&NewLine;<pre class&equals;"highlight">package com&period;dineshonjava&period;struts2&period;login&semi; &NewLine; &NewLine;import java&period;sql&period;Connection&semi; &NewLine;import java&period;sql&period;DriverManager&semi; &NewLine;import java&period;sql&period;PreparedStatement&semi; &NewLine;import java&period;sql&period;ResultSet&semi; &NewLine; &NewLine;import com&period;opensymphony&period;xwork2&period;ActionSupport&semi; &NewLine; &NewLine;&sol;&ast;&ast; &NewLine; &ast; &commat;author Dinesh Rajput &NewLine; &ast; &NewLine; &ast;&sol; &NewLine;&commat;SuppressWarnings&lpar;"serial"&rpar; &NewLine;public class LoginAction extends ActionSupport&lbrace; &NewLine; private String username&semi; &NewLine; private String password&semi; &NewLine; private String userid&semi; &NewLine; &NewLine; public String execute&lpar;&rpar; &lbrace; &NewLine; String ret &equals; ERROR&semi; &NewLine; Connection conn &equals; null&semi; &NewLine; &NewLine; try &lbrace; &NewLine; String URL &equals; "jdbc&colon;mysql&colon;&sol;&sol;localhost&colon;3306&sol;DOJDB"&semi; &NewLine; Class&period;forName&lpar;"com&period;mysql&period;jdbc&period;Driver"&rpar;&semi; &NewLine; conn &equals; DriverManager&period;getConnection&lpar;URL&comma; "root"&comma; "root"&rpar;&semi; &NewLine; String sql &equals; "SELECT name FROM user WHERE"&semi; &NewLine; sql&plus;&equals;" userName &equals; &quest; AND password &equals; &quest;"&semi; &NewLine; PreparedStatement ps &equals; conn&period;prepareStatement&lpar;sql&rpar;&semi; &NewLine; ps&period;setString&lpar;1&comma; username&rpar;&semi; &NewLine; ps&period;setString&lpar;2&comma; password&rpar;&semi; &NewLine; ResultSet rs &equals; ps&period;executeQuery&lpar;&rpar;&semi; &NewLine; &NewLine; while &lpar;rs&period;next&lpar;&rpar;&rpar; &lbrace; &NewLine; username &equals; rs&period;getString&lpar;1&rpar;&semi; &NewLine; ret &equals; SUCCESS&semi; &NewLine; &rcub; &NewLine; &rcub; catch &lpar;Exception e&rpar; &lbrace; &NewLine; addActionError&lpar;getText&lpar;"error&period;login"&rpar;&rpar;&semi; &NewLine; ret &equals; ERROR&semi; &NewLine; &rcub; finally &lbrace; &NewLine; if &lpar;conn &excl;&equals; null&rpar; &lbrace; &NewLine; try &lbrace; &NewLine; conn&period;close&lpar;&rpar;&semi; &NewLine; &rcub; catch &lpar;Exception e&rpar; &lbrace; &NewLine; &rcub; &NewLine; &rcub; &NewLine; &rcub; &NewLine; return ret&semi; &NewLine; &rcub; &NewLine; &NewLine; public String getUsername&lpar;&rpar; &lbrace; &NewLine; return username&semi; &NewLine; &rcub; &NewLine; &NewLine; public void setUsername&lpar;String username&rpar; &lbrace; &NewLine; this&period;username &equals; username&semi; &NewLine; &rcub; &NewLine; &NewLine; public String getPassword&lpar;&rpar; &lbrace; &NewLine; return password&semi; &NewLine; &rcub; &NewLine; &NewLine; public void setPassword&lpar;String password&rpar; &lbrace; &NewLine; this&period;password &equals; password&semi; &NewLine; &rcub; &NewLine; &NewLine; public String getUserid&lpar;&rpar; &lbrace; &NewLine; return userid&semi; &NewLine; &rcub; &NewLine; &NewLine; public void setUserid&lpar;String userid&rpar; &lbrace; &NewLine; this&period;userid &equals; userid&semi; &NewLine; &rcub; &NewLine; &NewLine;&rcub; &NewLine; &NewLine;<&sol;pre>&NewLine;<&sol;div>&NewLine;<h2><b>Create main page&colon;<&sol;b><&sol;h2>&NewLine;<p>Now&comma; let us create a JSP file<i><b> Login&period;jsp<&sol;b><&sol;i> to collect the <b>username <&sol;b>and <b>password<&sol;b>&period; This username and password will be checked against the database&period;<&sol;p>&NewLine;<pre class&equals;"highlight">&lt&semi;&percnt;&commat; page contentType&equals;"text&sol;html&semi; charset&equals;UTF-8"&percnt;&gt&semi; &NewLine;&lt&semi;&percnt;&commat; taglib prefix&equals;"s" uri&equals;"&sol;struts-tags"&percnt;&gt&semi; &NewLine; &NewLine;&lt&semi;&excl;DOCTYPE html PUBLIC "-&sol;&sol;W3C&sol;&sol;DTD HTML 4&period;01 Transitional&sol;&sol;EN" "http&colon;&sol;&sol;www&period;w3&period;org&sol;TR&sol;html4&sol;loose&period;dtd"&gt&semi; &NewLine;&lt&semi;html&gt&semi; &NewLine;&lt&semi;head&gt&semi; &NewLine;&lt&semi;meta http-equiv&equals;"Content-Type" content&equals;"text&sol;html&semi; charset&equals;ISO-8859-1"&gt&semi; &NewLine;&lt&semi;title&gt&semi;Struts 2 - Login Application &vert; dineshonjava&period;com&lt&semi;&sol;title&gt&semi; &NewLine;&lt&semi;&sol;head&gt&semi; &NewLine;&lt&semi;body&gt&semi; &NewLine;&lt&semi;h2&gt&semi;Struts 2 - Login Application&lt&semi;&sol;h2&gt&semi; &NewLine;&lt&semi;font color&equals;"red"&gt&semi;&lt&semi;s&colon;actionerror &sol;&gt&semi;&lt&semi;&sol;font&gt&semi; &NewLine;&lt&semi;s&colon;form action&equals;"login&sol;welcome&period;action" method&equals;"post"&gt&semi; &NewLine; &lt&semi;s&colon;textfield name&equals;"username" key&equals;"label&period;username" size&equals;"20" &sol;&gt&semi; &NewLine; &lt&semi;s&colon;password name&equals;"password" key&equals;"label&period;password" size&equals;"20" &sol;&gt&semi; &NewLine; &lt&semi;s&colon;submit method&equals;"execute" key&equals;"label&period;login" align&equals;"center" &sol;&gt&semi; &NewLine;&lt&semi;&sol;s&colon;form&gt&semi; &NewLine;&lt&semi;&sol;body&gt&semi; &NewLine;&lt&semi;&sol;html&gt&semi; &NewLine;<&sol;pre>&NewLine;<&sol;div>&NewLine;<h2><b>Create Views&colon;<&sol;b><&sol;h2>&NewLine;<p>Now let us create <i><b>success&period;jsp<&sol;b><&sol;i> file which will be invoked in case action returns <b>SUCCESS<&sol;b>&comma; but we will have another view file in case of an <b>ERROR <&sol;b>is returned from the action&period;<&sol;p>&NewLine;<pre class&equals;"highlight">&lt&semi;&percnt;&commat; page contentType&equals;"text&sol;html&semi; charset&equals;UTF-8"&percnt;&gt&semi; &NewLine;&lt&semi;&percnt;&commat; taglib prefix&equals;"s" uri&equals;"&sol;struts-tags"&percnt;&gt&semi; &NewLine;&lt&semi;&excl;DOCTYPE html PUBLIC "-&sol;&sol;W3C&sol;&sol;DTD HTML 4&period;01 Transitional&sol;&sol;EN" "http&colon;&sol;&sol;www&period;w3&period;org&sol;TR&sol;html4&sol;loose&period;dtd"&gt&semi; &NewLine;&lt&semi;html&gt&semi; &NewLine;&lt&semi;head&gt&semi; &NewLine;&lt&semi;meta http-equiv&equals;"Content-Type" content&equals;"text&sol;html&semi; charset&equals;ISO-8859-1"&gt&semi; &NewLine;&lt&semi;title&gt&semi;Welcome&lt&semi;&sol;title&gt&semi; &NewLine;&lt&semi;&sol;head&gt&semi; &NewLine; &NewLine;&lt&semi;body&gt&semi; &NewLine; &lt&semi;h2&gt&semi;Hello Welcome &comma; &lt&semi;s&colon;property value&equals;"username" &sol;&gt&semi;&period;&period;&period;&excl; Dineshonjava&period;com&lt&semi;&sol;h2&gt&semi; &NewLine; &NewLine;&lt&semi;&sol;body&gt&semi; &NewLine;&lt&semi;&sol;html&gt&semi; &NewLine;<&sol;pre>&NewLine;<&sol;div>&NewLine;<h2><b>Configuration Files&colon;<&sol;b><&sol;h2>&NewLine;<p>Finally&comma; let us put everything together using the <b>struts&period;xml<&sol;b> configuration file as follows&colon;<&sol;p>&NewLine;<pre class&equals;"highlight">&lt&semi;&quest;xml version&equals;"1&period;0" encoding&equals;"UTF-8" &quest;&gt&semi; &NewLine;&lt&semi;&excl;DOCTYPE struts PUBLIC &NewLine; "-&sol;&sol;Apache Software Foundation&sol;&sol;DTD Struts Configuration 2&period;0&sol;&sol;EN" &NewLine; "http&colon;&sol;&sol;struts&period;apache&period;org&sol;dtds&sol;struts-2&period;0&period;dtd"&gt&semi; &NewLine; &NewLine;&lt&semi;struts&gt&semi; &NewLine; &lt&semi;constant name&equals;"struts&period;enable&period;DynamicMethodInvocation" value&equals;"false" &sol;&gt&semi; &NewLine; &lt&semi;constant name&equals;"struts&period;devMode" value&equals;"false" &sol;&gt&semi; &NewLine; &lt&semi;constant name&equals;"struts&period;custom&period;i18n&period;resources" value&equals;"myapp" &sol;&gt&semi; &NewLine; &NewLine; &lt&semi;package name&equals;"login" extends&equals;"struts-default" namespace&equals;"&sol;login"&gt&semi; &NewLine; &lt&semi;action name&equals;"welcome" class&equals;"com&period;dineshonjava&period;struts2&period;login&period;LoginAction"&gt&semi; &NewLine; &lt&semi;result name&equals;"success"&gt&semi;&sol;Welcome&period;jsp&lt&semi;&sol;result&gt&semi; &NewLine; &lt&semi;result name&equals;"error"&gt&semi;&sol;Login&period;jsp&lt&semi;&sol;result&gt&semi; &NewLine; &lt&semi;&sol;action&gt&semi; &NewLine; &lt&semi;&sol;package&gt&semi; &NewLine; &NewLine; &lt&semi;&sol;struts&gt&semi; &NewLine;<&sol;pre>&NewLine;<&sol;div>&NewLine;<p><b>myapp&period;properties<&sol;b><&sol;p>&NewLine;<pre class&equals;"highlight">label&period;username&equals; Username &NewLine;label&period;password&equals; Password &NewLine;label&period;login&equals; Login &NewLine;error&period;login&equals; Invalid Username&sol;Password&period; Please try again&period; &NewLine;<&sol;pre>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;<&sol;div>&NewLine;<p>Following is the content of <i><b>web&period;xml<&sol;b><&sol;i> file&colon;<&sol;p>&NewLine;<pre class&equals;"highlight">&lt&semi;&quest;xml version&equals;"1&period;0" encoding&equals;"UTF-8"&quest;&gt&semi; &NewLine;&lt&semi;web-app xmlns&colon;xsi&equals;"http&colon;&sol;&sol;www&period;w3&period;org&sol;2001&sol;XMLSchema-instance" xmlns&equals;"http&colon;&sol;&sol;java&period;sun&period;com&sol;xml&sol;ns&sol;javaee" xmlns&colon;web&equals;"http&colon;&sol;&sol;java&period;sun&period;com&sol;xml&sol;ns&sol;javaee&sol;web-app&lowbar;2&lowbar;5&period;xsd" xsi&colon;schemaLocation&equals;"http&colon;&sol;&sol;java&period;sun&period;com&sol;xml&sol;ns&sol;javaee http&colon;&sol;&sol;java&period;sun&period;com&sol;xml&sol;ns&sol;javaee&sol;web-app&lowbar;3&lowbar;0&period;xsd" id&equals;"WebApp&lowbar;ID" version&equals;"3&period;0"&gt&semi; &NewLine; &lt&semi;display-name&gt&semi;Struts2MyFirstApp&lt&semi;&sol;display-name&gt&semi; &NewLine; &lt&semi;filter&gt&semi; &NewLine; &lt&semi;filter-name&gt&semi;struts2&lt&semi;&sol;filter-name&gt&semi; &NewLine; &lt&semi;filter-class&gt&semi; &NewLine; org&period;apache&period;struts2&period;dispatcher&period;FilterDispatcher &NewLine; &lt&semi;&sol;filter-class&gt&semi; &NewLine; &lt&semi;&sol;filter&gt&semi; &NewLine; &lt&semi;filter-mapping&gt&semi; &NewLine; &lt&semi;filter-name&gt&semi;struts2&lt&semi;&sol;filter-name&gt&semi; &NewLine; &lt&semi;url-pattern&gt&semi;&sol;&ast;&lt&semi;&sol;url-pattern&gt&semi; &NewLine; &lt&semi;&sol;filter-mapping&gt&semi; &NewLine; &lt&semi;welcome-file-list&gt&semi; &NewLine; &lt&semi;welcome-file&gt&semi;Login&period;jsp&lt&semi;&sol;welcome-file&gt&semi; &NewLine; &lt&semi;&sol;welcome-file-list&gt&semi; &NewLine;&lt&semi;&sol;web-app&gt&semi; &NewLine;<&sol;pre>&NewLine;<p>Now&comma; right click on the project name and click <b>Export &gt&semi; WAR<&sol;b> File to create a War file&period; Then deploy this WAR in the Tomcat&&num;8217&semi;s webapps directory&period; Finally&comma; start Tomcat server and try to access<&sol;p>&NewLine;<p>URL <i><b>http&colon;&sol;&sol;localhost&colon;8080&sol;doj&sol;Login&period;jsp&period;<&sol;b><&sol;i><&sol;p>&NewLine;<p>This will give you following screen&colon;<&sol;p>&NewLine;<div class&equals;"separator" style&equals;"clear&colon; both&semi; text-align&colon; center&semi;"><img src&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;wp-content&sol;uploads&sol;2013&sol;07&sol;6666&period;png" border&equals;"0" &sol;><&sol;div>&NewLine;<&sol;div>&NewLine;<p><b>Enter a wrong user name and password&period; You should see the next page-<&sol;b><&sol;p>&NewLine;<div class&equals;"separator" style&equals;"clear&colon; both&semi; text-align&colon; center&semi;"><img src&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;wp-content&sol;uploads&sol;2013&sol;07&sol;77777&period;png" border&equals;"0" &sol;><&sol;div>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;<&sol;div>&NewLine;<p><b>Now enter &&num;8220&semi;<i>Dinesh<&sol;i>&&num;8221&semi; as user name and &&num;8220&semi;<i>Sweety<&sol;i>&&num;8221&semi; as password&period; You should see the next page-<&sol;b><&sol;p>&NewLine;<div class&equals;"separator" style&equals;"clear&colon; both&semi; text-align&colon; center&semi;"><img src&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;wp-content&sol;uploads&sol;2013&sol;07&sol;8888-1&period;png" border&equals;"0" &sol;><&sol;div>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;<&sol;div>&NewLine;<h2><b>Download Source Code&plus;Libs<&sol;b><br &sol;>&NewLine;<b><a href&equals;"https&colon;&sol;&sol;sites&period;google&period;com&sol;a&sol;dineshonjava&period;com&sol;dineshonjava&sol;dineshonjava&sol;Struts2Database&period;zip&quest;attredirects&equals;0&amp&semi;d&equals;1" target&equals;"&lowbar;blank" rel&equals;"noopener">Struts2Database&period;zip<&sol;a><&sol;b><&sol;h2>&NewLine;<p>&nbsp&semi;<&sol;p>&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;struts-2-file-upload-example&sol;">Previous<&sol;a> &lt&semi;&lt&semi;   &vert;&vert; <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;struts-2-baby-step-to-learn&sol;">Index <&sol;a>&vert;&vert;   &gt&semi;&gt&semi;<a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;struts-2-sending-mails&sol;">Next<&sol;a> &gt&semi;&gt&semi;<&sol;b><&sol;div>&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;struts-2-file-upload-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;struts-2-sending-mails&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; '384'&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