Announcement Announcement Module
Collapse
No announcement yet.
Error creating bean Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • christopher1234
    started a topic Error creating bean

    Error creating bean

    I am trying to run the roseindia.net example for springframework. Everytime i try to run my application it gives me an error msg:

    Code:
    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataCon' defined in class path resource [com/abc/database/datacon.xml]: Cannot resolve reference to bean 'dataSource' while setting bean property 'dataSource'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [com/abc/database/datacon.xml]: Error setting property values; nested exception is org.springframework.beans.PropertyAccessExceptionsException: PropertyAccessExceptionsException (1 errors); nested propertyAccessExceptions are: [org.springframework.beans.MethodInvocationException: Property 'driverClassName' threw exception; nested exception is org.springframework.jdbc.CannotGetJdbcConnectionException: Could not load JDBC driver class [sun.jdbc.odbc.JdbcOdbcDriver]; nested exception is java.lang.ClassNotFoundException: sun.jdbc.odbc.JdbcOdbcDriver]
    Any clues what i might be doing wrong? I mean i have setter and getter methods for my datasource. here is my configuration file looks like

    Code:
    <?xml version="1.0" encoding="UTF-8"?> 
    
    <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" 
    
    "http://www.springframework.org/dtd/spring-beans.dtd"> 
    
    <beans> 
    
      <bean id="dataSource" 
    
          class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 
    
        <property name="driverClassName"> 
    
           <value>sun.jdbc.odbc.JdbcOdbcDriver</value> 
    
        </property> 
    
        <property name="url"> 
    
           <value>jdbc:odbc:test</value> 
    
        </property> 
    
        <property name="username"> 
    
           <value>root</value> 
    
        </property> 
    
        <property name="password"> 
    
           <value>sql</value> 
    
        </property> 
    
      </bean> 
    
      <bean   id="dataCon"  class="com.abc.database.DataConImpl"> 
    
        <property name="dataSource"> 
    
            <ref local="dataSource"/> 
    
        </property> 
    
      </bean> 
    
    </beans>

  • Suleman Zia
    replied
    yup thanks for the tip jon.

    Leave a comment:


  • Jon Seymour
    replied
    2 things.

    The demonstration you provided only shows that you have included a jdbc.jar called mysql.jar in your build time classpath - this says nothing about whether it is available in the runtime class path.

    In your first report, the NoClassDefFound message referred to the sun odbc JDBC driver *not* the MySQL JDBC driver. Are you intending to use Sun ODBC JDBC driver or MySQL? If you are intending to use MySQL you need to change your bean config to refer to the name of the MySQL driver. Spring may be good, but it can't read your mind!

    jon.

    Leave a comment:


  • christopher1234
    replied
    Thanks rahul. Working fine now ;--). Appreciate helping a newbie --)

    Leave a comment:


  • christopher1234
    replied
    I got it thanks

    Leave a comment:


  • rahulsai
    replied
    Try this:
    in SpringServlet.java
    public class SpringServlet extends ....
    private DataConImpl dataconimpl;
    comment out this:
    /*Resource res = new ClassPathResource("com/abc/database/spring-servlet.xml");
    System.out.println("Xml File Path ");
    BeanFactory factory = new XmlBeanFactory(res);
    System.out.println("Xml file loaded");
    DataCon bean1 = (DataCon)factory.getBean("dataCon");
    System.out.println("After factory.getBean()");
    DataSource ds=bean1.dbCon();
    System.out.println("DataSource loaded");*/

    and JdbcTemplate line would be like
    JdbcTemplate jt = new JdbcTemplate(dataconimpl.dbCon());

    and spring-servlet.xml would be like this:

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
    "http://www.springframework.org/dtd/spring-beans.dtd">
    <beans>
    <bean id="dataSource"
    class="org.springframework.jdbc.datasource.DriverM anagerDataSource">
    <property name="driverClassName">
    <value>sun.jdbc.odbc.JdbcOdbcDriver</value>
    </property>
    <property name="url">
    <value>jdbc:odbc:test</value>
    </property>
    <property name="username">
    <value>root</value>
    </property>
    <property name="password">
    <value>sql</value>
    </property>
    </bean>
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate" >
    <property name="dataSource">
    <ref bean="dataSource"/>
    </property>
    </bean>

    <bean id="springServlet" class="com.abc.database.SpringServlet">
    <property name="dataConnImpl">
    <ref bean="dataCon"/>
    </property>
    </bean>

    <bean id="dataCon" class="com.abc.database.DataConImpl">
    <property name="jdbcTemplate">
    <ref bean="jdbcTemplate"/>
    </property>
    <property name="dataSource">
    <ref bean="dataSource"/>
    </property>
    </bean>
    </beans>

    Leave a comment:


  • christopher1234
    replied
    same thing rahul. No difference.
    Code:
    Error creating bean with name 'dataSource' defined in class path resource [com/abc/database/spring-servlet.xml]: Error setting property values; nested exception is org.springframework.beans.PropertyAccessExceptionsException

    Leave a comment:


  • rahulsai
    replied
    Can you put your jdbc driver jar in WEB-INF/lib folder and then try ?
    Also in DataConImpl.java
    it should be
    public DataSource dataSource;

    public void setDataSource(DataSource dataSource) {

    this.dataSource = dataSource;
    }

    Leave a comment:


  • christopher1234
    replied
    Yea sure. In Eclipse under windows preferences i added the mysql jar file in the classpath. Right click on project properties, under Java Build path i added the variable for mysql jar file.

    I know doing database stuff in servlet is worse. This is just a test application to see if my database connection is working or not.
    Last edited by christopher1234; Dec 21st, 2007, 10:27 AM.

    Leave a comment:


  • Jon Seymour
    replied
    You say the JDBC driver jar is in the classpath. Can you demonstrate this to us? Where is it exactly? If it is in the EAR, does your WAR's META-INF/MANIFEST.MF file reference it?
    Last edited by Jon Seymour; Dec 21st, 2007, 09:41 AM. Reason: META-INF -> META-INF/MANIFEST.MF

    Leave a comment:


  • christopher1234
    replied
    Thanks rahul. Actually i have everything under my DAO classes. Actually my problem is not coding its just the configuration file where i am having problem. Hope to clear this thing soon. Anyways i am still not able to run my code.

    DataCon.java
    Code:
    package com.abc.database;
    
    import javax.sql.DataSource;
    
    public interface DataCon {
    
    	public DataSource dbCon();
    	
    }
    DataConImpl.java
    Code:
    package com.abc.database
    import javax.sql.DataSource;
    
    import org.springframework.jdbc.core.JdbcTemplate;
    
    
    
    public class DataConImpl implements DataCon{
    	
    	public DataSource dataSource;
    	
    	private JdbcTemplate jdbcTemplate;
    	
    	public DataConImpl() {}
    	
    	public void setDataSource(DataSource ds) {
    		
    		dataSource = ds;
    	}
    	
    	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
    		
    		this.jdbcTemplate = jdbcTemplate;
    	}
    	
    	
    	public DataSource dbCon() {
    		// TODO Auto-generated method stub
    		return dataSource;
    	}
    
    	
    	
    }
    SpringServlet.java
    Code:
    import java.io.IOException;
    import java.io.PrintWriter;
    import java.util.Iterator;
    import java.util.List;
    
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import javax.sql.DataSource;
    
    import org.springframework.beans.factory.BeanFactory;
    import org.springframework.beans.factory.xml.XmlBeanFactory;
    import org.springframework.context.ApplicationContext;
    import org.springframework.core.io.ClassPathResource;
    import org.springframework.core.io.Resource;
    import org.springframework.jdbc.core.JdbcTemplate;
    
    /**
     * Servlet implementation class for Servlet: SpringServlet
     *
     */
     public class SpringServlet extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {
      
    	 
    	public SpringServlet() {
    		super();
    	}
    	
    	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    		// TODO Auto-generated method stub
    		doPost(request, response);
    	}  	
    	
    	protected void doPost(HttpServletRequest req, HttpServletResponse response) throws ServletException, IOException {
    		// TODO Auto-generated method stub
    	    response.setContentType("text/html"); 
    
    	      PrintWriter out = response.getWriter(); 
    
    	      String a = req.getParameter("text1"); 
    	      
    	      String d = req.getParameter("combo2"); 
    	      
    	   
    
    	      try 
    
    	      { 
    
    	         System.out.println("Wait..."); 
    
    	         Resource  res = new ClassPathResource("com/abc/database/spring-servlet.xml");
    	         System.out.println("Xml File Path ");
    	         BeanFactory  factory = new   XmlBeanFactory(res); 
    	         System.out.println("Xml file loaded");
    	         DataCon bean1 = (DataCon)factory.getBean("dataCon"); 
    	         System.out.println("After factory.getBean()");
    	          DataSource ds=bean1.dbCon(); 
    	         System.out.println("DataSource loaded");
    	         if(d.equals("find")) {
    	        	
    	        	 List list;
    	        	 JdbcTemplate jt = new JdbcTemplate(ds);
    	            	list = jt.queryForList("select item_no, item_id, item_quantity from item where item_no = '"+a+"' ");
    	        	 Iterator i = list.iterator();
    	        	 while(i.hasNext()) {
    	        		 
    	        		 Object o = i.next();
    	        		 out.println(o.toString());
    	        	 }
    	        	 
    	        	 
    	         }
    	} catch(Exception e) {
    		
    		System.out.println(e.toString());
    		}
    	}
     }
    springservlet.html
    Code:
    <html> 
    
    <body bgcolor="pink"> 
    
    <form    method=post  
    
             action="http://localhost:8080/DatabaseTesting/SpringServlet"> 
    
    Item Id      :  <input type=text name="text1">  <br><br> 
    
    
    
        <select name="combo2" size=1> 
        
             <option value="find">Find 
    
    
        </select>                 <br><br> 
    
        <input type=submit> 
    
    </form> 
    
    </body>
    spring-servlet.xml
    Code:
    <?xml version="1.0" encoding="UTF-8"?> 
    
    <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" 
    
    "http://www.springframework.org/dtd/spring-beans.dtd"> 
    
    <beans> 
    
      <bean id="dataSource" 
    
          class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 
    
        <property name="driverClassName"> 
    
           <value>sun.jdbc.odbc.JdbcOdbcDriver</value> 
    
        </property> 
    
        <property name="url"> 
    
           <value>jdbc:odbc:test</value> 
    
        </property> 
    
        <property name="username"> 
    
           <value>root</value> 
    
        </property> 
    
        <property name="password"> 
    
           <value>sql</value> 
    
        </property> 
    
      </bean> 
    
      	<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate" >
    	<property name="dataSource">
    	<ref bean="dataSource"/>
    	</property>
    	</bean>
    
    	<bean id="dataCon" class="com.abc.database.DataConImpl">
    <property name="jdbcTemplate">
    <ref bean="jdbcTemplate"/>
    </property>
    <property name="dataSource">
    <ref bean="dataSource"/>
    </property>
    </bean>
    </beans>
    Thanks for looking. If someone can point out what am i doing wrong and in which java class i really appreciate that.

    Thank you
    Last edited by christopher1234; Dec 21st, 2007, 09:25 AM.

    Leave a comment:


  • rahulsai
    replied
    Hi Christopher,
    Few checks
    In the DAO (data access class) make sure you have a reference variable of the datasource and jdbctemplate (if using it)
    private DataSource ds;
    protected JdbcTemplate jdbcTemplate;

    public void setDataSource(DataSource ds) {
    this.ds = ds;
    }

    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
    this.jdbcTemplate = jdbcTemplate;
    }



    [/B]

    and you must have this in the spring-servlet.xml
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverM anagerDataSource">
    <property name="driverClassName"><value>com.mysql.jdbc.Drive r</value></property>
    <property name="url">
    <value>jdbc:mysql://localhost:3306/TestDB?autoReconnect=true</value>
    </property>
    <property name="username"><value>root</value></property>
    <property name="password"><value></value></property>
    </bean>


    and if using JdbcTemplate, then this also in the spring-servlet.xml
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate" >
    <property name="dataSource">
    <ref bean="dataSource"/>
    </property>
    </bean>

    and in the DAO Class bean definition in the spring-servlet.xml, the references to JdbcTemplate and DataSource ref beans like shown below:
    <bean id="productManagerDaoJdbc" class="com.springapp.demo.ProductManagerDaoJdbc">
    <property name="jdbcTemplate">
    <ref bean="jdbcTemplate"/>
    </property>
    <property name="dataSource">
    <ref bean="dataSource"/>
    </property>
    </bean>

    Leave a comment:


  • karldmoore
    replied
    It's the message right at the end of the trace that is the cause of the actual problem, it can't create the bean because it can't resolve the dataSource, it can't resolve the dataSource because it can't resolve a property, it can't resolve the property as it can't find the class.

    Leave a comment:


  • christopher1234
    replied
    Thanks for the input. Actually i do have the jar file under my classpath. I dont know why am i still getting that driverclassname error. What about this statement:

    Code:
    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataCon' defined in class path resource [com/abc/database/datacon.xml]: Cannot resolve reference to bean 'dataSource' while setting bean property 'dataSource';

    Leave a comment:


  • Thyme
    replied
    Based on the innermost exception, it looks like you don't have the JDBC driver jar for the JDBC to ODBC connector.

    Leave a comment:

Working...
X