Online Training Spring Hibernate Training in Delhi R Programming Training Course Big Data and Hadoop Training Java Training

Struts 2 MySQL

In this section, You will learn to connect the MySQL database with the struts 2 application.


Login form in Struts2 version 2.3.16 Struts 2 Login Application Struts 2 Hello World Annotation Example Struts 2 Tutorial: Easy steps to learn Struts 2 Struts2 Application in Eclipse: Running the application in Eclipse IDE Struts 2 Hello World Application using Eclipse Interceptors in Struts 2 Struts 2 Action Tag Struts 2 in Agile Development Environment Struts 2 UI Tags Generic Tags Creating own Interceptor What are Interceptors in Struts 2 and how do they work? Results Value Stack / OGNL Action in Struts 2 Framework Struts 2 Interceptors Struts Maven - How to use Maven dependency of Struts Download Struts How Struts 2 Framework works? Architecture of Struts 2 Framework Features and Enhancements in Struts Struts 2 Tutorials - Struts version Struts Flow Diagram Step By Step Struts Tutorial Struts 2 Login Form Example Struts 2 Hello World Example Introduction to Struts 2 Framework Downloading and Installing Struts 2.3.8 Struts 2.3.8 released with new features Struts 2.3.8 Tutorials and Examples Subset Tag Actionerror Tag Struts 2 Autocompleter,Struts 2 Autocomplete Generator Tag Struts 2 Application Struts 2 zero configuration,Struts 2 zero configuration Example Static Parameter, Static Parameter in Struts 2, Struts 2 Static Parameter Pagination Example Struts 2.2.1 Title Struts2 ElseIf Tag Example Struts2 Else Tag Example Struts2 If,Else,ElseIf Tag Example Struts2 tag refrences and Example How to download, compile and test the tutorials using ant. Struts 2.1.8 Login Form Struts 2.1.8 Hello World Example Download Struts 2.1.8, Downloading and installing Struts 2.1.8 Showcase application

Struts 2 MySQL


In this section, You will learn to connect the MySQL database with the struts 2 application.

Follow the following steps to connect with MySQL database:







Step 1: Create the struts.xml file and add the following xml snippet in the struts.xml file.

<?xml version="1.0" encoding="UTF-8" ?>
  "-//Apache Software Foundation//DTD Struts 
  Configuration 2.0//EN"



  <!-- Rose India Struts Tutorials -->
  <constant name="struts.enable.DynamicMethodInvocation" 
"false" />

  <constant name="struts.devMode" value="true" />
<include file="struts-default.xml"/>  

  <!-- Add packages here -->

  <package name="roseindia" namespace="/roseindia" 

<!-- inserting data into data base through JDBC -->

  <action name="insert">

  <action name="insertData" class="net.roseindia.insert">
  <result  name="error">/pages/insertData.jsp</result>


Step 2 : Create an input jsp form.


<%taglib prefix="s" uri="/struts-tags" %>
<title>Struts Insert Data Application!</title>

<link href="<s:url value="/css/main.css"/>" rel="stylesheet"


<s:form action="insertData" method="POST" validate="true">

<td colspan="2">
Please enter


<s:actionerror />
<s:fielderror />

<s:textfield name="username" label="User Name"/>
<s:password name="password" label="Password"/>
<s:submit value="Save" align="center"/>




Step 3 : Create an Action class.

First, Establish a connection with the MySQL Database with the help of MySQL driver (""). 
Now, Make an account in the MySQL database to get connected with the database. 

After establishing a connection,  you can  retrieve, insert and update data to the MySQL database table. 

The following action class establishes a connection with MySQL database with the help of appropriate type of methods and API interfaces. If connection is established then the entered data is added to the MySQL  database table otherwise it displays an error message.

package net.roseindia;
import com.opensymphony.xwork2.ActionSupport;
import java.util.Date;
import java.sql.*;

 <p> Validate a user login. </p>
public  class insert  extends ActionSupport {
  public String execute() throws Exception {
  String url = "jdbc:mysql://localhost:3306/";
  String dbName = "taskproject";
  String driverName = "";
  String userName = "root";
  String password = "root";
  Connection con=null;
  Statement stmt=null;
  con=DriverManager.getConnection(url+dbName, userName, 
  catch(Exception e){
  String uname=getUsername();
  String pws=getPassword();
  stmt = con.createStatement();
  int val = stmt.executeUpdate("INSERT employee VALUES

  if(val == 0){
  return ERROR;
  return SUCCESS;
  // ---- Username property ----

 <p>Field to store User username.</p>
  private String username = null;

 <p>Provide User username.</p>
 @return Returns the User username.
  public String getUsername() {
  return username;

 <p>Store new User username</p>
 @param value The username to set.
  public void setUsername(String value) {
  username = value;

  // ---- Username property ----

 <p>Field to store User password.</p>
  private String password = null;

 <p>Provide User password.</p>
 @return Returns the User password.
  public String getPassword() {
  return password;

 <p>Store new User password</p>
 @param value The password to set.
  public void setPassword(String value) {
  password = value;


Description of the code:

This is an interface in  java.sql package that specifies establishing connection with the specific database like: MySQL, Ms-Access, Oracle etc and java files. The SQL statements are executed within the context of the Connection interface.

Class.forName(String driver):
This method is static. It attempts to load the class dynamically and returns class instance and takes string type value (driver) when it matches with the class with given string.

It is a class of java.sql package that controls a set of JDBC drivers. Each driver has to be registered with this class.

getConnection(String url, String userName, String password):
This method establishes a connection to specified database url. It takes three string types of arguments like: 

    url: - Database url to link with
  userName: - User name of database
  password: -Password of database

This method is used for disconnecting the connection. It frees all the resources occupied by the database.

Step 4 : Create the validator

The validation.xml format is either <ActionClassName>-validation.xml or <ActionClassName>-<ActionAliasName>-validation.xml.


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE validators PUBLIC 
  "-//OpenSymphony Group//XWork Validator 1.0.2//EN" 
  <field name="username">
  <field-validator type="requiredstring">
  <param name="trim">true</param>
  <message>User name is required</message>
  <field name="password">
  <field-validator type="requiredstring">
  <param name="trim">true</param>
  <message>Password is required</message>

When entered the correct data in the text field the user gets the insertSuccess.jsp page  displaying the entered data. 




<title>Inserted Data List</title>


<b> Inserted Data: </b>
<b>User name = </b><%=request.getParameter("username"%>!
<b>Password = </b><%=request.getParameter("password"%>!



When this application executes the user gets the following:

Without filling fields and click "Save" button, you will get  the output page as :

If you fill only the "Password" field and  click "Save" button without filling the next fields, you will get  the output page as :

If you fill only the "User Name" field and  click "Save" button without filling the next fields, you will get  the output page as :

If you fill both field:

Then you get:




Share on Google+Share on Google+

Struts 2 MySQL

Posted on: December 18, 2008 If you enjoyed this post then why not add us on Google+? Add us to your Circles



Discuss: Struts 2 MySQL   View All Comments

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
April 5, 2011
where is a "web.xml" file

we know that a web xml file is must to run strus where it is....define
July 13, 2012
struts with jdbc

hi i am begginer for struts ... and i want to create the page of the sruts +jdbc+jsp page in which two radio button,two check field 1 dropdown and submit button this task given by company on monday i have to show ..... please fulfill my requirement urgently
August 29, 2012
remove the girl image from this site

remove the girl image from this site.
September 10, 2012
Unable to locate action mapping for request

I want to write a simple form submission program with struts2 and liferay portlet that I got this error in action path: ERROR [render_portlet_jsp:154] Unable to locate action mapping for request, probably due to an invalid action path: /view/formSubmission - [unknown location can anybody help me?