使用hibernate和ms sql server驱动程序

时间:2013-09-11 09:34:05

标签: java sql-server hibernate jdbc orm

Microsoft不向中央maven存储库提供jdbc jar。它需要手动注册驱动程序。 但我犯了错误。我看到了同样的异常痕迹:

Initial SessionFactory creation failed.org.hibernate.service.classloading.spi.ClassLoadingException: Specified JDBC Driver "com.microsoft.sqlserver.jdbc.SQLServerDriver" class not found
Exception in thread "main" java.lang.ExceptionInInitializerError
    at logic.HibernateUtil.buildSessionFactory(HibernateUtil.java:22)
    at logic.HibernateUtil.<clinit>(HibernateUtil.java:8)
    at logic.Main.main(Main.java:12)
Caused by: org.hibernate.service.classloading.spi.ClassLoadingException: Specified JDBC Driver "com.microsoft.sqlserver.jdbc.SQLServerDriver" class not found
    at org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl.configure(DriverManagerConnectionProviderImpl.java:107)
    at org.hibernate.service.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:75)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:159)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:131)
    at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.buildJdbcConnectionAccess(JdbcServicesImpl.java:223)
    at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:89)
    at org.hibernate.service.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:75)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:159)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:131)
    at org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:71)
    at org.hibernate.cfg.Configuration.buildSettingsInternal(Configuration.java:2277)
    at org.hibernate.cfg.Configuration.buildSettings(Configuration.java:2273)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1742)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1782)
    at logic.HibernateUtil.buildSessionFactory(HibernateUtil.java:17)
    ... 2 more
Caused by: org.hibernate.service.classloading.spi.ClassLoadingException: Unable to load class ["com.microsoft.sqlserver.jdbc.SQLServerDriver"]
    at org.hibernate.service.classloading.internal.ClassLoaderServiceImpl.classForName(ClassLoaderServiceImpl.java:141)
    at org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl.configure(DriverManagerConnectionProviderImpl.java:104)
    ... 16 more
Caused by: java.lang.ClassNotFoundException: Could not load requested class : "com.microsoft.sqlserver.jdbc.SQLServerDriver"
    at org.hibernate.service.classloading.internal.ClassLoaderServiceImpl$1.findClass(ClassLoaderServiceImpl.java:99)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:247)
    at org.hibernate.service.classloading.internal.ClassLoaderServiceImpl.classForName(ClassLoaderServiceImpl.java:138)
    ... 17 more

我使用STS(eclipse产品) 在maven依赖项中: enter image description here

我做错了什么?

2 个答案:

答案 0 :(得分:1)

如果您使用的是JRE版本6.0或7.0,请使用sqljdbc4.jar文件。如果JRE版本是5.0,则使用sqljdbc.jar文件。

可以从here

下载这些jar文件

您还可以从here

更清楚地了解JDBC驱动程序的系统要求

答案 1 :(得分:0)

请检查以下属性是否正确配置

<property name="hibernate.connection.url">jdbc:sqlserver://localhost:1433;database=<dbname>;</property>
    <property name="hibernate.dialect">org.hibernate.dialect.SQLServer2008Dialect</property>