Java

Tuesday, 20 November 2012

struct 2 spring dao







Struct 2 spring  dao:


TUTIORALS FROM MADHAV:
 

     JAVA-SERVLETS     JAVA-JDBC     JAVA-JSP       HIBERNATE-SHCEMABASED 

    SPRING-AOP-ANNOTATIONS      SPRING -DAO     SPRIN-MVC     SPRING-SECUTITY




Spring.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
    <package name="default" extends="struts-default" namespace="/">
       
        <action name="DeptController" class="madhav.controller.HelloWorld">
            <result name="success">Welcome.jsp</result>
        </action>
    </package>
</struts>

                                                                                
Web .xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <filter>
    <filter-name>struts2</filter-name>
    <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
  <welcome-file-list>                  
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>

  </web-app>


applicationContext.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="datasource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName">
<value>com.mysql.jdbc.Driver</value>
</property>
<property name="url">
<value>jdbc:mysql://localhost:3306/mysql</value>
</property>
<property name="username">
<value>root</value>
</property>
<property name="password">
<value>mysql</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.daoimpl.DeptDao">
<constructor-arg>
<ref local="jdbctemp"/>
</constructor-arg>
</bean>

</beans>

Dept.java
package madhav.pojodao;


public class Dept {
      public int deptno;
      public String dname,loc;
      public int getDeptno() {
            return deptno;
      }
      public void setDeptno(int deptno) {
            this.deptno = deptno;
      }
      public String getDname() {
            return dname;
      }
      public void setDname(String dname) {
            this.dname = dname;
      }
      public String getLoc() {
            return loc;
      }
      public void setLoc(String loc) {
            this.loc = loc;
      }
}





DeptDao.java


package madhav.daoimpl;
import madhav.pojodao.*;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.springframework.jdbc.core.*;
import java.util.*;
public class DeptDao {
            JdbcTemplate jt;
            public DeptDao(JdbcTemplate jt)
            {
            this.jt=jt;
            }
           
            public void enterRecord()
            {
                        Dept d=new Dept();
                        d.setDeptno(9);
                        d.setDname("hugotech");
                        d.setLoc("hytech");
                        MyPreparedStatementCreater ms=new MyPreparedStatementCreater(d);
                        jt.update(ms);
                        System.out.println("record in inserted");
            }


           
            private static final 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 department values(?,?,?)");
                        ps.setInt(1,d.deptno);
                        ps.setString(2,d.dname);
                        ps.setString(3,d.loc);
                        return ps;
                  }

            }
}


DeptController.java

package madhav.controller;
import com.opensymphony.xwork2.ActionSupport;
import madhav.daoimpl.*;
import madhav.daoimpl.DeptDao;
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 DeptController extends ActionSupport{
                String message;
    public String getMessage() {
                                return message;
                }

                public void setMessage(String message) {
                                this.message = message;
                }
                public String execute()
   {
                                System.out.println("succces is called hai madhav");
                                 DeptDao deptDao;
                        ApplicationContext beanfactory;
                        beanfactory=new ClassPathXmlApplicationContext("applicationContext.xml");
                        System.out.println("break 1");
                         deptDao = (DeptDao)beanfactory.getBean("deptdao");
                         System.out.println("break 2");
                deptDao.enterRecord();
                System.out.println("record entered successfully");
                  return "success";
   }
}

                                                                                                                                                                                        





OUTPUT:


No comments:

Post a Comment