CREATE TABLE [UserDetails](
[UserId] int IDENTITY(1,1) PRIMARY KEY,
[FirstName] [varchar](500) NULL,
[LastName] [varchar](500) NULL,
[EmailId] [varchar](500) NULL,
[MobileNo] [int] NULL,
)
DBconnection.java
package connection.com;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class DBconnection {
public static Connection getConnection(){
Connection con = null;
try{
//Class.forName("oracle.jdbc.driver.OracleDriver");
//con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","username","password");
//Class.forName("com.mysql.jdbc.Driver");
// con=DriverManager.getConnection("jdbc:mysql://localhost:3306/databasename","username","password");
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
con=DriverManager.getConnection("jdbc:sqlserver://localhost:53245;"+"databaseName=java9r;user=sa;password=password");
}catch(SQLException e){
e.printStackTrace();
}catch(Exception e){
e.printStackTrace();
}
return con;
}
}
UserBean.java
package userbean.com;
public class UserBean {
private String fname,lname,email;
private int mobile, userid;
public String getFname() {
return fname;
}
public void setFname(String fname) {
this.fname = fname;
}
public String getLname() {
return lname;
}
public void setLname(String lname) {
this.lname = lname;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public int getMobile() {
return mobile;
}
public void setMobile(int mobile) {
this.mobile = mobile;
}
public int getUserid() {
return userid;
}
public void setUserid(int userid) {
this.userid = userid;
}
}
UserController.java
package java9r.com.user.controller;
import java9r.com.user.dao.UserDAO;
import com.opensymphony.xwork2.ActionSupport;
import userbean.com.UserBean;
public class UserController extends ActionSupport {
private static final long serialVersionUID = -6239703166715154765L;
private UserDAO ub=new UserDAO();
private UserBean urb=new UserBean();
public String adduser(){
this.ub.adduser(this.urb);
return SUCCESS;
}
public UserDAO getUb() {
return ub;
}
public void setUb(UserDAO ub) {
this.ub = ub;
}
public UserBean getUrb() {
return urb;
}
public void setUrb(UserBean urb) {
this.urb = urb;
}
}
UserDAO.java
package java9r.com.user.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import connection.com.DBconnection;
import userbean.com.UserBean;
public class UserDAO {
public void adduser(UserBean urb) {
try {
Connection con=DBconnection.getConnection();
String sql="insert into UserDetails (FirstName,LastName,EmailId,MobileNo) values(?,?,?,?)";
PreparedStatement ps=con.prepareStatement(sql);
ps.setString(1, urb.getFname());
ps.setString(2, urb.getLname());
ps.setString(3, urb.getEmail());
ps.setInt(4, urb.getMobile());
ps.executeUpdate();
} catch (SQLException e) {
System.err.println(e.getMessage());
}
}
}
struts.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<constant name="struts.enable.DynamicMethodInvocation" value="false"/>
<constant name="struts.devMode" value="false"/>
<constant name="struts.multipart.saveDir" value="/tmp"> </constant>
<constant name="struts.action.extension" value=","/>
<package name="abc" namespace="/" extends="struts-default" >
<action name="adduser" class="java9r.com.user.controller.UserController" method="adduser">
<result name="success" >UserDetailsList.jsp</result>
</action>
</package>
</struts>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>Struts2Ajax</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
Index.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html ">
<html>
<head>
<title>User</title>
<link rel="stylesheet" href="css/bootstrap.min.css" >
<script src="js/bootstrap.min.js"></script>
<!-- Ajax start -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js" ></script>
<script type="text/javascript">
$(document).ready(function() {
$('#ibuttonuser').click(function(e) {
e.preventDefault();
var firstname = jQuery("#urb.fname").val();
var lastname = jQuery("#urb.lname").val();
var mailid = jQuery("#urb.email").val();
var mobileno = jQuery("#urb.mobile").val();
$.ajax({
url: "adduser",
// type: "post",
data: "fname=" + firstname+"&lname"+lastname+"&email"+mailid+"&mobile"+mobileno,
cache: false,
success: function(data) {
$("#usermessage").html(data).slideDown('slow');
}
});
});
});
</script>
<!-- Ajax End -->
</head>
<body background="java9r.png" marginheight="100px;">
<div class="container">
<br>
<form action="adduser" method="post">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title">Add New User Using Ajax </h4>
</div>
<div class="modal-body">
<div class="form-group">
<label for="usr">First Name:</label>
<input type="text" name="urb.fname" class="form-control" >
</div>
<div class="form-group">
<label for="usr">Last Name:</label>
<input type="text" name="urb.lname" class="form-control" >
</div>
<div class="form-group">
<label for="usr">Mobile No:</label>
<input type="text" name="urb.mobile" class="form-control" >
</div>
<div class="form-group">
<label for="usr">Email Id:</label>
<input type="text" name="urb.email" class="form-control" >
</div>
</div>
<div class="modal-footer">
<div id="usermessage" align="center"></div>
<input type="submit" class="btn btn-default" id="ibuttonuser" value="Save">
<!-- here calling ajax id id="ibuttonuser" -->
<button type="reset" class="btn btn-default" data-dismiss="modal">Reset</button>
</div>
</div>
</form>
</div>
</body>
</html>
success.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Success</title>
</head>
<body>
<h3>Success Fully Added Record</h3>
</body>
</html>
OUTPUT
0 comments:
Post a Comment