I am trying an example to connect to database using jdbc and spring and getting the below exception. Please advise.
exception:
Aug 17, 2017 10:44:51 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@6422b8ff: startup date [Thu Aug 17 22:44:51 IST 2017]; root of context hierarchy
Aug 17, 2017 10:44:51 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [spring.xml]
Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'daoClass' is defined
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:638)
at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1159)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:282)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:200)
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:979)
at org.manjosh.main.JdbcMain.main(JdbcMain.java:14)
main class:
package org.manjosh.main;
import org.manjosh.dao.DaoClass;
import org.manjosh.model.Circle;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class JdbcMain {
public static void main(String[] args){
ApplicationContext ctx = new ClassPathXmlApplicationContext("spring.xml");
DaoClass dao = ctx.getBean("daoClass",DaoClass.class);
Circle circle = dao.getCirle(1);
System.out.println(circle.getName());
}
}
dao class:
package org.manjosh.dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.manjosh.model.Circle;
import org.springframework.stereotype.Component;
@Component
public class DaoClass {
public Circle getCirle(int circleId){
String driver = "oracle.jdbc.driver.OracleDriver";
Connection conn = null;
try{
//step1 load the driver class
Class.forName(driver).newInstance();
//step2 create the connection object
conn =DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","system");
//step3 create the statement object
PreparedStatement stmt=conn.prepareStatement("SELECT * FROM circle where ID =?");
stmt.setInt(1, circleId);
Circle circle = null;
//step4 execute query
ResultSet rs=stmt.executeQuery();
while(rs.next()){
circle = new Circle(rs.getInt(circleId),rs.getString("name"));
}
rs.close();
stmt.close();
return circle;
}
catch(Exception e){
throw new RuntimeException(e);
}
finally {
try{
conn.close();
}
catch(SQLException e){}
}
}
}
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"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:annotation-config/>
<context:component-scan base-package = "org.manjosh.main"/>
</beans>