Java hibernate 错误 "Specified JDBC Driver com.microsoft.sqlserver.jdbc.SQLServerDriver class not found"

标签 java hibernate maven

我在运行我的应用程序时遇到错误,错误是:

Specified JDBC Driver com.microsoft.sqlserver.jdbc.SQLServerDriver class not found

我的来源: pom.xml

    <dependency>
        <groupId>org.apache.struts</groupId>
        <artifactId>struts2-convention-plugin</artifactId>
        <version>2.3.20</version>
    </dependency>
    <dependency>
        <groupId>org.apache.struts</groupId>
        <artifactId>struts2-rest-plugin</artifactId>
        <version>2.3.20</version>
    </dependency> 

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>4.0.1.Final</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-validator</artifactId>
        <version>4.2.0.Final</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate.common</groupId>
        <artifactId>hibernate-commons-annotations</artifactId>
        <version>4.0.1.Final</version>
        <classifier>tests</classifier>
    </dependency>
    <dependency>
        <groupId>org.hibernate.javax.persistence</groupId>
        <artifactId>hibernate-jpa-2.0-api</artifactId>
        <version>1.0.1.Final</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
        <version>4.0.1.Final</version>
    </dependency>
    <dependency>
        <groupId>javax.validation</groupId>
        <artifactId>validation-api</artifactId>
        <version>1.0.0.GA</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.6.4</version>
    </dependency>
    <dependency>
        <groupId>org.jboss.logging</groupId>
        <artifactId>jboss-logging</artifactId>
        <version>3.1.0.CR2</version>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
        <version>1.6.4</version>
    </dependency>

    <dependency>
        <groupId>com.microsoft.sqlserver.jdbc</groupId>
        <artifactId>sqljdbc4</artifactId>
        <version>1.0</version>
        <scope>system</scope>
        <systemPath>${basedir}/src/main/lib/sqljdbc4.jar</systemPath>
    </dependency>

hibernate.cfg.xml

<hibernate-configuration>     
<session-factory> 

 <!-- Database connection settings -->  
    <property name="connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>  
    <property name="connection.url">jdbc:sqlserver://.;database=db</property>  
    <property name="connection.username">sa</property>  
    <property name="connection.password">12345</property>   
    <property name="connection.pool_size">1</property>   
    <property name="dialect">org.hibernate.dialect.SQLServer2005Dialect</property>   
    <property name="show_sql">false</property>   
    <property name="hbm2ddl.auto">update</property>  

    <!-- Mapping files --> 
    <mapping resource="employee.hbm.xml"/> 

</session-factory> 

调用Hibernate的代码:

public static List<Employee> SelectAll(){ 
    Configuration cfg = new Configuration();  
    cfg.configure("hibernate.cfg.xml");
    SessionFactory factory = cfg.buildSessionFactory();  
    Session session = factory.openSession(); 

    //String sql_query= "from Employee where Name = 'Kevin'";
    String sql_query= "from Employee";
    Query query = session.createQuery(sql_query);
    List<Employee> employees = query.list();

    session.close();  
    factory.close(); 
    return employees;
}

有人知道为什么吗? 错误发生在 SessionFactory 工厂 = cfg.buildSessionFactory();

目录:

enter image description here

当我检查库时,它有 SQLServerDriver

enter image description here

最佳答案

@Lumanyun 您使用的 jar 不存在于存储库 Maven 中:

<dependency>
        <groupId>com.microsoft.sqlserver.jdbc</groupId>
        <artifactId>sqljdbc4</artifactId>
        <version>1.0</version>
        <scope>system</scope>
        <systemPath>${basedir}/lib/sqljdbc4.jar</systemPath>
    </dependency>

去查看here一个可以帮助您的版本

关于Java hibernate 错误 "Specified JDBC Driver com.microsoft.sqlserver.jdbc.SQLServerDriver class not found",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36278318/

相关文章:

java - Java 中的 XMLGregorianCalendar,没有时区

java - 如何从一个 Java FXML Controller 中从另一个 FXML Controller 获取数据(这些 Controller 不是嵌套 Controller )?

java - 基本类型 map 的 Hibernate 标准

hibernate - JPA 注释 = EJB3 注释 = Hibernate 注释?

maven - jetty :run-exploded updating static resources

Maven 最后构建父 pom

java - Android Studio 登录 创建帐户

java - 是否可以使用 JPA/Hibernate 为非映射列设置 `null` 值?

maven - 在 STS - spring 工具套件中,war 文件未显示添加到 tomcat 中?

java - 使用构建器定义不可变映射的类型