Java

Saturday, 7 April 2012

Sprng with DAO module(example programs by madhav)


Sprng with DAO  module(example programs by madhav)

Ex1:
This below example program will describes soing simple DML operation on data.


Dept.java

package madhav;

public class Dept implements java.io.Serializable{
public int deptno;
public String dname,loc;
}

DeptDao.java

package madhav;

public interface DeptDao {

void changeLocation(int dno,String loc);

void removeDept(int dno);

}

DeptDaoImpl.java

package madhav;
import org.springframework.jdbc.core.*;
import java.util.*;
public class DeptDaoImpl implements DeptDao {
JdbcTemplate jt;
public DeptDaoImpl(JdbcTemplate jt)
{
this.jt=jt;
}
public void changeLocation(int dno,String loc)
{
      jt.update("update dept set loc=? where deptno=?",
                  new Object[]{loc,new Integer(dno)});
}
public void removeDept(int dno)
{
      jt.update("delete from dept where deptno="+dno);
}
}

Spring.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"
      xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">

<bean id="datasource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName">
<value>sun.jdbc.odbc.JdbcOdbcDriver</value>
</property>
<property name="url">
<value>jdbc:odbc:goddati</value>
</property>
<property name="username">
<value>system</value>
</property>
<property name="password">
<value>chiruta</value>
</property>
</bean>
<bean id="jdbctemp" class="org.springframework.jdbc.core.JdbcTemplate">
<constructor-arg>
<ref local="datasource"/>
</constructor-arg>
</bean>
<bean id="deptdao" class="madhav.DeptDaoImpl">
<constructor-arg>
<ref local="jdbctemp"/>
</constructor-arg>
</bean>
</beans>


DeptDaoTestCase.java

import madhav.*;
import org.springframework.beans.factory.*;
import org.springframework.beans.factory.xml.*;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.*;
import org.apache.commons.dbcp.BasicDataSource;
public class DeptDaoTestCase {

           
           
            public static void main(String[] args) throws Exception {
                       
                       
                        DeptDao deptDao;
                        ApplicationContext beanfactory;
                       
                        beanfactory=new ClassPathXmlApplicationContext("spring.xml");
                         deptDao = (DeptDao)beanfactory.getBean("deptdao");

            System.out.println("testing change loc");
            deptDao.changeLocation(40, "hyderabad");
            System.out.println("location has been changed");

            System.out.println("testing romove dept");
            deptDao.removeDept(10);
            System.out.println("dept no has been changed");
           
}
}


Ex2:
This program will descrtibe inserting a record  by using PrepareStatementCreator object.


Dept.java

package madhav;

public class Dept implements java.io.Serializable{
public int deptno;
public String dname,loc;
}

DeptDao.java

package madhav;

public interface DeptDao {

public void changeLocation(int dno,String loc);

public void removeDept(int dno);
public int createDept(Dept d);

}

DeptDaoImpl.ajva

package madhav;
import org.springframework.jdbc.core.*;
import java.util.*;
public class DeptDaoImpl implements DeptDao {
JdbcTemplate jt;
public DeptDaoImpl(JdbcTemplate jt)
{
this.jt=jt;
}
public void changeLocation(int dno,String loc)
{
            jt.update("update dept set loc=? where deptno=?",
                                    new Object[]{loc,new Integer(dno)});
}
public void removeDept(int dno)
{
            jt.update("delete from dept where deptno="+dno);
}
public int createDept(Dept d)
{
            MyPreparedStatementCreater psc=new MyPreparedStatementCreater(d);
            return jt.update(psc);
           
}
}

MyPreparedStatementCreater.java

package madhav;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import org.springframework.jdbc.core.*;
import java.sql.*;

public class MyPreparedStatementCreater implements PreparedStatementCreator {
      Dept d;
      public MyPreparedStatementCreater(Dept d)
      {
            this.d=d;
      }

      public PreparedStatement createPreparedStatement(Connection con)
                  throws SQLException {
            // TODO Auto-generated method stub
            PreparedStatement ps=con.prepareStatement("insert into dept values(?,?,?)");
            ps.setInt(1,d.deptno);
            ps.setString(2,d.dname);
            ps.setString(3,d.loc);
            return ps;
      }

}

Sprng .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"
      xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">

<bean id="datasource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName">
<value>sun.jdbc.odbc.JdbcOdbcDriver</value>
</property>
<property name="url">
<value>jdbc:odbc:goddati</value>
</property>
<property name="username">
<value>system</value>
</property>
<property name="password">
<value>chiruta</value>
</property>
</bean>
<bean id="jdbctemp" class="org.springframework.jdbc.core.JdbcTemplate">
<constructor-arg>
<ref local="datasource"/>
</constructor-arg>
</bean>
<bean id="deptdao" class="madhav.DeptDaoImpl">
<constructor-arg>
<ref local="jdbctemp"/>
</constructor-arg>
</bean>
</beans>

DeptDaoTestCase.java

import madhav.*;
import org.springframework.beans.factory.*;
import org.springframework.beans.factory.xml.*;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.*;
import org.apache.commons.dbcp.BasicDataSource;
public class DeptDaoTestCase {

           
            public static void main(String[] args) throws Exception {
                       
                       
                        try
                        {
                        DeptDao deptDao;
                        ApplicationContext beanfactory;
                       
                        beanfactory=new ClassPathXmlApplicationContext("spring.xml");
                         deptDao = (DeptDao)beanfactory.getBean("deptdao");

                         
    Dept d=new Dept();
    d.dname="newdept";
    d.loc="mig";
    d.deptno=70;
    System.out.println("i am trying to insert dept vlaues");
    deptDao.createDept(d);
    System.out.println("record is inserted");
                        }
                        catch(Exception e)
                        {
                                    e.printStackTrace();
                        }
}
}

                                                                                 

Ex3:
This program will describes to execute sql select quiries( by using ResultSetExtractor interface object).

Dept.java

package madhav;

public class Dept implements java.io.Serializable{
public int deptno;
public String dname,loc;
}

DeptExtractor.java

package madhav;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.*;
import java.sql.*;
import java.util.*;

public class DeptExtractor implements ResultSetExtractor {

      public Object extractData(ResultSet rs) throws SQLException,
                  DataAccessException {
            // TODO Auto-generated method stub
           
           
            while(rs.next())
            {
                 
                  System.out.print("=" +rs.getInt(1));
                  System.out.print("=" +rs.getString(2));
                  System.out.print("=" + rs.getString(3));
                  System.out.println("================");
                 
            }
            return null;
      }

}
Testing.java

package madhav;
import org.springframework.jdbc.core.*;

public class Testing {
      public JdbcTemplate jdbcTemplate;
      public Testing(JdbcTemplate jdbcTemplate)
      {
      this.jdbcTemplate=jdbcTemplate;
      }
      public void extract()
      {
            String sql="select * from dept";
      DeptExtractor  dx=new DeptExtractor();
      jdbcTemplate.query(sql, dx);
      }

}
Spring.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"
      xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">

<bean id="datasource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName">
<value>sun.jdbc.odbc.JdbcOdbcDriver</value>
</property>
<property name="url">
<value>jdbc:odbc:goddati</value>
</property>
<property name="username">
<value>system</value>
</property>
<property name="password">
<value>chiruta</value>
</property>
</bean>
<bean id="jdbctemp" class="org.springframework.jdbc.core.JdbcTemplate">
<constructor-arg>
<ref local="datasource"/>
</constructor-arg>
</bean>
<bean id="testing" class="madhav.Testing">
<constructor-arg>
<ref local="jdbctemp"/>
</constructor-arg>
</bean>
</beans>

DeptExtractorTestCase.java

import madhav.Testing;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;


public class DeptExtractorTestCase {

      /**
       * @param args
       */
      public static void main(String[] args) {
            // TODO Auto-generated method stub
            try
            {
            Testing t;
            ApplicationContext beanfactory;
           
            beanfactory=new ClassPathXmlApplicationContext("spring.xml");
             t = (Testing)beanfactory.getBean("testing");
             System.out.println("i am trying to extract data");
             t.extract();
             System.out.println("extraction is over");
            }
            catch(Exception e)
            {
                  e.printStackTrace();
            }
      }

}

Ex4:
This example program will describes getting data by using RowCallbackHandler interface ojbect.

Dept.java

package madhav;

public class Dept implements java.io.Serializable{
public int deptno;
public String dname,loc;
}


DeptResults.java


            package madhav;

import java.sql.ResultSet;
import java.sql.SQLException;

import org.springframework.jdbc.core.RowCallbackHandler;

public class DeptResults implements RowCallbackHandler{

      public void processRow(ResultSet rs) throws SQLException {
            // TODO Auto-generated method stub
            System.out.print("=" +rs.getInt(1));
            System.out.print("=" +rs.getString(2));
            System.out.println("=" +rs.getString(3));
            System.out.println("----------");
      }

}




Testing.java

            package madhav;
import org.springframework.jdbc.core.*;

public class Testing {
      public JdbcTemplate jdbcTemplate;
      public Testing(JdbcTemplate jdbcTemplate)
      {
      this.jdbcTemplate=jdbcTemplate;
      }
      public void extract()
      {
            String sql="select * from dept";
      //DeptExtractor  dx=new DeptExtractor();
            DeptResults dr=new DeptResults();
      jdbcTemplate.query(sql, dr);
      }
}

Spring.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"
      xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">

<bean id="datasource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName">
<value>sun.jdbc.odbc.JdbcOdbcDriver</value>
</property>
<property name="url">
<value>jdbc:odbc:goddati</value>
</property>
<property name="username">
<value>system</value>
</property>
<property name="password">
<value>chiruta</value>
</property>
</bean>
<bean id="jdbctemp" class="org.springframework.jdbc.core.JdbcTemplate">
<constructor-arg>
<ref local="datasource"/>
</constructor-arg>
</bean>
<bean id="testing" class="madhav.Testing">
<constructor-arg>
<ref local="jdbctemp"/>
</constructor-arg>
</bean>
</beans>




DeptExtractorTestCase.java

import madhav.Testing;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;


public class DeptExtractorTestCase {

      /**
       * @param args
       */
      public static void main(String[] args) {
            // TODO Auto-generated method stub
            try
            {
            Testing t;
            ApplicationContext beanfactory;
           
            beanfactory=new ClassPathXmlApplicationContext("spring.xml");
             t = (Testing)beanfactory.getBean("testing");
             System.out.println("i am trying to extract data");
             t.extract();
             System.out.println("extraction is over");
            }
            catch(Exception e)
            {
                  e.printStackTrace();
            }
      }

}



Ex5:

This program will describes calling stored procedures.