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);
}
}
===============================================================================================
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);
}
}
===============================================================================================
No comments:
Post a Comment