0

我是 Java、Spring 和 Eclipse 的新手。我正在开发一个简单的 Spring 程序,该程序使用 Spring Framework JDBC 连接到 Microsoft SQL Server 2012。通过正确的配置,我成功地使用传统的 JDBC 与数据库通信。我遇到的问题是应用程序无法找到 Microsoft JDBC 驱动程序。我假设这只是以不同方式引用 JAR 文件的问题。这是我目前的类路径文件:

<?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>

这是 app-context.xml 文件:

<?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>

我在 Eclipse 的调试器中运行它。我得到的错误是:

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
4

1 回答 1

2

SQL Server JDBC 驱动程序类是com.microsoft.sqlserver.jdbc.SQLServerDriver,不是com.microsoft.sqlserver.jdbc.Driver

于 2012-11-09T16:01:42.777 回答