2015-04-02 8 views
0

приложений context.xmlDataSource стал нулевым во время инъекции EmployeeDAO

<bean id="dataSource1" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 
    <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/> 
    <property name="url" value="jdbc:oracle:thin:@localhost:1521:orcl"/> 
    <property name="username" value="scott"/> 
    <property name="password" value="tiger"></property> 
</bean> 
<bean id="employeeDAO1"  class="com.santosh.spring.dao.impl.EmployeeDAOImpl"> 
    <constructor-arg ref="dataSource1" index="0" ></constructor-arg> 
</bean> 

<bean id="employeeService" class="com.santosh.spring.service.impl.EmployeeServicesImpl"> 
    <property name="employeeDao" ref="employeeDAO1"></property> 
</bean> 

В этом EmployeeDAOImpl классе dataSource стал нулевым, поэтому я не могу подключиться к базе данных:

EmployeeDAOImpl класса

public class EmployeeDAOImpl implements EmployeeDAO{ 

    private DataSource dataSource; 
    public EmployeeDAOImpl(DataSource dataSource){ 
     dataSource=this.dataSource; 
     System.out.println("Getting DataSourse"); 
     System.out.println(dataSource); 
    } 

    @Override 
    public double getSalary(int empno) { 

     Connection connection=null; 
     try{ 
      /*Class.forName("oracle.jdbc.driver.OracleDriver"); 
      connection=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl", "scott", "tiger");*/ 
      String GET_SALARY_BY_EMPNO="SELECT SAL FROM EMPLOYEE WHERE ENO=?"; 
      //getting the connection 
      //connection=dataSource.getConnection(); 
      System.out.println("Connection is-:"+connection); 
      System.out.println("Connection-:"+connection); 
      PreparedStatement preparedStatement=connection.prepareStatement(GET_SALARY_BY_EMPNO); 
      preparedStatement.setInt(1, empno); 
      ResultSet resultSet=preparedStatement.executeQuery(); 
      if(resultSet.next()){ 
       return resultSet.getDouble(1); 
      } 
      throw new RuntimeException("Employee Not Found "); 
     } 
     catch(RuntimeException runtimeExp){ 
      System.out.println(runtimeExp); 
     }//catch 
     catch(Exception e){ 
      e.printStackTrace(); 
      System.out.println(e); 
     }//catch 
     finally{ 
      try{ 
       connection.close(); 
      } 
      catch(Exception exception){ 

      } 
     }//finally 
     return 0; 

    }//getSal 

EmployeeServiceImpl класс

public class EmployeeServicesImpl implements EmployeeServices { 

    private EmployeeDAO employeeDao; 

    public EmployeeDAO getEmployeeDao() { 
     return employeeDao; 
    } 

    public void setEmployeeDao(EmployeeDAO employeeDao) { 
     this.employeeDao = employeeDao; 
    } 

    @Override 
    public boolean incrementSalary(int empno, double salary) { 
     double esalary=employeeDao.getSalary(100); 
     System.out.println("Employee Current Salary-:"+esalary); 
     return true; 
    } 

Пока я инъекционный dataSource в мой EmployeeDAO, я получаю нуль в качестве источника данных, так что я не подключен к базе данных. Я получаю выход как:

связи IS-: нулевой
Подключение-: нуль
java.lang.NullPointerException

+0

код, как y ou вызывающ dao пожалуйста? – SMA

ответ

0

Попробуйте использовать

class="org.apache.commons.dbcp.BasicDataSource" 

вместо

class="org.springframework.jdbc.datasource.DriverManagerDataSource" 
Смежные вопросы