Spring Framework

Spring Projects

Spring Project 1

adplus-dvertising
Example of Without using template insert, update, delete and select data
Previous Home Next

Introduction: This example provide the difference between template design pattern and simple design pattern to insert , update, delete and select all data from the data base.

Technology use to run this source code

  1. Spring 2.5 jar files
  2. Eclipse IDE
  3. Tomcat Server

Source Code:

ConnectionProvider.java

package org.r4r;
import java.sql.Connection;
import java.sql.DriverManager;
public class ConnectionProvider {
public static Connection  getConnection()
{
Connection con=null;
try{
Class.forName("com.mysql.jdbc.Driver");
con=DriverManager.getConnection
("jdbc:mysql://localhost/tableone","root","root");
}catch(Exception e)
{
System.out.println(e);
}
return con;
}
}

Emp.java

package org.r4r;
public class Emp {
int id;
String name,job;
int salary;
public Emp() {
super();
// TODO Auto-generated constructor stub
}
public Emp(int id, String name, String job, int salary) {
super();
this.id = id;
this.name = name;
this.job = job;
this.salary = salary;
}
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 getJob() {
return job;
}
public void setJob(String job) {
this.job = job;
}
public int getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}
}

EmpDao.java

package org.r4r;
import java.sql.*;
import java.util.Scanner;
public class EmpDao {
public void save(Emp emp){
try{
Connection con=ConnectionProvider.getConnection();
PreparedStatement stmt=con.prepareStatement
("insert into emp values(?,?,?,?)");
stmt.setInt(1, emp.getId());
stmt.setString(2, emp.getName());
stmt.setString(3, emp.getJob());
stmt.setInt(4, emp.getSalary());
stmt.executeUpdate();
con.close();
}catch(Exception e){
System.out.println(e);
}
}
public void update(Emp emp){
try{
Connection con=ConnectionProvider.getConnection();
Scanner in=new Scanner(System.in);
System.out.println("Enter the id:");
int id=in.nextInt();
in.nextLine();
System.out.println("Enter the name:");
String name=in.nextLine();
System.out.println("Enter the job:");
String job=in.nextLine();
System.out.println("Enter the salary:");
int salary=in.nextInt();
in.nextLine();
PreparedStatement stmt=con.prepareStatement
("update emp set name=?, job=?, salary=? where id=?");
stmt.setInt(4, id);
stmt.setString(1, name);
stmt.setString(2, job);
stmt.setInt(3, salary);
stmt.executeUpdate();
con.close();
}catch(Exception e){
System.out.println(e);
}
}
public void delete(Emp emp){
try{
Connection con=ConnectionProvider.getConnection();
Scanner in=new Scanner(System.in);
System.out.println("Enter the id:");
int id=in.nextInt();
PreparedStatement stmt=con.prepareStatement
("delete from emp where id=?");
stmt.setInt(1, id);
stmt.executeUpdate();
con.close();
}catch(Exception e){
System.out.println(e);
}
}
}

InsertTest.java

package org.r4r;
import java.util.Scanner;
public class InsertTest {
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
EmpDao dao=new EmpDao();
while(true){
System.out.println("Enter the id:");
int id=in.nextInt();
in.nextLine();
System.out.println("Enter the name:");
String name=in.nextLine();
System.out.println("Enter the job:");
String job=in.nextLine();
System.out.println("Enter the salary:");
int salary=in.nextInt();
in.nextLine();
Emp emp=new Emp(id,name,job,salary);
dao.save(emp);
System.out.println("Insert continue write yes/no:");
String reg=in.nextLine();
if(reg.equals("no"))
break;
}
System.out.println("You are successfully insert data...");
      }
}

UpdateTest.java

package org.r4r;
public class UpdateTest {
public static void main(String[] args) {
System.out.println("Update Emp Object.....");
EmpDao dao=new EmpDao();
Emp emp=new Emp();
dao.update(emp);
System.out.println("You are successfully update");
}
}

DeleteTest.java

package org.r4r;
public class DeleteTest {
public static void main(String[] args) {
System.out.println("Deleted Emp Object.....");
EmpDao dao=new EmpDao();
Emp emp=new Emp();
dao.delete(emp);
System.out.println("You are successfully delete");
}
}

output:

InsertTest

Enter the id:
1005
Enter the name:
Mukund Singh
Enter the job:
Java Developer
Enter the salary:
25000
Insert continue write yes/no:
no
You are successfully insert data...

UpdateTest

Update Emp Object.....
Enter the id:
1005
Enter the name:
Bal Mukund
Enter the job:
Spring Develo
Enter the salary:
30000
You are successfully update

DeleteTest

Deleted Emp Object.....
Enter the id:
1005
You are successfully delete
Previous Home Next