Java 找不到指定的 JDBC 驱动程序“com.microsoft.sqlserver.jdbc.SQLServerDriver”类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18739161/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Specified JDBC Driver "com.microsoft.sqlserver.jdbc.SQLServerDriver" class not found
提问by gstackoverflow
I try to create simple application for testing hibernate. I use ms sql server as database
我尝试创建简单的应用程序来测试休眠。我使用 ms sql server 作为数据库
I download sqljdbc4 from microsoft official site and add to build path of my sts(eclipse):

我从微软官方网站下载 sqljdbc4 并添加到我的 sts(eclipse) 的构建路径中:

And it locates on maven dependencies
它位于 maven 依赖项上
When I invoke my application I saw:
当我调用我的应用程序时,我看到:
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.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
I don't understand the cause if this messages. If in code I write:
如果此消息,我不明白原因。如果在代码中我写:
com.microsoft.sqlserver.jdbc.SQLServerDriver
and type ctrl+left mouse click I saw ![enter image description here][2]
然后输入 ctrl+鼠标左键我看到了![在此处输入图片描述][2]
Therefore I can made output, that this class exists, but hibernate doesn't see its.
因此我可以输出,这个类存在,但休眠没有看到它。
Can you help me?
你能帮助我吗?
UPDATE
更新
How do you invoke your application?
你如何调用你的应用程序?
I have so class:
我有这样的课:
import org.hibernate.Session;
public class Main {
public static void main(String[] args){
Prepod prepod = new Prepod();
Student student = new Student();
prepod.getStudents().add(student);
student.getPrepods().add(prepod);
Session session = HibernateUtil.getSessionFactory().openSession();
session.beginTransaction();
session.update(student);
session.getTransaction().commit();
}
}
and so:
所以:
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateUtil {
private static final SessionFactory sessionFactory = buildSessionFactory();
private static SessionFactory buildSessionFactory() {
try {
// Create the SessionFactory from hibernate.cfg.xml
// Configuration configuration = new Configuration().configure(
// "C:\Users\Nikolay_Tkachev\workspace\hiberTest\src\logic\hibernate.cfg.xml");
// return configuration.buildSessionFactory();
SessionFactory sessionFactory = new Configuration()
.configure("/resources/hibernate.cfg.xml").buildSessionFactory();
return sessionFactory;
} catch (Throwable ex) {
// Make sure you log the exception, as it might be swallowed
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
public static void shutdown() {
// Close caches and connection pools
getSessionFactory().close();
}
}
right click for Main.java -> run as->java Application
右键单击 Main.java -> run as->java Application
UPDATE 2
更新 2
project structure:
项目结构:
![enter image description here][3]
![在此处输入图像描述][3]
pom.xml:
pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>hiberTest</groupId>
<artifactId>hiberTest</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>myTest</name>
<description>description</description>
<properties>
<hibernate.version>4.2.1.Final</hibernate.version>
<hibernate-validator.version>4.3.1.Final</hibernate-validator.version>
</properties>
<dependencies>
<!-- <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version> </dependency> -->
<!-- HIBERNATE -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>4.0.1.Final</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-c3p0</artifactId>
<version>4.0.1.Final</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-server</artifactId>
<version>1.8</version>
</dependency>
<dependency>
<groupId>com.sun.jersey.contribs</groupId>
<artifactId>jersey-multipart</artifactId>
<version>1.8</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.6.1</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>4.1.4.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-annotations</artifactId>
<version>3.5.6-Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>4.3.0.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate.common</groupId>
<artifactId>hibernate-commons-annotations</artifactId>
<version>4.0.1.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-envers</artifactId>
<version>4.1.4.Final</version>
</dependency>
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>sqljdbc4</artifactId>
<version>3.0</version>
</dependency>
</dependencies>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.0</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
UPDATE FOR Vinay
维奈更新
Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation. All rights reserved.
...
C:\Users\Nikolay_Tkachev\.m2> mvn install:install-file -Dfile=sqljdbc4-3.0.jar -DgroupId=com.microsoft.sqlserver -DartifactI
d=sqljdbc4 -Dversion=3.0 -Dpackaging=jar
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building Maven Stub Project (No POM) 1
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-install-plugin:2.3.1:install-file (default-cli) @ standalone-pom ---
[INFO] Installing C:\Users\Nikolay_Tkachev\.m2\sqljdbc4-3.0.jar to C:\Users\Nikolay_Tkachev\.m2\repository\com\microsoft\sql
server\sqljdbc4.0\sqljdbc4-3.0.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.686s
[INFO] Finished at: Wed Sep 11 17:05:35 MSK 2013
[INFO] Final Memory: 3M/90M
[INFO] ------------------------------------------------------------------------
C:\Users\Nikolay_Tkachev\.m2>cd C:\Users\Nikolay_Tkachev\mavenHibernateTest
C:\Users\Nikolay_Tkachev\mavenHibernateTest>mvn eclipse:eclipse
[INFO] Scanning for projects...
[WARNING]
[WARNING] Some problems were encountered while building the effective model for hiberTest:hiberTest:jar:0.0.1-SNAPSHOT
[WARNING] 'dependencies.dependency.(groupId:artifactId:type:classifier)' must be unique: org.hibernate:hibernate-core:jar ->
version 4.0.1.Final vs 4.1.4.Final @ line 45, column 15
[WARNING]
[WARNING] It is highly recommended to fix these problems because they threaten the stability of your build.
[WARNING]
[WARNING] For this reason, future Maven versions might no longer support building such malformed projects.
[WARNING]
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building myTest 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] >>> maven-eclipse-plugin:2.9:eclipse (default-cli) @ hiberTest >>>
[INFO]
[INFO] <<< maven-eclipse-plugin:2.9:eclipse (default-cli) @ hiberTest <<<
[INFO]
[INFO] --- maven-eclipse-plugin:2.9:eclipse (default-cli) @ hiberTest ---
[INFO] Using Eclipse Workspace: null
[INFO] Adding default classpath container: org.eclipse.jdt.launching.JRE_CONTAINER
[INFO] File C:\Users\Nikolay_Tkachev\mavenHibernateTest\.project already exists.
Additional settings will be preserved, run mvn eclipse:clean if you want old settings to be removed.
[INFO] Wrote Eclipse project for "hiberTest" to C:\Users\Nikolay_Tkachev\mavenHibernateTest.
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.061s
[INFO] Finished at: Wed Sep 11 17:07:20 MSK 2013
[INFO] Final Memory: 7M/113M
[INFO] ------------------------------------------------------------------------
C:\Users\Nikolay_Tkachev\mavenHibernateTest>
But I have old problems(((
但是我有老问题(((
UPDATE
更新
C:\Users\Nikolay_Tkachev\mavenHibernateTest>
C:\Users\Nikolay_Tkachev\mavenHibernateTest>mvn exec:java -Dexec.mainClass="logic.Main"
[INFO] Scanning for projects...
[WARNING]
[WARNING] Some problems were encountered while building the effective model for hiberTest:hiberTest:jar:0.0.1-SNAPSHOT
[WARNING] 'dependencies.dependency.(groupId:artifactId:type:classifier)' must be unique: org.hibernate:hibernate-core:jar ->
version 4.0.1.Final vs 4.1.4.Final @ line 45, column 15
[WARNING]
[WARNING] It is highly recommended to fix these problems because they threaten the stability of your build.
[WARNING]
[WARNING] For this reason, future Maven versions might no longer support building such malformed projects.
[WARNING]
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building myTest 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] >>> exec-maven-plugin:1.2.1:java (default-cli) @ hiberTest >>>
[INFO]
[INFO] <<< exec-maven-plugin:1.2.1:java (default-cli) @ hiberTest <<<
[INFO]
[INFO] --- exec-maven-plugin:1.2.1:java (default-cli) @ hiberTest ---
log4j:WARN No appenders could be found for logger (org.jboss.logging).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
Initial SessionFactory creation failed.org.hibernate.service.classloading.spi.ClassLoadingException: Specified JDBC Driver "
com.microsoft.sqlserver.jdbc.SQLServerDriver" class not found
[WARNING]
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.codehaus.mojo.exec.ExecJavaMojo.run(ExecJavaMojo.java:297)
at java.lang.Thread.run(Thread.java:662)
Caused by: java.lang.ExceptionInInitializerError
at logic.HibernateUtil.buildSessionFactory(HibernateUtil.java:18)
at logic.HibernateUtil.<clinit>(HibernateUtil.java:8)
at logic.Main.main(Main.java:12)
... 6 more
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(DriverManagerConnec
tionProviderImpl.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:13)
... 8 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(DriverManagerConnec
tionProviderImpl.java:104)
... 22 more
Caused by: java.lang.ClassNotFoundException: Could not load requested class : "com.microsoft.sqlserver.jdbc.SQLServerDriver"
at org.hibernate.service.classloading.internal.ClassLoaderServiceImpl.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)
... 23 more
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.403s
[INFO] Finished at: Wed Sep 11 17:14:07 MSK 2013
[INFO] Final Memory: 6M/115M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.codehaus.mojo:exec-maven-plugin:1.2.1:java (default-cli) on project hiberTest: An excepti
on occured while executing the Java class. null: InvocationTargetException: ExceptionInInitializerError: Specified JDBC Driv
er "com.microsoft.sqlserver.jdbc.SQLServerDriver" class not found: Unable to load class ["com.microsoft.sqlserver.jdbc.SQLSe
rverDriver"]: Could not load requested class : "com.microsoft.sqlserver.jdbc.SQLServerDriver" -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException
C:\Users\Nikolay_Tkachev\mavenHibernateTe
st>
st>
content sqljdbc.jar
内容 sqljdbc.jar
Solution:
解决方案:
WRONG:
错误:
<property name="hibernate.connection.driver_class">"com.microsoft.sqlserver.jdbc.SQLServerD??river"</property>
RIGHT:
对:
<property name="hibernate.connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerD??river</property>
采纳答案by gstackoverflow
very simple mistake:
非常简单的错误:
Solution:
解决方案:
WRONG:
错误:
<property name="hibernate.connection.driver_class">"com.microsoft.sqlserver.jdbc.SQLServerDriver"</property>
RIGHT:
对:
<property name="hibernate.connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
It is very funny)
这很有趣)
回答by Vinay Lodha
回答by rapport89
The following snippet works well for connecting and querying database in Microsoft SQL SERVER. Please note that Microsoft does not make jar file 'sqljdbc42.jar'(which is jdbc driver) available on any online repo like MavenCentral(). Therefore, you would need to download this jar from Microsoft's website and save in project workspace directory. Download 'sqljdbc_4.2.6420.100_enu.exe'
以下代码段适用于在 Microsoft SQL SERVER 中连接和查询数据库。请注意,Microsoft 不会在任何在线存储库(如 MavenCentral())上提供 jar 文件“sqljdbc42.jar”(即 jdbc 驱动程序)。因此,您需要从 Microsoft 网站下载此 jar 并将其保存在项目工作区目录中。下载'sqljdbc_4.2.6420.100_enu.exe'
Unzip the downloaded file and go to Microsoft JDBC Driver 4.2 for SQL Server-->sqljdbc_4.2-->enu. Here you will see the file sqljdbc42.jar. Copy the file into project workspace. I copied into dir name 'lib'.
解压下载的文件并转到 Microsoft JDBC Driver 4.2 for SQL Server-->sqljdbc_4.2-->enu。在这里您将看到文件 sqljdbc42.jar。将文件复制到项目工作区。我复制到目录名称'lib'。
repositories {
flatDir name: 'localRepository', dirs: 'lib'
}
configurations {
driver
}
dependencies {
driver group: 'sql', name: 'sqljdbc42', version:''
}
URLClassLoader loader = GroovyObject.class.classLoader
configurations.driver.each { File file ->
loader.addURL(file.toURL())
}
task connectToCoreDb << {
def props = [user: 'sa', password: 'shock', allowMultiQueries: 'true'] as Properties
def connectionUrl = "jdbc:sqlserver://cpt-op-01-db1:1433;databaseName=buds"
def driver = 'com.microsoft.sqlserver.jdbc.SQLServerDriver'
def sql = Sql.newInstance(connectionUrl, props, driver)
sql.eachRow('SELECT name, alias, expiry_date FROM [buds].[dbo].[obj_keystore] ) { row ->
println "$row.name $row.alias $row.expiry_date"
}
logger.info "Closing connection..."
sql.close()
}

