com.microsoft.sqlserver.jdbc.SQLServerDriver的ClassNotFoundException [英] ClassNotFoundException for com.microsoft.sqlserver.jdbc.SQLServerDriver

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

问题描述

此问题出现在之前在SO上,我确信经验丰富的Java开发人员已经厌倦了告诉新手如何设置类路径。也就是说,我已尝试通过环境变量和-cp选项设置类路径,但没有成功。

This is an issue that has come up before on SO, and I'm sure seasoned Java devs are tired of telling newbies howto set the classpath. That said, I've tried setting the classpath via environment variable and with the -cp option, with no success.

我正在使用捆绑的示例应用程序SQLServer JDBC驱动程序。我正在运行Ubuntu 14.10。我在命令行上编译应用程序:

I'm using the sample applications that are bundled with the SQLServer JDBC driver. I'm running Ubuntu 14.10. I compile the app on the command line:

javac -cp .:/path/to/sqljdbc42.jar connectURL.java 

并运行它:

java connectURL

让我熟悉 ClassNotFoundException

java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver
    at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:191)
    at connectURL.main(connectURL.java:42)

我没有修改过示例类文件,但为了完整性我在这里包含它:

I haven't modified the sample class file, but I'm including it here for completeness:

import java.sql.*;

public class connectURL {

    public static void main(String[] args) {

        // Create a variable for the connection string.
        String connectionUrl = "jdbc:sqlserver://localhost:1433;" +
            "databaseName=AdventureWorks;integratedSecurity=true;";

        // Declare the JDBC objects.
        Connection con = null;
        Statement stmt = null;
        ResultSet rs = null;

            try {
                // Establish the connection.
                Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
                    con = DriverManager.getConnection(connectionUrl);

                    // Create and execute an SQL statement that returns some data.
                    String SQL = "SELECT TOP 10 * FROM Person.Contact";
                    stmt = con.createStatement();
                    rs = stmt.executeQuery(SQL);

                    // Iterate through the data in the result set and display it.
                    while (rs.next()) {
                        System.out.println(rs.getString(4) + " " + rs.getString(6));
                    }
            }

        // Handle any errors that may have occurred.
        catch (Exception e) {
            e.printStackTrace();
        }

        finally {
            if (rs != null) try { rs.close(); } catch(Exception e) {}
                if (stmt != null) try { stmt.close(); } catch(Exception e) {}
                if (con != null) try { con.close(); } catch(Exception e) {}
        }
    }
}

SQL JDBC .jar的路径绝对正确。如果我将 import com.microsoft.sqlserver.jdbc.SQLServerDriver; 添加到类文件中,我在编译时没有任何抱怨,但仍然得到 ClassNotFoundException 在运行时。

The path to the SQL JDBC .jar is definitely correct. If I add import com.microsoft.sqlserver.jdbc.SQLServerDriver; to the class file I don't get any complaints when compiling, but still get the ClassNotFoundException at runtime.

我在其他地方读过,新版本的JDBC不需要你通过<$ c加载驱动程序$ c> Class.forName ,但如果我删除那行,那么我可以预见 java.sql.SQLException:找不到合适的驱动程序

I've read elsewhere that newer versions of the JDBC don't need you to load drivers via Class.forName, but if I remove that line then I get, predictably, java.sql.SQLException: No suitable driver found.

我做错了什么?我确定.jar正在加载并且正在找到类,因为如果我尝试例如 import com.microsoft.sqlserver.jdbc.SomeNonExistentClass; 我得到:

What am I doing wrong? I'm sure the .jar is being loaded and the class is being found, because if I try e.g. import com.microsoft.sqlserver.jdbc.SomeNonExistentClass; I get:

connectURL。 java:2:错误:找不到符号

我的Java详情:

$ java -version
java version "1.7.0_79"
OpenJDK Runtime Environment (IcedTea 2.5.5) (7u79-2.5.5-0ubuntu0.14.10.2)
OpenJDK 64-Bit Server VM (build 24.79-b02, mixed mode)


推荐答案

你已经到了一半。您在编译时在类路径中包含JDBC JAR,但是当您执行它时还需要将它包含在类路径中:

You're halfway there. You include the JDBC JAR on your classpath when you compile, but you also need to include it on your classpath when you execute it as well:


java -cp .:/path/to/sqljdbc42.jar connectURL

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

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