Tuesday, 3 April 2018

//java database connectivity with Java Server Pages Technology(JSP)
                                          CURD-OPERATION

===============================================================================================
//ConnectionUtility.java
//UserBean.java
//UserDao.java
//index.jsp
//adduserform.jsp
//userform.html
//adduser.jsp
//addusersuccess.jsp
//addusererror.jsp
//viewuserform.jsp
//editform.jsp
//deleteform.jsp
//edituser.jsp

Note:-Jstl.jar file is required
postgresql jar file is required
===============================================================================================
//ConnectionUtility.java
package govind.connectionutility;

import java.sql.Connection;
import java.sql.DriverManager;

public class ConnectionUtility {

private ConnectionUtility()
{

}

static Connection con=null;
static
{
try
{
Class.forName("org.postgresql.Driver");
    con = DriverManager.getConnection("jdbc:postgresql://localhost:5433/govind", "postgres", "manager");
}
catch (Exception e)
{
e.printStackTrace();
}
}


public static Connection getconnection()
{
return con;
}

}
================================================================================
// UserBean.java
package govind.userbean;

public class UserBean {

public int id;
public String name,password,email,sex,country;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}


}
===============================================================================
// UserDao.java
package govind.userdao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import govind.connectionutility.ConnectionUtility;
import govind.userbean.UserBean;

public class UserDao {

public static int save(UserBean ub)
{
int x=0;
try
{
Connection con = ConnectionUtility.getconnection();
PreparedStatement ps = con.prepareStatement("insert into curdjsp(name,password,email,sex,country)values(?,?,?,?,?)");
    ps.setString(1, ub.getName());
    ps.setString(2, ub.getPassword());
    ps.setString(3, ub.getEmail());
    ps.setString(4, ub.getSex());
    ps.setString(5, ub.getCountry());
    x=ps.executeUpdate();
}
    catch (Exception e)
{
e.printStackTrace();
}
return x;

}
===============================================================================================
public static int update(UserBean ub)
{
int x=0;
try
{
Connection con = ConnectionUtility.getconnection();
PreparedStatement ps = con.prepareStatement("update curdjsp set name=?,password=?,email=?,sex=?,country=? where id=?");
ps.setString(1, ub.getName());
ps.setString(2, ub.getPassword());
ps.setString(3, ub.getEmail());
ps.setString(4, ub.getSex());
ps.setString(5, ub.getCountry());
ps.setInt(6, ub.getId());

x=ps.executeUpdate();

}
catch (Exception e) {
e.printStackTrace();
}
return x;
}
==============================================================================================
public static int delete(UserBean ub)
{
int x=0;
try
{
Connection con = ConnectionUtility.getconnection();
PreparedStatement ps = con.prepareStatement("delete from curdjsp where id=?");
ps.setInt(1, ub.getId());
    x=ps.executeUpdate();
}
catch (Exception e) {
e.printStackTrace();
}
return x;
}


==============================================================================================

public static List<UserBean> getAllRecords()
{
List<UserBean> list=new ArrayList<UserBean>();
try
{
Connection con=ConnectionUtility.getconnection();
PreparedStatement ps = con.prepareStatement("select * from curdjsp");
ResultSet rs = ps.executeQuery();
while(rs.next())
{
UserBean ub=new UserBean();
ub.setId(rs.getInt(1));
ub.setName(rs.getString(2));
ub.setPassword(rs.getString(3));
ub.setEmail(rs.getString(4));
ub.setSex(rs.getString(5));
ub.setCountry(rs.getString(6));
list.add(ub);
}
}
catch (Exception e) {
e.printStackTrace();
}
return list;
}
===================================================================================================
public static UserBean getRecordById(int id)
{
UserBean ub=new UserBean();
try
{
Connection con = ConnectionUtility.getconnection();
PreparedStatement ps = con.prepareStatement("select * from curdjsp where id=?");
ps.setInt(1, id);
ResultSet rs = ps.executeQuery();
if(rs.next())
{
ub.setId(rs.getInt(1));
ub.setName(rs.getString(2));
ub.setPassword(rs.getString(3));
ub.setEmail(rs.getString(4));
ub.setSex(rs.getString(5));
ub.setCountry(rs.getString(6));

}
}
catch (Exception e) {
e.printStackTrace();
}
return ub;
}



}

================================================================================================
//index.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>Jsp Curd Example</title>
</head>
<body bgcolor="#E6E6FA" text="black">
<h1><center>JSP CURD EXAMPLE</center></h1>
<h1><center><a href="adduserform.jsp">Add User</a></center></h1>
<h1><center><a href="viewuserform.jsp">View Users</a></center></h1>
</body>
</html>
================================================================================
//adduserform.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>Insert title here</title>
</head>
<body bgcolor="#E6E6FA" text="whilte"">
<jsp:include page="userform.html"></jsp:include>
</body>
</html>

================================================================================

//userform.html
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<h1><center><a href="viewuserform.jsp">View All Records</a><br/></h1></center>
<body bgcolor="#E6E6FA" text="whilte">
<h1><center>Add New User</center></h1>
<form action="adduser.jsp" method="post">
<table border="1" align="center">
<tr><td>Name</td><td><input type="text" name="name"></td></tr>
<tr><td>Password</td><td><input type="password" name="password"></td></tr>
<tr><td>Email</td><td><input type="email" name="email"></td></tr>
<tr><td>Sex</td><td>
<input type="radio" value="male" name="sex" checked="checked">Male
<input type="radio" value="female" name="sex">Female</td></tr>
<tr><td>Country</td><td>
<select name="country">
<option>Select</option>
<option>India</option>
<option>Pakistan</option>
<option>Srilanka</option>
<option>Bangladesh</option>
<option>USA</option>
<option>Southafrica</option>
<option>Other</option>
</select></td></tr>
<tr><td><input type="submit" value="Add User"></td></tr>
</table>
</form>
</body>
</html>


================================================================================
//adduser.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
    <%@page import="govind.userdao.UserDao" %>
   <jsp:useBean id="ub" class="govind.userbean.UserBean"></jsp:useBean>
   <jsp:setProperty property="*" name="ub"/>
<!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>Insert title here</title>
</head>
<body>
<%
int x=UserDao.save(ub);

if(x>0)
{
response.sendRedirect("addusersuccess.jsp");
}
else
{
response.sendRedirect("addusererror.jsp");
}

%>

</body>
</html>

================================================================================
//addusersuccess.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>Add User Success</title>
</head>
<body>
<h1><p><center>Add User Successfully</center></p></h1>
<jsp:include page="userform.html"></jsp:include>
</body>
</html>
=================================================================================
//addusererror.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>Add User Error</title>
</head>
<body >
<h1><center><p>Sorry an, errror occured!</p></center></h1>
<jsp:include page="userform.html"></jsp:include>
</body>
</html>
=================================================================================
//viewuserform.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
 <%@ page import="govind.userdao.UserDao,govind.userbean.*,java.util.*" %>

 <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!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>View Users</title>
</head>
<body>
<h1><center>User List</center></h1>
<%
List<UserBean> list=UserDao.getAllRecords();
request.setAttribute("li", list);
%>
<html>
<body  bgcolor="#E6E6FA">
<table border="1" width="100%">
<tr><th>Id</th><th>Name</th><th>Password</th><th>Email</th><th>Sex</th><th>Country</th>
<th>edit</th><th>delete</th></tr>
<c:forEach items="${li}" var="u">
<tr><td>${u.id}</td><td>${u.name}</td><td>${u.password}</td><td>${u.email}</td><td>${u.sex}</td><td>${u.country}</td>
<td><a href="editform.jsp?id=${u.id}">Edit</a></td>   
<td><a href="deleteform.jsp?id=${u.id}">Delete</a></td></tr>
</c:forEach>
</table>
<h1><center><a href="adduserform.jsp">Add New User</a></center></h1>
</body>
</html>
================================================================================
//editform.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
       
    <%@page import="govind.userdao.UserDao" %>
    <jsp:useBean id="u" class="govind.userbean.UserBean"></jsp:useBean>
    <jsp:setProperty property="*" name="u"/>
<!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>Insert title here</title>
</head>
<body>
<%
int x=UserDao.update(u);
response.sendRedirect("viewuserform.jsp");
%>
</body>
</html>
================================================================================

//deleteform.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
    <%@ page import="govind.userdao.UserDao" %>
    <jsp:useBean id="u" class="govind.userbean.UserBean"></jsp:useBean>
    <jsp:setProperty property="*" name="u"/>
<!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>delete user</title>
</head>
<body>


<%
UserDao.delete(u);
response.sendRedirect("viewuserform.jsp");
%>

</body>
</html>

=================================================================================
//edituser.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
       
    <%@page import="govind.userdao.UserDao" %>
    <jsp:useBean id="u" class="govind.userbean.UserBean"></jsp:useBean>
    <jsp:setProperty property="*" name="u"/>
<!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>Insert title here</title>
</head>
<body>
<%
int x=UserDao.update(u);
response.sendRedirect("viewuserform.jsp");
%>
</body>
</html>
================================================================================











No comments:

Post a Comment