ClassNotFoundException-com.microsoft.jdbc.sqlserver.SQLServerDriver [英] ClassNotFoundException - com.microsoft.jdbc.sqlserver.SQLServerDriver

查看:120
本文介绍了ClassNotFoundException-com.microsoft.jdbc.sqlserver.SQLServerDriver的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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).

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

The sqljdbc41.jar is in my application build path:

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

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

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

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

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:我是否正在加载正确的驱动程序?
  2. 在代码"Class.forName(" com.microsoft.sqlserver.jdbc.SqlServerDriver);"
  1. java.lang.ClassNotFoundException: com.microsoft.jdbc.sqlserver.SQLServerDriver : Am I loading the right driver?
  2. https://social.msdn.microsoft.com/Forums/sqlserver/en-US/b425c201-9882-4a48-b049-4004f202b0c6/javalangclassnotfoundexception-commicrosoftsqlserverjdbcsqlserverdriver?forum=sqldataaccess
  3. Getting ClassNotFoundException on code: "Class.forName("com.microsoft.sqlserver.jdbc.SqlServerDriver");"

还有更多

编译器级别1.7和JRE 1.7-根据

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:

echo $CLASSPATH
/var/common/sqljdbc41.jar

是哪个.此外:

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

更新

我再次从Microsoft下载了sqljdbc41.jar,只是为了确保第一个jar未被损坏.

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

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

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.

仍然得到ClassNotFoundException.

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

Also, the Eclipse IDE can see the driver:

推荐答案

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

不能扔 ClassNotFoundException - com.microsoft.jdbc.sqlserver.SQLServerDriver

,因为名称不同.

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

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

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

我刚刚在Microsoft的Web文档中找到了这两个名称,所以在某个时候重命名了此类(更改了其程序包),或者在某些文档中出现了错误.

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.

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

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.

如果您具有正确的类名,并且在lib目录中具有正确的jar,并且仍然看到该错误,我想知道您的eclipse设置中是否存在某种错字,并且从eclipse进行部署会以某种方式迫使尝试尝试加载损坏的类名. (我不使用Eclipse,也不知道从那里进行部署.)

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).

尝试创建一个非常简单的应用程序(不要向eclipse讲述MS驱动程序类):

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

@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!

这意味着驱动程序已正确加载.连接失败b/c我当前没有要测试的SQLServer实例.

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

这篇关于ClassNotFoundException-com.microsoft.jdbc.sqlserver.SQLServerDriver的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆