// Develop a Spring-JDBC program to insert a record into Customer table

Let we will assume that we have customer table that will create in Oracle10gXE database environment.

 

create table customer
(
id varchar2(200),
name varchar2(200),
address varchar2(200)
)



Now in eclipse environment we will develop a spring application:

Files Required

  • Customer.java
  • CustomerDao.java
  • spconfig3.xml
  • SpringJdbcExample1.java

Project Structure:

framework

Customer.java

package java11s;
public class Customer
{

   private int id;
   private String name;
   private String address;


   public Customer(int id, String name, String address)
   {

     this.id = id;
     this.name = name;
     this.address = address;
   }

   public int getId()
   {
     return id;
   }
   public void setId(Integer id)
   {
     this.id=id;
   }


   public String getName()
   {
     return name;
   }
   public void setName(String name)
   {
     this.name=name;
   }

   public String getAddress()
   {
     return address;
   }
   public void setAddress(String address)
   {
     this.address=address;
   }

  }

CustomerDao.java

 

package java11s;

import org.springframework.jdbc.core.JdbcTemplate;

public class CustomerDao {

    private JdbcTemplate ob;
    public void setOb(JdbcTemplate ob) {
        this.ob = ob;
    }

    public int insertCustomer(Customer c) {
        String s = "insert into customer1 values('" + c.getId() + "','" + c.getName() + "','" + c.getAddress() + "')";
        return ob.update(s);
    }
}

spconfig.xml

<< ? xml version = "1.0"
encoding = "UTF-8" ? >
    <
    beans
xmlns = "http://www.springframework.org/schema/beans"
xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance"
xmlns: p = "http://www.springframework.org/schema/p"
xsi: schemaLocation = "http://www.springframework.org/schema/beans
http: //www.springframework.org/schema/beans/spring-beans-3.0.xsd">
    <
    bean id = "id1"
class = "org.springframework.jdbc.datasource.DriverManagerDataSource" >
    <
    property name = "driverClassName"
value = "oracle.jdbc.driver.OracleDriver" / >
    <
    property name = "url"
value = "jdbc:oracle:thin:@localhost:1521:XE" / >
    <
    property name = "username"
value = "system" / >
    <
    property name = "password"
value = "misstapu" / >
    <
    /bean> <
    bean id = "ob"
class = "org.springframework.jdbc.core.JdbcTemplate" >
    <
    name = "dataSource"
ref = "id1" >
    <
    /property> <
    /bean> <
    id = "cdao"
class = "java11s.CustomerDao> <
    name = "ob"
ref = "ob" >
    <
    /property> <
    /bean> <
    /beans>

SpringJdbcExample1.java

  

package java11s;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.lang.*;

public class SpringJdbcExample1 {

    public static void main(String[] args) {

        ApplicationContext ctx = new
        ClassPathXmlApplicationContext("spconfig3.xml");

        Object obj = ctx.getBean("cdao");
        CustomerDao cd = (CustomerDao) obj;

        int k = cd.insertCustomer(new Customer(101, "Sanghamitra", "BBSR"));
        System.out.println(k + "row inserted");

    }
}

OUTPUT
framework


After getting output we will check select * from customer in oracle environment, then we will see a record.


101 Sanghamitra BBSR

Thanks & Regards:
Trilochan Tarai
+91-9439202111

Our Services:

Java Training | Python Training | Machine Learning with Python Training | Deep Learning with Python Training | Data Science with Python Training | Projects

SILAN Technology, BBSR
Call to: 9439202111  |  9658317757  |  0674-2361252

Comment