无法在Spring中连接数据源

时间:2016-12-21 05:11:03

标签: java database spring spring-data datasource

我无法从spring.xml获取数据源连接。并尝试使用@Repository,@ Service进行测试,但它无法正常工作。如何解决此问题?请让我知道如何解决这个问题。感谢

spring.xml

<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.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">
   <context:annotation-config/>

   <context:component-scan base-package="org.minthurein.javabrains" />

   <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" >
     <property name="driverClassName" value="org.postgresql.Driver"></property>
     <property name="url" value="jdbc:postgresql://localhost:5432/db"></property>   
     <property name="username" value="postgres" ></property>
     <property name="password" value="123" ></property>
   </bean>

</beans>

jdbcDaoImpl.java

    import java.sql.Connection;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    import javax.sql.DataSource;
    import org.minthurein.javabrains.model.Circle;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
    @Component
    public class jdbcDaoImpl {
        @Autowired
        private DataSource dataSource; 

        public Circle getCricle(int circleId){
        Connection conn=null;
        Statement stmet=null;
    //  System.out.println("ID"+circleId);

        try{
          conn = dataSource.getConnection();
          System.out.println(conn+">>>>>>>");
          conn.setAutoCommit(false);
          stmet = conn.createStatement();
          String sql="SELECT * FROM circle WHERE ID="+circleId;
          Circle circle=null;   
          ResultSet rs=stmet.executeQuery(sql);
          if(rs.next())
          {
             //  System.out.println(rs.getString("name"));
             circle=new Circle(circleId,rs.getString("name"));
          }
      rs.close();
      stmet.close();
      conn.commit();
      return circle;
    }

错误控制台

enter image description here

0 个答案:

没有答案