Sam Carleton
Sam Carleton

Reputation: 1408

Adding JDBC jar so Spring Framework can find it

I am new to Java, Spring, and Eclipse. I am working on a simple Spring program that is using Spring Framework JDBC to connect to Microsoft SQL Server 2012. With the correct configuration I have succeeded in using the traditional JDBC to talk to the database. The problem I am running into is the application is not able to find the Microsoft JDBC driver. I am assuming it is simply a matter of referencing the JAR file differently. Here is what I currently have for my classpath file:

<?xml version="1.0" encoding="UTF-8"?>
<classpath>
    <classpathentry kind="src" output="target/classes" path="src/main/java"/>
    <classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources"/>
    <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/>
    <classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER"/>
    <classpathentry kind="lib" path="C:/Program Files/Microsoft JDBC Driver 4.0 for SQL Server/sqljdbc_4.0/enu/sqljdbc4.jar"/>
    <classpathentry kind="output" path="target/classes"/>
</classpath>

And here is the app-context.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"
    xsi:schemaLocation="
      http://www.springframework.org/schema/beans   http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
      http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="com.microsoft.sqlserver.jdbc.Driver"/>
        <property name="url" value="jdbc:sqlserver://localhost;databaseName=ProSpringCh8"/>
        <property name="username" value="****"/>
        <property name="password" value="****"/>
    </bean>

    <context:property-placeholder location="jdbc.properties" />

    <bean id="contactDao" class="com.accumed.DatabaseTest.dao.jdbc.xml.JdbcContactDao">
        <property name="dataSource">
            <ref local="dataSource"/>
        </property>
    </bean>

</beans>

I am running it from within Eclipse's debugger. The errors I am getting are:

09:28:19,711 DEBUG g.springframework.jdbc.core.JdbcTemplate: 635 - Executing prepared SQL query
09:28:19,726 DEBUG g.springframework.jdbc.core.JdbcTemplate: 570 - Executing prepared SQL statement [select first_name from contact where id = ?]
09:28:19,774 DEBUG ramework.jdbc.datasource.DataSourceUtils: 110 - Fetching JDBC Connection from DataSource
Exception in thread "main" org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is org.apache.commons.dbcp.SQLNestedException: Cannot load JDBC driver class 'com.microsoft.sqlserver.jdbc.Driver'
    at org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:80)
    at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:573)
    at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:637)
    at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:666)
    at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:674)
    at org.springframework.jdbc.core.JdbcTemplate.queryForObject(JdbcTemplate.java:729)
    at org.springframework.jdbc.core.JdbcTemplate.queryForObject(JdbcTemplate.java:745)
    at com.accumed.DatabaseTest.dao.jdbc.xml.JdbcContactDao.findFirstNameById(JdbcContactDao.java:55)
    at com.accumed.DatabaseTest.JdbcContactDaoSample.main(JdbcContactDaoSample.java:21)
Caused by: org.apache.commons.dbcp.SQLNestedException: Cannot load JDBC driver class 'com.microsoft.sqlserver.jdbc.Driver'
    at org.apache.commons.dbcp.BasicDataSource.createConnectionFactory(BasicDataSource.java:1429)
    at org.apache.commons.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:1371)
    at org.apache.commons.dbcp.BasicDataSource.getConnection(BasicDataSource.java:1044)
    at org.springframework.jdbc.datasource.DataSourceUtils.doGetConnection(DataSourceUtils.java:111)
    at org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:77)
    ... 8 more
Caused by: java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.Driver
    at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
    at org.apache.commons.dbcp.BasicDataSource.createConnectionFactory(BasicDataSource.java:1420)
    ... 12 more

Upvotes: 0

Views: 8582

Answers (1)

Emil Gotsev
Emil Gotsev

Reputation: 223

The SQL Server JDBC driver class is com.microsoft.sqlserver.jdbc.SQLServerDriver, not com.microsoft.sqlserver.jdbc.Driver

Upvotes: 2

Related Questions