在课程

时间:2016-01-20 19:12:42

标签: java getter-setter

emplyeer.java

package sibintergartionp;

public class emplyeer {

    private int emp_id;  
    private String name;  
    private String address;
    private String desig;
    emplyeer empr=new emplyeer();
    public void setemp_id(int i) {
        // TODO Auto-generated method stub
        emp_id=i;
    }
    public void setname(String string) {
        // TODO Auto-generated method stub
        name=string;
    }
    public void setaddress(String string) {
        // TODO Auto-generated method stub
        address=string;
    }
    public void setdesig(String string) {
        // TODO Auto-generated method stub
        desig=string;
    }

}

emplyee.hbm.xml

<?xml version='1.0' encoding='UTF-8'?>  
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"  
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">   
<hibernate-mapping>  
<class name="sibintergartionp.emplyeer" table="emp">    
           <id name="emp_id">  
          <generator class="assigned"></generator>  
          </id>
            <property name="name"></property> 
            <property name="address"></property>
            <property name="desig"></property>
</class>              
</hibernate-mapping>  

testclass.java

package sibintergartionp;
import org.springframework.beans.factory.BeanFactory;  
import org.springframework.beans.factory.xml.XmlBeanFactory;   
import org.springframework.core.io.ClassPathResource;  
import org.springframework.core.io.Resource;  
public class testclass {  
    public static void main(String[] args) {  
        Resource r=new ClassPathResource("context.xml");  
        BeanFactory factory=new XmlBeanFactory(r);  
        employeedao dao=(employeedao)factory.getBean("d");  
        emplyeer e=new emplyeer();  
        e.setemp_id(114);  
        e.setname("varun");  
        e.setaddress("Vastra`enter code here`pur");  
        e.setdesig("Analyst");  
        dao.saveEmployee(e);  
    }  
}  

context.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"></property>  
        <property name="url" value="jdbc:mysql://localhost:3306/mysql"></property>  
        <property name="username" value="root"></property>  
        <property name="password" value="root1234"></property>
        <property name="initialSize" value="1000" />
        <property name="maxActive" value="25" />  
    </bean>  

    <bean id="mysessionFactory"  class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">  
        <property name="dataSource" ref="dataSource"></property>  

        <property name="mappingResources">  
        <list>  
        <value>emplyee.hbm.xml</value>  
        </list>  
        </property>  

        <property name="hibernateProperties">  
            <props>  
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>  
                <prop key="hibernate.hbm2ddl.auto">update</prop>  
                <prop key="hibernate.show_sql">true</prop>  

            </props>  
        </property>  
    </bean>  

    <bean id="template" class="org.springframework.orm.hibernate3.HibernateTemplate">  
    <property name="sessionFactory" ref="mysessionFactory"></property>  
    </bean>  

    <bean id="d" class="sibintergartionp.employeedao">  
    <property name="template" ref="template"></property>  
    </bean>  


    </beans>  

错误:

  

org.apache.commons.dbcp.SQLNestedException:无法加载JDBC驱动程序   class&#39; com.mysql.jdbc.driver&#39;

     

引起:java.lang.ClassNotFoundException:com.mysql.jdbc.driver

     

线程中的异常&#34; main&#34;   org.springframework.beans.factory.BeanCreationException:错误   创建名称为&#39; d&#39;在类路径资源中定义   [context.xml]:无法解析对bean&#39; template&#39;的引用而   设置bean属性&#39;模板&#39 ;;嵌套异常是   org.springframework.beans.factory.BeanCreationException:错误   创建名称&#39;模板&#39;在类路径资源中定义   [context.xml]:无法解析对bean&#39; mysessionFactory&#39;的引用   设置bean属性&#39; sessionFactory&#39 ;;嵌套异常是   org.springframework.beans.factory.BeanCreationException:错误   用名称&#39; mysessionFactory&#39;创建bean在类路径中定义   resource [context.xml]:init方法的调用失败;嵌套   异常是org.hibernate.PropertyNotFoundException:找不到   在类sibintergartionp.emplyeer

中获取emp_id的getter

1 个答案:

答案 0 :(得分:0)

您的驱动程序名称错误:

<property name="driverClassName"  value="com.mysql.jdbc.driver"></property>  

应该是

<property name="driverClassName"  value="com.mysql.jdbc.Driver"></property>