Cannot create JDBC driver of class '' for connect URL 'null' java.sql.SQLException: No suitable driver

32 Views Asked by At

I am trying to convert a context.xml file to a java class so that I can inject the values for the username and password from my Defaults.properties file. My application works perfectly when the contents are written in XML format, but when I write it as a Java class, it says the connection string is null and that there is no suitable driver.

I've tried creating the class as an @Configuration and defined my .properties file with the @PropertySource annotation. I also defined all the values of my JNDI references into the .properties file. Also manually defined the bean in my AppContext.xml for this application.

Here is the context.xml file I am converting:

<Context>
<WatchedResource>WEB-INF/web.xml</WatchedResouce>
<Manager pathname="" />
<CookieProcessor className="org.apache.tomcat.util.http.LegacyCookieProcessor" />

<Resource
    name="<jndiNameHere>"
    auth="Container"
    type="javax.sql.DataSource"
    maxActive="50"
    maxIdle="15"
    maxWait="50000"
    username="<username>"
    password="<password>"
    driverClassName="com.mysql.cj.jdbc.Driver"
    url="<url goes here>" />
</Context>

Here is my JNDI class:

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jndi.JndiObjectFactoryBean;

import javax.sql.DataSource;

@Configuration
@PropertySource(value="classpath:Defaults.properties")
public class JndiResourceConfig {

    @Value("${db.jndi.name}")
    private String jndiName;

    @Bean
    public DataSource dataSource() {
        JndiObjectFactoryBean jndiObjectFactoryBean = new JndiObjectFactoryBean();
        jndiObjectFactoryBean.setJndiName(jndiName);
        jndiObjectFactoryBean.setResourceRef(true);
        jndiObjectFactoryBean.setProxyInterface(DataSource.class);

        return (DataSource) jndiObjectFactoryBean.getObject();
    }
}

Here is my Defaults.properties with jndi references:

db.jndi.name=<jndiNameHere>
db.jndi.url=<DB url>
db.jndi.username=<username>
db.jndi.password=<password>
db.jndi.auth=Container
db.jndi.type=javax.sql.DataSource
db.jndi.driverClassName=com.mysql.cj.jdbc.Driver
db.jndi.maxActive=50
db.jndi.maxIdle=15
db.jndi.maxWait=5000

I am sure that the class is exposed to the properties file because I am able to inject the values and sout them.

The error I am receiving is:

Cannot create JDBC driver of class '' for connect URL 'null' java.sql.SQLException: No suitable driver

0

There are 0 best solutions below