Hello everybody,
Here I will describe one by one steps to insert data in MySQL Database using struts2 and JAVA. This example project is build using NetBean 6.0. So it is more easy to those who has knowledge to use NetBean IDE.
Add all jar files need for struts and Database. Jar files can be added in two ways:
i) Copy all jar files to lib folder of Tomcat server
ii)RMC (Right Mouse Click) on Libraries folder on your project » Add Jar/Folder »Select jar files or the folder containing jar files
*** Following jar files needed to work with struts2 and MySQL
** commons-beanutils-core.jar
** commons-logging.jar
** struts2-core-2.0.11.jar
** xwork-2.0.4.jar
** ognl-2.6.11.jar
** mysql-connector-java-5.0.7-bin.jar
1.For create just a simple web project:
» New Project » Java Web » Web Application » Give a Project Name (Given SimpleStruts2)
2.Create a struts.xml file in the following way:
» RMC (Right Mouse Click) on in Source Packages » New » XML document » .... » then write the following code:
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="SimpleBrowserPage" extends="struts-default">
<action name="actionName" method="methodInJavaFile" class="com.Test.Action.JavaFileAction">
<result name="success">ForSuccess.jsp</result>
<result name="error">ForError.jsp</result>
</action>
</package>
</struts>
3. Create a form in the index.jsp. Code of index.jsp:
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%--This line used for struts--%>
<!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>JSP Page</title>
</head>
<body>
<h1>Please insert Name and Email address</h1>
<form action="actionName.action" method="post">
Name:<input type="text" size="30" name="name"/><br/>
Email:<input type="text" size="30" name="email"/><br/>
<input type="submit" value="Submit"/>
</form>
</body>
</html>
4.Create a ForSuccess.jsp .Code of ForSuccess.jsp :
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%--This line used for struts--%>
<!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>JSP Page</title>
</head>
<body>
<h1>Insertion Success</h1>
Thank you, Mr. <s:property value="name"/>
</body>
</html>
5.Create a ForError.jsp .Code of ForError.jsp :
<%--
Document : ForError
Created on :
Author : shahalom
--%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%--This line used for struts--%>
<!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>JSP Page</title>
</head>
<body>
<h1> Error:: <s:property value="%{message}"/></h1>
<h2>Please insert Name and Email address</h2>
<form action="actionName.action">
Name:<input type="text" size="30" name="name"/><br>
Email:<input type="text" size="30" name="email"/><br>
<input type="submit" value="Submit"/><br>
</form>
</body>
</html>
6.Create a package for java file :
RMC on Source Packages » New » Java Package » Write com.Test.Action.
7.Create a new Java file (name given ‘JavaFileAction.java’).Write the following code:
package com.Test.Action;
import com.opensymphony.xwork2.Action;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
/**
* @author shahalom
*/
public class JavaFileAction {
private String name;
private String email;
private String message;
public String methodInJavaFile() {
Connection con = null;
Statement stm = null;
try {
if (this.getName().equalsIgnoreCase("") || this.getEmail().equalsIgnoreCase("")) {
throw new Exception();
}
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/dbName", "root", "aaa");
stm = con.createStatement();
String query = "insert into infoTable(name,email) values('" + this.getName() + "','" + this.getEmail() + "');";
System.out.println("****************** " + query);
stm.executeUpdate(query);
stm.close();
con.close();
} catch (Exception e) {
this.setMessage(e.getMessage());
return Action.ERROR;
}
return Action.SUCCESS;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
8. Replace the following code in web.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" 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">
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<session-config>
<session-timeout>30</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
9.Now Configure MySQL 5.1 to port#3306 and Run the following query in MySQL console:
--Create database
create database dbName;
use dbName;
--Create table
create table infoTable (
id int(5) unsigned NOT NULL auto_increment,
name varchar(30),
email varchar(30),
PRIMARY KEY (id));