0

I have seen other posts related to the issue, but could not seem to understand it and/or apply to my codes to fix the issue. Hence your help will be highly helpful.

Here is the error stack trace:

org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is java.sql.SQLException: Cannot load JDBC driver class 'com.ibm.db2.jcc.DB2Driver'
at org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:80)
at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:627)
at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:692)
at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:724)
at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:734)
at org.springframework.jdbc.core.JdbcTemplate.queryForRowSet(JdbcTemplate.java:899)
at com.cinfin.edocs.services.dao.FormMatrixDaoImpl.validateNonProductionStatusInMatrix(FormMatrixDaoImpl.java:857)
at com.cinfin.edocs.services.dao.FormMatrixDaoImplTest.testValidateNonProductionStatusInMatrix(FormMatrixDaoImplTest.java:36)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:72)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:81)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:216)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:82)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:60)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:67)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:162)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: java.sql.SQLException: Cannot load JDBC driver class 'com.ibm.db2.jcc.DB2Driver'
at org.apache.commons.dbcp2.BasicDataSource.createConnectionFactory(BasicDataSource.java:2001)
at org.apache.commons.dbcp2.BasicDataSource.createDataSource(BasicDataSource.java:1897)
at org.apache.commons.dbcp2.BasicDataSource.getConnection(BasicDataSource.java:1413)
at org.springframework.jdbc.datasource.DataSourceUtils.doGetConnection(DataSourceUtils.java:111)
at org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:77)
... 36 more
Caused by: java.lang.ClassNotFoundException: com.ibm.db2.jcc.DB2Driver
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
at org.apache.commons.dbcp2.BasicDataSource.createConnectionFactory(BasicDataSource.java:1992)
... 40 more

My FormMatrixDaoImpl class:

import javax.sql.DataSource;

@Repository
public class FormMatrixDaoImpl extends NamedParameterJdbcDaoSupport implements FormMatrixDao {

@Autowired  
public FormMatrixDaoImpl(@Qualifier("vitDataSource") DataSource dataSource, @Value("#{edocsQueryMap}") HashMap<String, String> edocsQueryMap) {
    super();

        @Override
    public SqlRowSet validateNonProductionStatusInMatrix(String projectId) throws SQLException {
        logger.info("validateNonProductionStatusInMatrix. projectId: "+projectId);

        SqlRowSet ret = getJdbcTemplate().queryForRowSet(qValidateNonProductionStatusInMatrix, new Object[] {projectId});

        return ret;
    }
}

My FormMatrixDaoImplTest class:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:datasources.xml","classpath:edocservices.xml","classpath:edocservicesclients.xml", "/WEB-INF/edocservices-servlet.xml"})
@ActiveProfiles("local")

public class FormMatrixDaoImplTest {

@Autowired
private FormMatrixDaoImpl formMatrixDaoImpl;
public FormMatrixDaoImpl getFormMatrixDaoImpl() {
    return formMatrixDaoImpl;
}
public void setFormMatrixDaoImpl(FormMatrixDaoImpl formMatrixDaoImpl) {
    this.formMatrixDaoImpl = formMatrixDaoImpl;
}

@Test
public final void testValidateNonProductionStatusInMatrix() throws SQLException {
    SqlRowSet test = formMatrixDaoImpl.validateNonProductionStatusInMatrix("0b003e8880072976");
    assertEquals(test,null);
    }

My datasources.xml file:

<?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:util="http://www.springframework.org/schema/util" 
   xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
     http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsd"> 

<beans profile="production,user,quality,development">         
    <bean id="vitDataSource" class="org.springframework.jndi.JndiObjectFactoryBean" lazy-init="true">
        <description>JNDI VITEDOC DB2 datasource</description>
        <property name="jndiName" value="${database.vitedoc.jndi}"/>        
    </bean>
    <bean id="docDataSource" class="org.springframework.jndi.JndiObjectFactoryBean" lazy-init="true">
        <description>JNDI DOCEDOC DB2 datasource</description>
        <property name="jndiName" value="${database.docedoc.jndi}"/>        
    </bean>

    <beans profile="local">     
    <bean id="vitDataSource" class="org.apache.commons.dbcp2.BasicDataSource" lazy-init="true" destroy-method="close">
        <description>Standalone VITEDOC datasource</description>
        <property name="driverClassName" value="${database.vitedoc.driver.classname}"/>
        <property name="url" value="${database.vitedoc.url}" />
        <property name="username" value="${database.vitedoc.username}"/>
        <property name="password" value="${database.vitedoc.password}"/>
    </bean>
    <bean id="docDataSource" class="org.apache.commons.dbcp2.BasicDataSource" lazy-init="true" destroy-method="close">
        <description>Standalone DOCEDOC datasource</description>
        <property name="driverClassName" value="${database.docedoc.driver.classname}"/>
        <property name="url" value="${database.docedoc.url}" />
        <property name="username" value="${database.docedoc.username}"/>
        <property name="password" value="${database.docedoc.password}"/>
    </bean>
</beans>

My edocservices.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:ws="http://www.springframework.org/schema/web-services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:c="http://www.springframework.org/schema/c"    
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util" 
xmlns:jms="http://www.springframework.org/schema/jms"   
xsi:schemaLocation="http://www.springframework.org/schema/web-services http://www.springframework.org/schema/web-services/web-services.xsd
                    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
                    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsd                       
                    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd                      
                    http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms-4.1.xsd">

The above xml has marshallers, unmarshallers, endpoints & wsdls.

Here is my edocservicesclient.xml file:

<?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:util="http://www.springframework.org/schema/util"     
xmlns:context="http://www.springframework.org/schema/context"   
xmlns:oxm="http://www.springframework.org/schema/oxm"       
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsd       
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
    http://www.springframework.org/schema/oxm http://www.springframework.org/schema/oxm/spring-oxm-4.1.xsd">

<context:property-placeholder location="classpath:edocservices.properties"/>
<context:component-scan base-package="com.cinfin.edocs.services"/>

The above xml also has some other marshallers, unmarshallers and saajmessagefactory.

Here is my edocservices-servlet.xml file:

<?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" xmlns:oxm="http://www.springframework.org/schema/oxm"
xmlns:mvc="http://www.springframework.org/schema/mvc"  xmlns:util="http://www.springframework.org/schema/util"
xmlns:p="http://www.springframework.org/schema/p" xmlns:c="http://www.springframework.org/schema/c"
xmlns:cache="http://www.springframework.org/schema/cache" xmlns:task="http://www.springframework.org/schema/task"               
xsi:schemaLocation="http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd  
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
    http://www.springframework.org/schema/oxm http://www.springframework.org/schema/oxm/spring-oxm-4.1.xsd
    http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd      
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsd
    http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-4.1.xsd
    http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.1.xsd">

<context:property-placeholder location="classpath:edocservices.properties"/>
<context:component-scan base-package="com.cinfin.edocs.services"/>  
<context:annotation-config/>

When I run my FormMatrixDaoImplTest class as JUnit test, I get the could not get jdbc connection error. This error is associated only with JUnit test, and does not appear when the program runs locally. So I have a feeling I might be missing something with the JUnit setting or something like that.

Any help pointing out errors/mistakes in the codes and/or pointing towards other way to test run 'select' command to retrieve data from database will be highly appreciated.

Thank you in advance.

Beetle Pepsi
  • 5
  • 1
  • 3
  • 1
    So, just offhand, it looks like the jar containing your driver class isn't available during testing. Are you using Maven? Gradle? Make sure the scope of the dependency includes "test". – billjamesdev May 19 '15 at 02:34
  • It sounds like you're probably using Spring on WebSphere. The core error in your log is `Cannot load JDBC driver class 'com.ibm.db2.jcc.DB2Driver`. SUGGESTIONS: 1) trace where your JNDI definitions (for example, `vitDataSource`) are defining their driver classnames (like. `database.vitedoc.driver.classname`) 2) Make sure this DB2Driver available as a JDBC resource in your WebSphere Admin console, and/or 3) Make sure the DB2 driver .jar is configured in your pom.xml (if you're using Maven). – paulsm4 May 19 '15 at 02:38
  • Thank you for your responses. We are not using Maven or Hibernate, just Spring. @BillJames How to make the jar containing the driver class available during testing? – Beetle Pepsi May 19 '15 at 02:45
  • @paulsm4: I checked the properties file, the classname is defined as: database.vitedoc.driver.classname=com.ibm.db2.jcc.DB2Driver – Beetle Pepsi May 19 '15 at 02:48
  • Just let us know how you ran this test? – Eranda May 19 '15 at 02:53
  • @Eranda I ran the test using JUnit. The test class is FormMatrixDaoImplTest which tests FormMatrixDaoImpl class. Basically I am trying to see/test the data retrieved from database using the validateNonProductionInMatrix. – Beetle Pepsi May 19 '15 at 02:57
  • What was the command you executed to run this test or did you use the IDE to run it. – Eranda May 19 '15 at 02:58
  • I used Eclipse to run it. Run As>>JUnit Test. – Beetle Pepsi May 19 '15 at 02:59

3 Answers3

0

The Error Cannot load JDBC driver class 'com.ibm.db2.jcc.DB2Driver' states that jar containing class is not available in runtime classpath. make sure the jar containing class is available.

Ref : How to Set Runtime classpath with eclipse

Community
  • 1
  • 1
Akash Yadav
  • 2,411
  • 20
  • 32
  • This totally worked. I had the db2jcc.jar is the project classpath, however, not in the JUnit classpath (I did not know we needed two different configurations). I am accepting this answer because this is more specific to JUnit Run Configuration. Problem solved - time to run some JUnit tests. Thanks All. – Beetle Pepsi May 19 '15 at 03:45
  • Usually, while running from eclipse the build class path is part of run time class-path , so you don't explicitly change or configure that. but sometimes when luck favors you need to :) – Akash Yadav May 19 '15 at 05:13
0

1) From my notes above:

It sounds like you're probably using Spring on WebSphere. The core error in your log is Cannot load JDBC driver class com.ibm.db2.jcc.DB2Driver.

SUGGESTIONS:

  1. trace where your JNDI definitions (for example, vitDataSource) are defining their driver classnames (like database.vitedoc.driver.classname)

2) Make sure this DB2Driver available as a JDBC resource in your WebSphere Admin console ... and/or ...

3) Make sure the DB2 driver .jar is configured in your pom.xml (if you're using Maven).

2) You've confirmed:

"...the classname is defined as: database.vitedoc.driver.classname=com.ibm.db2.jcc.DB2Driver"

<= Good!

3) So now all you need to do is:

a) Make sure you have a valid db2jcc.jar:

http://www-01.ibm.com/support/docview.wss?uid=swg21363866

b) Make sure the .jar is in the classpath

If you're running WebSphere, my preference is to do this in the WebSphere Admin console:

http://www-01.ibm.com/support/knowledgecenter/SSAW57_8.5.5/com.ibm.websphere.nd.doc/ae/tdat_ccrtpds.html?cp=SSAW57_8.5.5%2F1-3-0-23-3-0-7-2

If this is a simple web app, you can just add it to your Eclipse project's \lib folder:

http://help.eclipse.org/indigo/index.jsp?topic=%2Forg.eclipse.wst.webtools.doc.user%2Ftopics%2Fccwebprj.html

paulsm4
  • 114,292
  • 17
  • 138
  • 190
0

Here's how you can add third party libraries to eclipse.

  1. Open eclipse go to package explorer.
  2. Right click on your project and select project properties.
  3. Click on java Build path in the bar on left hand side of the popup.
  4. Select the libraries tab.
  5. Select the "Add External Jar File" and using the browser navigate to your JAR file and select them "Open" and then "OK".
Eranda
  • 1,439
  • 1
  • 17
  • 30