ClassNotFoundException - com.microsoft.jdbc.sqlserver.SQLServerDriver

时间:2021-08-11 09:22:17

I have a web development project using local install of Tomcat 7. I am trying to connect to SQL Server 2012 using Microsoft's driver for jdbc (sqljdbc41.jar).

我有一个使用Tomcat 7本地安装的Web开发项目。我正在尝试使用Microsoft的jdbc驱动程序(sqljdbc41.jar)连接到SQL Server 2012。

The sqljdbc41.jar is in my application build path:

sqljdbc41.jar在我的应用程序构建路径中:

ClassNotFoundException  -  com.microsoft.jdbc.sqlserver.SQLServerDriver

and I am exporting it. Furthermore, in the Tomcat application directory lib folder I have also placed a copy of sqljdbc41.jar.

我正在出口它。此外,在Tomcat应用程序目录lib文件夹中,我还放置了sqljdbc41.jar的副本。

There are no compile errors, but at runtime when I try to load the SQL Server driver I get the following:

没有编译错误,但在运行时,当我尝试加载SQL Server驱动程序时,我得到以下内容:

ClassNotFoundException - com.microsoft.jdbc.sqlserver.SQLServerDriver

The exception is thrown in the following code block:

以下代码块中抛出异常:

Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String connectionUrl = "jdbc:sqlserver://"+server+":1433;databaseName="+database+";user=sa;password="+password+";";
con = (Connection) DriverManager.getConnection(connectionUrl);

I have seen many posts on this topic without resolution:

我已经看过很多关于这个主题的帖子而没有解决:

  1. java.lang.ClassNotFoundException: com.microsoft.jdbc.sqlserver.SQLServerDriver : Am I loading the right driver?
  2. java.lang.ClassNotFoundException:com.microsoft.jdbc.sqlserver.SQLServerDriver:我加载了正确的驱动程序吗?
  3. https://social.msdn.microsoft.com/Forums/sqlserver/en-US/b425c201-9882-4a48-b049-4004f202b0c6/javalangclassnotfoundexception-commicrosoftsqlserverjdbcsqlserverdriver?forum=sqldataaccess
  4. https://social.msdn.microsoft.com/Forums/sqlserver/en-US/b425c201-9882-4a48-b049-4004f202b0c6/javalangclassnotfoundexception-commicrosoftsqlserverjdbcsqlserverdriver?forum=sqldataaccess
  5. Getting ClassNotFoundException on code: "Class.forName("com.microsoft.sqlserver.jdbc.SqlServerDriver");"
  6. 在代码上获取ClassNotFoundException:“Class.forName(”com.microsoft.sqlserver.jdbc.SqlServerDriver“);”

and many more.

还有很多。

Compiler level 1.7 and a JRE of 1.7 - According to documentation, I believe I am using the correct driver. This also states that the CLASSPATH must be set:

编译器级别1.7和JRE 1.7 - 根据文档,我相信我使用的是正确的驱动程序。这也表明必须设置CLASSPATH:

echo $CLASSPATH
/var/common/sqljdbc41.jar

Which it is. Furthermore:

它是什么。此外:

java -version
java version "1.7.0_75"
Java(TM) SE Runtime Environment (build 1.7.0_75-b13)
Java HotSpot(TM)
64-Bit Server VM (build 24.75-b04, mixed mode)

So, why am I still encountering this???

那么,为什么我还在遇到这个?

Update

更新

I downloaded the sqljdbc41.jar from Microsoft again - just to be sure that somehow the first jar was not corrupt.

我再次从Microsoft下载了sqljdbc41.jar - 只是为了确保第一个jar没有损坏。

Following Mick Mnemonic's link, I removed the jar from the Java Build path and put the newly downloaded jar into the WEB-INF/lib folder of the web application. I then restarted Eclipse and the Tomcat server and cleaned the Tomcat server, and the project.

按照Mick Mnemonic的链接,我从Java Build路径中删除了jar,并将新下载的jar放入Web应用程序的WEB-INF / lib文件夹中。然后我重新启动了Eclipse和Tomcat服务器并清理了Tomcat服务器和项目。

Still getting the ClassNotFoundException.

仍然得到ClassNotFoundException。

Also, the Eclipse IDE can see the driver: ClassNotFoundException  -  com.microsoft.jdbc.sqlserver.SQLServerDriver

此外,Eclipse IDE可以看到驱动程序:

4 个解决方案

#1


4  

The code Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver")

代码Class.forName(“com.microsoft.sqlserver.jdbc.SQLServerDriver”)

cannot throw ClassNotFoundException - com.microsoft.jdbc.sqlserver.SQLServerDriver

不能抛出ClassNotFoundException - com.microsoft.jdbc.sqlserver.SQLServerDriver

as the names are different. Is it possible that you have it set up incorrectly in your code?

因为名字不同。是否可能在代码中设置错误?

I downloaded the sqljdbc41.jar from their website and see that the correct name for the class is com.microsoft.sqlserver.jdbc.SQLServerDriver.

我从他们的网站下载了sqljdbc41.jar,看到该类的正确名称是com.microsoft.sqlserver.jdbc.SQLServerDriver。

$ jar tf sqljdbc41.jar | grep SQLServerDriver.class
com/microsoft/sqlserver/jdbc/SQLServerDriver.class

I just found both names on Microsoft's web documentation, so either the renamed this class (changed its package) at some point, or they have errors on some of their docs.

我刚刚在微软的网络文档中找到了这两个名字,所以要么在某个时候重命名这个类(更改了它的包),要么他们的某些文档有错误。

All you should need to do is drop that .jar in Tomcat's lib directory (e.g.apache-tomcat-7.0.67\lib), and restart Tomcat.

您需要做的就是将.jar放在Tomcat的lib目录(例如,apache-tomcat-7.0.67 \ lib)中,然后重新启动Tomcat。

If you have the correct class name, and the right jar in the lib directory, and are still seeing that error, I wonder if you have some sort of typo in your eclipse setup, and deploying from eclipse is somehow forcing an attempt to load that broken class name. (I don't use Eclipse, and I don't know about deploying from there).

如果你有正确的类名,并且在lib目录中有正确的jar,并且仍然看到该错误,我想知道你的eclipse设置中是否有某种拼写错误,并且从eclipse部署会以某种方式强制尝试加载破碎的班级名称。 (我不使用Eclipse,我不知道从那里部署)。

Try creating a very simple application (and don't tell eclipse about the MS driver class):

尝试创建一个非常简单的应用程序(并且不要告诉eclipse关于MS驱动程序类):

@WebServlet("/")
public class SimpleServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // Set response content type
        resp.setContentType("text/html");
        PrintWriter out = resp.getWriter();
        out.println("<h1>" + "Welcome to the servlet!" + "</h1>");
        try {
            String server = "localhost";
            String database = "testDB";
            String password = "sapassword";

            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            String connectionUrl = "jdbc:sqlserver://"+server+":1433;databaseName="+database+";user=sa;password="+password+";";
            Connection con = (Connection) DriverManager.getConnection(connectionUrl);
        } catch (ClassNotFoundException e) {
            out.println("<h2>" + e.getClass().getSimpleName() + "_" + e.getMessage() + "</h2>");
        } catch (SQLException e){
            out.println("<h2>" + e.getClass().getSimpleName() + "_" + e.getMessage() + "</h2>");
        } finally {
            out.println("<h1>" + "That's the end of the servlet!" + "</h1>");
        }
    }
}

And running it. If you see output like:

并运行它。如果您看到如下输出:

Welcome to the servlet!

SQLServerException_The TCP/IP connection to the host localhost, port 1433 has failed. Error: "Connection refused: connect. Verify the connection properties. Make sure that an instance of SQL Server is running on the host and accepting TCP/IP connections at the port. Make sure that TCP connections to the port are not blocked by a firewall.".

That's the end of the servlet!

It means that the driver loaded properly. The connection failed b/c I don't have SQLServer instance currently running to test against.

这意味着驱动程序正确加载。连接失败b / c我没有正在运行的SQLServer实例进行测试。

#2


2  

You need to add the library to catalina.properties because you are using the DB connection within the context of the container's core functionality, and not just as one of the child applications inside it.

您需要将库添加到catalina.properties,因为您在容器的核心功能的上下文中使用数据库连接,而不仅仅是其中的子应用程序之一。

Downvote my answer as you wish, but I only ask you test it first.

按照你的意愿向我推荐我的答案,但我只是要求你先测试一下。

#3


1  

I think you need to be registered on the dialect class: org.hibernate.dialect.SQLServer2012Dialect

我想你需要在方言类上注册:org.hibernate.dialect.SQLServer2012Dialect

if it were spring project, it is connected to a file application.properties:

如果它是spring项目,则它连接到文件application.properties:

spring.datasource.driver-class-name=com.microsoft.sqlserver.jdbc.SQLServerDriver
    spring.jpa.database-platform=org.hibernate.dialect.SQLServer2012Dialect
    spring.datasource.url=jdbc:sqlserver:database;user=sa;password=password;
    spring.datasource.username=sa
    spring.datasource.password=password

but here it will be exactly like I do not remember

但这里就像我不记得的那样

#4


1  

Looking at all the comments here on this post, I assume that you've tried almost all the options provided. Looking at your snapshots provided above, I assume you are using a Unix/Linux based system for your development. There is a change in the way you update your CLASSPATH variable in Windows and say in Linux. ';' is used in Windows while ':' in Linux. Hope you've taken care of such minor details:

看看这篇文章中的所有评论,我假设您已经尝试了几乎所有提供的选项。查看上面提供的快照,我假设您正在使用基于Unix / Linux的系统进行开发。在Windows中更新CLASSPATH变量的方式发生了变化,比如在Linux中更新。 ';'在Windows中使用,而在Linux中使用':'。希望你能够处理这些细节:

There's a helpful link that I can suggest you to go through, where there are many remedies given to resolve the issue that you are facing: Link

我建议您通过一个有用的链接,其中有许多补救措施可以解决您所面临的问题:链接

#1


4  

The code Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver")

代码Class.forName(“com.microsoft.sqlserver.jdbc.SQLServerDriver”)

cannot throw ClassNotFoundException - com.microsoft.jdbc.sqlserver.SQLServerDriver

不能抛出ClassNotFoundException - com.microsoft.jdbc.sqlserver.SQLServerDriver

as the names are different. Is it possible that you have it set up incorrectly in your code?

因为名字不同。是否可能在代码中设置错误?

I downloaded the sqljdbc41.jar from their website and see that the correct name for the class is com.microsoft.sqlserver.jdbc.SQLServerDriver.

我从他们的网站下载了sqljdbc41.jar,看到该类的正确名称是com.microsoft.sqlserver.jdbc.SQLServerDriver。

$ jar tf sqljdbc41.jar | grep SQLServerDriver.class
com/microsoft/sqlserver/jdbc/SQLServerDriver.class

I just found both names on Microsoft's web documentation, so either the renamed this class (changed its package) at some point, or they have errors on some of their docs.

我刚刚在微软的网络文档中找到了这两个名字,所以要么在某个时候重命名这个类(更改了它的包),要么他们的某些文档有错误。

All you should need to do is drop that .jar in Tomcat's lib directory (e.g.apache-tomcat-7.0.67\lib), and restart Tomcat.

您需要做的就是将.jar放在Tomcat的lib目录(例如,apache-tomcat-7.0.67 \ lib)中,然后重新启动Tomcat。

If you have the correct class name, and the right jar in the lib directory, and are still seeing that error, I wonder if you have some sort of typo in your eclipse setup, and deploying from eclipse is somehow forcing an attempt to load that broken class name. (I don't use Eclipse, and I don't know about deploying from there).

如果你有正确的类名,并且在lib目录中有正确的jar,并且仍然看到该错误,我想知道你的eclipse设置中是否有某种拼写错误,并且从eclipse部署会以某种方式强制尝试加载破碎的班级名称。 (我不使用Eclipse,我不知道从那里部署)。

Try creating a very simple application (and don't tell eclipse about the MS driver class):

尝试创建一个非常简单的应用程序(并且不要告诉eclipse关于MS驱动程序类):

@WebServlet("/")
public class SimpleServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // Set response content type
        resp.setContentType("text/html");
        PrintWriter out = resp.getWriter();
        out.println("<h1>" + "Welcome to the servlet!" + "</h1>");
        try {
            String server = "localhost";
            String database = "testDB";
            String password = "sapassword";

            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            String connectionUrl = "jdbc:sqlserver://"+server+":1433;databaseName="+database+";user=sa;password="+password+";";
            Connection con = (Connection) DriverManager.getConnection(connectionUrl);
        } catch (ClassNotFoundException e) {
            out.println("<h2>" + e.getClass().getSimpleName() + "_" + e.getMessage() + "</h2>");
        } catch (SQLException e){
            out.println("<h2>" + e.getClass().getSimpleName() + "_" + e.getMessage() + "</h2>");
        } finally {
            out.println("<h1>" + "That's the end of the servlet!" + "</h1>");
        }
    }
}

And running it. If you see output like:

并运行它。如果您看到如下输出:

Welcome to the servlet!

SQLServerException_The TCP/IP connection to the host localhost, port 1433 has failed. Error: "Connection refused: connect. Verify the connection properties. Make sure that an instance of SQL Server is running on the host and accepting TCP/IP connections at the port. Make sure that TCP connections to the port are not blocked by a firewall.".

That's the end of the servlet!

It means that the driver loaded properly. The connection failed b/c I don't have SQLServer instance currently running to test against.

这意味着驱动程序正确加载。连接失败b / c我没有正在运行的SQLServer实例进行测试。

#2


2  

You need to add the library to catalina.properties because you are using the DB connection within the context of the container's core functionality, and not just as one of the child applications inside it.

您需要将库添加到catalina.properties,因为您在容器的核心功能的上下文中使用数据库连接,而不仅仅是其中的子应用程序之一。

Downvote my answer as you wish, but I only ask you test it first.

按照你的意愿向我推荐我的答案,但我只是要求你先测试一下。

#3


1  

I think you need to be registered on the dialect class: org.hibernate.dialect.SQLServer2012Dialect

我想你需要在方言类上注册:org.hibernate.dialect.SQLServer2012Dialect

if it were spring project, it is connected to a file application.properties:

如果它是spring项目,则它连接到文件application.properties:

spring.datasource.driver-class-name=com.microsoft.sqlserver.jdbc.SQLServerDriver
    spring.jpa.database-platform=org.hibernate.dialect.SQLServer2012Dialect
    spring.datasource.url=jdbc:sqlserver:database;user=sa;password=password;
    spring.datasource.username=sa
    spring.datasource.password=password

but here it will be exactly like I do not remember

但这里就像我不记得的那样

#4


1  

Looking at all the comments here on this post, I assume that you've tried almost all the options provided. Looking at your snapshots provided above, I assume you are using a Unix/Linux based system for your development. There is a change in the way you update your CLASSPATH variable in Windows and say in Linux. ';' is used in Windows while ':' in Linux. Hope you've taken care of such minor details:

看看这篇文章中的所有评论,我假设您已经尝试了几乎所有提供的选项。查看上面提供的快照,我假设您正在使用基于Unix / Linux的系统进行开发。在Windows中更新CLASSPATH变量的方式发生了变化,比如在Linux中更新。 ';'在Windows中使用,而在Linux中使用':'。希望你能够处理这些细节:

There's a helpful link that I can suggest you to go through, where there are many remedies given to resolve the issue that you are facing: Link

我建议您通过一个有用的链接,其中有许多补救措施可以解决您所面临的问题:链接