简体   繁体   English

com.microsoft.sqlserver.jdbc.SQLServerDriver的ClassNotFoundException

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

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. 这是以前在SO上出现过的问题,我确信经验丰富的Java开发人员已经厌倦了告诉新手如何设置类路径。 That said, I've tried setting the classpath via environment variable and with the -cp option, with no success. 也就是说,我尝试通过环境变量和-cp选项设置类路径,但没有成功。

I'm using the sample applications that are bundled with the SQLServer JDBC driver. 我正在使用与SQLServer JDBC驱动程序捆绑在一起的示例应用程序。 I'm running Ubuntu 14.10. 我正在运行Ubuntu 14.10。 I compile the app on the command line: 我在命令行上编译应用程序:

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

and the run it: 并运行它:

java connectURL

which gets me the familiar ClassNotFoundException : 这让我熟悉了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) {}
        }
    }
}

The path to the SQL JDBC .jar is definitely correct. SQL JDBC .jar的路径绝对正确。 If I add import com.microsoft.sqlserver.jdbc.SQLServerDriver; 如果我添加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. 到类文件时,编译时没有任何抱怨,但在运行时仍会收到ClassNotFoundException

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 . 我在其他地方读过,较新版本的JDBC不需要您通过Class.forName加载驱动程序,但是如果我删除该行,则可以预料地得到java.sql.SQLException: No suitable driver found

What am I doing wrong? 我究竟做错了什么? I'm sure the .jar is being loaded and the class is being found, because if I try eg import com.microsoft.sqlserver.jdbc.SomeNonExistentClass; 我确定正在加载.jar并找到了该类,因为如果尝试例如import com.microsoft.sqlserver.jdbc.SomeNonExistentClass; I get: 我得到:

connectURL.java:2: error: cannot find symbol

My Java details: 我的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)

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: 编译时,您将JDBC JAR包含在类路径中,但是在执行时,还需要将其包含在类路径中:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 com.microsoft.sqlserver.jdbc.SQLServerDriver的ClassNotFoundException - ClassNotFoundException for com.microsoft.sqlserver.jdbc.SQLServerDriver 用于“ com.microsoft.sqlserver.jdbc.SQLServerDriver”的ClassNotFoundException - ClassNotFoundException for “com.microsoft.sqlserver.jdbc.SQLServerDriver” java.lang.ClassNotFoundException:com.microsoft.sqlserver.jdbc.SQLServerDriver - java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver java.lang.ClassNotFoundException:无法在新的 docker 容器上加载 class:com.microsoft.sqlserver.jdbc.SQLServerDriver - java.lang.ClassNotFoundException: Unable to load class: com.microsoft.sqlserver.jdbc.SQLServerDriver on new docker container java.lang.ClassNotFoundException:创建jar后的com.microsoft.sqlserver.jdbc.SQLServerDriver - java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver after creating a jar error.java.lang.ClassNotFoundException:com.microsoft.sqlserver.jdbc.SQLServerDriver - error.java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver java.lang.ClassNotFoundException:com.microsoft.sqlserver.jdbc.SQLServerDriver找不到javax.net.ssl.SSLSocket - java.lang.ClassNotFoundException: javax.net.ssl.SSLSocket not found by com.microsoft.sqlserver.jdbc.SQLServerDriver 类路径设置,但是:java.lang.ClassNotFoundException:com.microsoft.sqlserver.jdbc.SQLServerDriver - Classpath set, but: java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver 从 Java 连接到 SQ 服务器时出错:java.lang.ClassNotFoundException:com.microsoft.sqlserver.jdbc.SQLServerDriver - Error connecting to SQ Server from Java: java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver 在代码上获取 ClassNotFoundException:“Class.forName(”com.microsoft.sqlserver.jdbc.SqlServerDriver“);” - Getting ClassNotFoundException on code: “Class.forName(”com.microsoft.sqlserver.jdbc.SqlServerDriver“);”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM