Loading...
Sunday, October 11, 2015

Struts2 Crud Operation Insert , Delete, Update And Retrieve Using Bootstrap And JSTL

Struts2 Crud Operation Insert , Delete, Update, Retrieve  Using Bootstrap


SQL

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 java.util.ArrayList;
import java.util.List;
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 List<UserBean> userlists=new ArrayList<UserBean>();

private UserDAO ub=new UserDAO();
private UserBean urb=new UserBean();

public String alluserlist() throws Exception {
this.userlists=this.ub.getAllUserBean();
return SUCCESS;
}

public String adduser(){

this.ub.adduser(this.urb);
this.userlists=this.ub.getAllUserBean();
this.urb=new UserBean();

return SUCCESS;
}

public String userupdate(){

this.ub.updateUser(this.urb);
this.userlists=this.ub.getAllUserBean();
this.urb=new UserBean();

return SUCCESS;
}


public String userdelete(){

this.ub.deleteUser(urb.getUserid());
this.userlists=this.ub.getAllUserBean();
this.urb=new UserBean();

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;
}

public List<UserBean> getUserlists() {
return userlists;
}

public void setUserlists(List<UserBean> userlists) {
this.userlists = userlists;
}


}



UserDAO.java

package java9r.com.user.dao;
import java.sql.Connection;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;



import connection.com.DBconnection;

import userbean.com.UserBean;
public class UserDAO {

public List<UserBean> getAllUserBean() {
List<UserBean> li = new ArrayList<UserBean>();

try {

Connection con=DBconnection.getConnection();
Statement st=con.createStatement();
String sql="SELECT UserId,FirstName ,LastName ,EmailId,MobileNo FROM UserDetails ";
ResultSet rs = st.executeQuery(sql);
UserBean urb = new UserBean();

while (rs.next()) {

urb = new UserBean();
urb.setUserid(rs.getInt("UserId"));
urb.setFname(rs.getString("FirstName"));
urb.setLname(rs.getString("LastName"));
urb.setEmail(rs.getString("EmailId"));
urb.setMobile(rs.getInt("MobileNo"));
li.add(urb);


}
} catch (SQLException e) {
System.err.println(e.getMessage());
}
return li;
}


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());
}
}


public void updateUser(UserBean urb) {

try {
Connection con=DBconnection.getConnection();
String sql ="update UserDetails set FirstName=? ,LastName=? ,EmailId=?,MobileNo=? where UserId=? ";
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.setInt(5, urb.getUserid());
ps.executeUpdate();

} catch (SQLException e) {
System.err.println(e.getMessage());
}
}

public void deleteUser(int Userid) {

try {
Connection con=DBconnection.getConnection();
String sql="delete from UserDetails where UserId=?";
PreparedStatement ps=con.prepareStatement(sql);
ps.setInt(1, Userid);
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>
<action name="deleteuser" class="java9r.com.user.controller.UserController" method="userdelete">
<result name="success" >UserDetailsList.jsp</result>
</action>

<action name="updateuser" class="java9r.com.user.controller.UserController" method="userupdate">
<result name="success" >UserDetailsList.jsp</result>
</action>


<action name="userslist" class="java9r.com.user.controller.UserController" method="alluserlist" >
<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>Strut2Crud</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>
<meta http-equiv="Refresh" content="1; url=userslist">
<title>User</title>
</head>
<body>

<div align="center" ><h3>Welcome To User </h3></div>


</body>
</html>


UserDetailsList.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html ">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>User Details</title>
<link rel="stylesheet" href="css/bootstrap.min.css" >
<script src="js/jquery.min.js" ></script>
<script src="js/bootstrap.min.js"></script>


</head>
<body background="java9r.png">


<div class="container">

<div align="center"><h3>User Details</h3><hr></div>



<table class="table" >
<tr>
<th><a href="userslist">@</a></th>
<th>User Id</th>
<th>First Name</th>
<th>Last Name </th>
<th>Mobile No</th>
<th>Email Id</th>

<th><button type="button" class="btn btn-info btn-sm" data-toggle="modal" data-target="#NewUser">+</button></th>
</tr>

<c:forEach var="userlists" items="${userlists}" >




<tr>
<td><form action="deleteuser" method="post">
<input type="hidden" name="urb.userid" value="${userlists.userid }" >
<input type="submit" class="btn btn-info btn-sm" value="-" alt="Delete">
</form></td>
<td>${userlists.userid }</td>
<td>${userlists.fname }</td>
<td>${userlists.lname }</td>
<td>${userlists.mobile }</td>
<td>${userlists.email }</td>
<td><button type="button" class="btn btn-info btn-sm" data-toggle="modal" data-target="#Edit${userlists.userid }">Edit</button></td>
</tr>

<!-- Modal -->
<div class="modal fade" id="Edit${userlists.userid }" role="dialog">
<div class="modal-dialog">
<form action="updateuser" method="post">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">&times;</button>
<h4 class="modal-title">Update User Details </h4>
</div>
<div class="modal-body">
<div class="form-group">
<label for="usr">First Name:</label>
<input type="hidden" name="urb.userid" value="${userlists.userid }" id="usr">

<input type="text" name="urb.fname" class="form-control" value="${userlists.fname }" id="usr">
</div>
<div class="form-group">
<label for="usr">Last Name:</label>
<input type="text" name="urb.lname" class="form-control" value="${userlists.lname }" id="usr">
</div>

<div class="form-group">
<label for="usr">Mobile No:</label>
<input type="text" name="urb.mobile" class="form-control" value="${userlists.mobile }" id="usr">
</div>

<div class="form-group">
<label for="usr">Email Id:</label>
<input type="text" name="urb.email" class="form-control" value="${userlists.email }" id="usr">
</div>

</div>
<div class="modal-footer">

<input type="submit" class="btn btn-default" value="Update">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</form>
</div>
</div>
<!-- Modal end -->




</c:forEach>


<!-- Modal Add New User -->
<div class="modal fade" id="NewUser" role="dialog">
<div class="modal-dialog">
<form action="adduser" method="post">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">&times;</button>
<h4 class="modal-title">Add New User Details </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" id="usr">
</div>
<div class="form-group">
<label for="usr">Last Name:</label>
<input type="text" name="urb.lname" class="form-control" id="usr">
</div>

<div class="form-group">
<label for="usr">Mobile No:</label>
<input type="text" name="urb.mobile" class="form-control" id="usr">
</div>

<div class="form-group">
<label for="usr">Email Id:</label>
<input type="text" name="urb.email" class="form-control" id="usr">
</div>

</div>
<div class="modal-footer">

<input type="submit" class="btn btn-default" value="Save">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</form>
</div>
</div>
<!-- Modal end -->

</table>
</div>
</body>
</html>


OUTPUT




0 comments:

Post a Comment

 
TOP