0

I am very new on Java Web. I just try to do simple login page on maven mvc. But I got this error:

org.springframework.beans.factory.BeanCreationException: Error creating bean with 
name 'dataSource' defined in ServletContext resource 
[/WEB-INF/mvc-dispatcher-servlet.xml]: Error setting property values; nested 
exception is org.springframework.beans.PropertyBatchUpdateException; nested 
PropertyAccessExceptions (1) are: PropertyAccessException 1:
 org.springframework.beans.MethodInvocationException: Property 'driverClassName' 
threw exception; nested exception is java.lang.IllegalStateException: Could not 
load JDBC driver class [oracle.jdbc.OracleDriver] at 
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues

Web.xml is:

<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
      http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
     version="2.5">


<display-name>Archetype Created Web Application</display-name>

  <servlet>
    <servlet-name>mvc-dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>

  <servlet-mapping>
    <servlet-name>mvc-dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>

  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value>
  </context-param>


  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
</web-app>

And mvc-dispatcher-servlet.xml:

<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:context="http://www.springframework.org/schema/context"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/tool"
   xmlns:cache="http://www.springframework.org/schema/cache"
   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 http://www.springframework.org/schema/tool http://www.springframework.org/schema/tool/spring-tool.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd">

<context:component-scan base-package="com.deniz.controller.*" />
<context:component-scan base-package="com.deniz.dao.*" />
<context:component-scan base-package="com.deniz.mapper.*" />

<bean
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix">
        <value>/WEB-INF/pages/</value>
    </property>
    <property name="suffix">
        <value>.jsp</value>
    </property>
</bean>

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="oracle.jdbc.OracleDriver"></property>
    <property name="url" value="jdbc:oracle:thin:@//IP_ADRESS:1521/SERVICE_NAME"></property>
    <property name="password" value="scott"></property>
    <property name="username" value="PASS"></property>
</bean>

My UserDAOImpl class is:

 package com.deniz.dao;
import com.deniz.mapper.UsersMapper;
import com.deniz.model.Users;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.support.JdbcDaoSupport;

import javax.sql.DataSource;
import java.util.List;

public class UsersDAOImpl extends JdbcDaoSupport implements UsersDAO {

    @Autowired
    public UsersDAOImpl(DataSource dataSource) {
        this.setDataSource(dataSource);
        System.out.println("oluştu");
    }

    public Users findUserInfo(String userName) {
        String sql = "Select u.Username,u.Password "//
                + " from Users u where u.Username = ? ";

        Object[] params = new Object[] { userName };
        UsersMapper mapper = new UsersMapper();
        try {
            Users user = this.getJdbcTemplate().queryForObject(sql, params, mapper);
            return user;
        } catch (EmptyResultDataAccessException e) {
            return null;
        }
    }

    @Override
    public List<String> list(){
        String sql = "Select username from users";

        Object[] params = new Object[] { "buffer" };

        List<String> roles = this.getJdbcTemplate().queryForList(sql,params, String.class);

        return roles;
    }

}

My UsersDAO Interface:

 package com.deniz.dao;

import com.deniz.model.Users;
import java.util.List;

public interface UsersDAO {

    public Users findUserInfo(String userName);

    public List<String> list();
}

And lastly my MvcConfiguration.java is config class. And here it is:

    package com.deniz.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

import javax.sql.DataSource;

@Configuration
@ComponentScan(basePackages="com.deniz.*")
@EnableTransactionManagement
@PropertySource("classpath:datasource-cfg.properties")
@EnableWebMvc
public class MvcConfiguration extends WebMvcConfigurerAdapter{

    @Autowired
    private Environment env;
    @Bean
    public ViewResolver getViewResolver(){
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setPrefix("/WEB-INF/pages/");
        resolver.setSuffix(".jsp");
        return resolver;
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
    }

    @Bean(name = "dataSource")
    public DataSource getDataSource() {
        DriverManagerDataSource dataSource = new DriverManagerDataSource();

        // See: datasouce-cfg.properties
        dataSource.setDriverClassName(env.getProperty("ds.database-driver"));
        dataSource.setUrl(env.getProperty("ds.url"));
        dataSource.setUsername(env.getProperty("ds.username"));
        dataSource.setPassword(env.getProperty("ds.password"));

        System.out.println("## getDataSource: " + dataSource);

        return dataSource;
    }

}

I know I just copy paste all my code and its dirty work. Sorry for that. But I need to learn develop a maven spring mvc project with oracle connection. Your help will be precious. I also want to complete example work with oracle tables. I really search google but there is not much.(All of these example with mysql. And when I try to convert them with oracle connection I got errors too. I really tired.)

Thank you

Antiokhos
  • 2,944
  • 5
  • 23
  • 32
  • Why don't you divide process into chunks instead of implementing all things at once. – Pratik Ambani Mar 27 '17 at 23:26
  • Have you added the oracle library to your classpath as mentioned here? http://stackoverflow.com/questions/17907863/spring-jdbc-could-not-load-jdbc-driver-class-oracle-jdbc-driver-oracledriver – Pete Mar 28 '17 at 00:26

1 Answers1

0

Put ojdbc6.jar in apache tomcat installation directory in the lib directory

d:\apache tomcat server\Tomcat 8.0\lib

It solved my problem.

L_J
  • 2,351
  • 10
  • 23
  • 28