Friday 30 March 2018

DataBase connectivity with JSTL-JSP-(JAVA SERVER PAGES)


DATABSE CONNECTIVITY WITH JSTL-JSP  (JAVA SERVER PAGES)

Postgres jar file is required
jstl.jar file is required

========================================================================
jstl sql tag demonstration........
<sql:setDataSource>
<sql:query>
<sql:update>
<sql:param>
<sql:dateParam>
<sql:transaction>

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

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
    <%@taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql" %>
<!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>

<sql:setDataSource var="ds" driver="org.postgresql.Driver" url="jdbc:postgresql://localhost:5433/java"
user="postgres" password="manager"/>
<%--retrieve all row from table in jstl jsp --%>
<sql:query  dataSource="${ds}" var="result">
select * from user1234
</sql:query>
<table border="2" width="100%"> 
<tr> 
<th>UserName</th> 
<th>Phone</th> 
<th>Emailid</th> 
<th>Uid</th>
<th>Status</th>
</tr> 
<h1>
<c:forEach items="${result.rows}" var="row">
<tr><td>${row.uname}</td><td>${row.phone}</td><td>${row.email}</td><td>${row.uid}</td><td>${row.status}</td></tr>
</c:forEach>
</h1>
</table>

<%--insert row into a table in jstl jsp --%>
<sql:update dataSource="${ds}" var="count">
insert into user1234 values('sushant','6767543433','sushant@gmail.com','78675su','y')
</sql:update>
<h1>The number of rows inserted:${count}<br></h1>


<%--delete row from table in jstl jsp --%>
<sql:update dataSource="${ds}" var="count">
delete from user1234 where uid='45698sa'
</sql:update>
<h1>The number of rows deleted:${count}<br></h1>


<%-- update row into a table in jstl jsp --%>
<sql:update dataSource="${ds}" var="count">
update user1234 set uname='manikumar' where uid='96969ma'
</sql:update>
<h1>The number of rows updated:${count}<br></h1>



<sql:update dataSource="${ds}" var="count">
update user1234 set uname=?where uid=?
<sql:param value="vikash"/>
<sql:param value="72345di"/>
</sql:update>
<h1>The number of rows updated:${count}<br></h1>

<sql:transaction dataSource="${ds}">

<sql:update>
update user1234 set uname='shanshay' where uid='9678hm';
</sql:update>

<sql:update>
update user1234 set uname='ragini' where uid='76545kp';
</sql:update>
</sql:transaction>
</body>
</html>


o/p view:-



The number of rows inserted:1

The number of rows deleted:1

The number of rows updated:1

The number of rows updated:1

Wednesday 28 March 2018

JDBC-JAVA DATABASE CONNECTIVITY WITH POSTGRESQL DATABASE
CURD-OPERATION & BATCH UPDATION
NOTE:-JAR FILE IS REQUIRD
================================================================================================
//UserBean.java
//UserBo.java
// Udto.java
//ConnectionUtility.java
//UserDao .java(InsertData(),update(),deleteData(),fetchData(),batchUpdate())
//GovindClient.java
//GovindUpdate .java
//GovindFetch.java
//GovindDelete.java
//GovindBatchUpdate.java

================================================================================================
// UserBean .java
package govind.bean;

public class UserBean {

private String uname;
private String phone;
private String email;
public String getUname() {
return uname;
}
public void setUname(String uname) {
this.uname = uname;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}


}


================================================================================================
//UserBo.java
package govind.bo;

import govind.bean.UserBean;
import govind.dao.UserDao;
import govind.dto.Udto;

public class UserBo {
boolean b=false;
public boolean setUserBo(UserBean ub)
{

String uname=ub.getUname().substring(0, 1).toUpperCase()+
ub.getUname().substring(1).toLowerCase();
String phone = (ub.getPhone().length()==10)?ub.getPhone():"";
String email = ub.getEmail();
String[] em = email.split("@");
String mail=(em[1].equals("gmail.com")?ub.getEmail():"invalid email");
String uid=ub.getPhone().substring(5)+ub.getUname().substring(0, 2);
                    Udto udto=new Udto();
                    udto.setUname(uname);
                    udto.setPhone(phone);
                    udto.setEmail(mail);
udto.setUid(uid);
                    return new UserDao().insertData(udto);


}

}


================================================================================================
// Udto.java
package govind.dto;

public class Udto {

private String uname;
private String phone;
private String email;
private String uid;
public String getUname() {
return uname;
}
public void setUname(String uname) {
this.uname = uname;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getUid() {
return uid;
}
public void setUid(String uid) {
this.uid = uid;
}


}

================================================================================================
//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/java", "postgres", "manager");

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

                   public static Connection getConnection() 
{
return con;
}

                    }
======================================================================================================
//UserDao .java(InsertData(),update(),deleteData(),fetchData(),batchUpdate())

//Insert operation
package govind.dao;
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.dto.Udto;

public class UserDao {

boolean f=false;

public boolean insertData(Udto udto)
{
try
{
Connection con = ConnectionUtility.getConnection();
PreparedStatement ps = con.prepareStatement("insert into user123 values(?,?,?,?,?)");
ps.setString(1, udto.getUname());
ps.setString(2, udto.getPhone());
ps.setString(3, udto.getEmail());
ps.setString(4, udto.getUid());
ps.setString(5, "y");
int i = ps.executeUpdate();
System.out.println(i);
if(i>0)
{
f=true;
}
    }
    catch (Exception e) 
    {
e.printStackTrace();

     }
     return f;
    }

===============================================================================================
//update email  & phone on the basis of uid .....



                public int update(String uid,String email,String phone)
{

try
{
Connection con = ConnectionUtility.getConnection();
PreparedStatement ps = con.prepareStatement("update user123 set email=?,phone=?where uid=?");
ps.setString(1, email);
ps.setString(2, phone);
ps.setString(3, uid);
   int i=ps.executeUpdate();
    return i;
    }
    catch (Exception e) {
e.printStackTrace();
    }
   return 0;
        }


public int update(String uid,String str)
{
PreparedStatement ps=null;

try
{
Connection con = ConnectionUtility.getConnection();
if(str.length()==10)
{
ps = con.prepareStatement("update user123 set phone=? where uid=?" );
    }
else
{
ps = con.prepareStatement("update user123 set email=? where uid=?");
}
ps.setString(1, str);
ps.setString(2, uid);
int i=ps.executeUpdate();
return i;
   }
    catch (Exception e) {
e.printStackTrace();
   }
  return 0;
   }

================================================================================================
//fetch all record
//fetch single record on the basis of uid


                   public List<Udto> fetchData(String uid)
{
List<Udto> list=new ArrayList<Udto>();
PreparedStatement ps=null;
try
{
Connection con = ConnectionUtility.getConnection();
    String query="select * from user123";
    if(uid.equalsIgnoreCase("all"))
    {
    ps=con.prepareStatement(query);
    }
    else
    {
    ps=con.prepareStatement(query+" "+"where uid=?");
    ps.setString(1, uid);
    }
    ResultSet rs = ps.executeQuery();
   while(rs.next())
    {
    Udto udto=new Udto();
    udto.setUname(rs.getString(1));
    udto.setPhone(rs.getString(2));
    udto.setEmail(rs.getString(3));
    udto.setUid(rs.getString(4));
    list.add(udto);
    }
    }
catch (Exception e) {
e.printStackTrace();
}
return list;
    }

================================================================================================      
//delete on the basis of uid......................              
    public int deleteData(String uid)
                   {
try
{
Connection con = ConnectionUtility.getConnection();
PreparedStatement ps = con.prepareStatement("delete from user123 where uid=?");
ps.setString(1, uid);
int i=ps.executeUpdate();
return i;
}
catch (Exception e) {
e.printStackTrace();
}
return 0;


                    }

================================================================================================
//Batch updation and data stored in other table(user1234)

public int batchUpdate(List<Udto> list)
{
try
{
    Connection con = ConnectionUtility.getConnection();
    PreparedStatement ps = con.prepareStatement("insert into user1234 values(?,?,?,?,?)");
//int i=0;
    for(Udto udto:list)
{
ps.setString(1, udto.getUname());
ps.setString(2, udto.getPhone());
ps.setString(3, udto.getEmail());
ps.setString(4, udto.getUid());
ps.setString(5, "y");
ps.addBatch();
//i+=ps.executeUpdate();
Thread.sleep(8000);
         }

        int[] x = ps.executeBatch();
        int t=x.length;
        return t;

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


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

//GovindClient.java

package govind.client;

import govind.bean.UserBean;
import govind.bo.UserBo;

public class GovindClient {
public static void main(String[] args) {
UserBean ub=new UserBean();
ub.setUname("govindkhan");
ub.setPhone("9039456100");
ub.setEmail("govindkhan@gmail.com");
UserBo ubo=new UserBo();
if(ubo.setUserBo(ub))
{
System.out.println("insert successfully");
}
else
{
System.out.println("Not Insert Successfully");
}

}

}
===============================================================================================
//GovindUpdate .java
package govind.client;

import govind.dao.UserDao;

public class GovindUpdate {
public static void main(String[] args) {
UserDao ud=new UserDao();
/*if(ud.update("56100go", "khan.big.heart@gmail.com", "9898989898")==1)
{
System.out.println("Update Successfully");
}
else
{
System.out.println("Sorry Not update Successfully");
}*/


/*if(ud.update("56100", "govind.big.heart@gmail.com")==1)
{
System.out.println("Update Successfully");
}
else
{
System.out.println("Sorry Not update Successfully");
}*/


if(ud.update("56100go", "9443934523")==1)
{
System.out.println("Update Successfully");
}
else
{
System.out.println("Sorry Not update Successfully");
}



}
}
===============================================================================================
//GovindFetch.java
package govind.client;

import java.util.Iterator;
import java.util.List;

import govind.dao.UserDao;
import govind.dto.Udto;

public class GovindFetch {
public static void main(String[] args) {
UserDao ud=new UserDao();
List<Udto> list = ud.fetchData("all");
//List<Udto> list = ud.fetchData("56100go");
System.out.println(list.size());
                     Iterator<Udto> it = list.iterator();
                     while (it.hasNext()) {
Udto udto = it.next();
System.out.println(udto.getUname()+" "+udto.getPhone()+" "+udto.getEmail()+" "+udto.getUid());

}
}

}
================================================================================================
//GovindDelete.java
package govind.client;

import govind.dao.UserDao;

public class GovindDelete {
public static void main(String[] args) {

UserDao ud=new UserDao();
int i = ud.deleteData("56100go");
if(i>0)
{
System.out.println("Delete Data Successfully");
}
else
{
System.out.println("Data Not Deleted Successfully");
}

}

}

================================================================================================
//GovindBatchUpdate.java
package govind.client;

import java.util.List;

import govind.dao.UserDao;
import govind.dto.Udto;

public class GovindBatchUpdate {
public static void main(String[] args) {

UserDao udao=new UserDao();
List<Udto> list = udao.fetchData("all");
for(Udto ud:list)
{
System.out.println(ud.getUname()+" "+ud.getPhone()+" "+ud.getEmail()+" "+ud.getUid());
}
int i = udao.batchUpdate(list);
System.out.println("Batch update successfully"+i);

}

}

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