【问题标题】:java.lang.ClassNotFoundException: com.microsoft.jdbc.sqlserver.SQLServerDriver : Am I loading the right driver?java.lang.ClassNotFoundException: com.microsoft.jdbc.sqlserver.SQLServerDriver : 我是否加载了正确的驱动程序?
【发布时间】:2015-10-20 13:38:00
【问题描述】:

当我运行以下 sn-p 连接到数据源以通过查询获取数据时:

        try {
        String driver = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
        Class.forName(driver);
        String Url = "jdbc:sqlserver://localhost:1433;databaseName=movies";
        Connection connection = DriverManager.getConnection(Url,"sa", "xxxxxxx);
        //            Context initContext = new InitialContext();
        //            Context envContext = (Context)initContext.lookup("java:comp/env");
        //            DataSource ds = (DataSource)envContext.lookup("");
        //            Connection connection = ds.getConnection();
        String query_name = "select name from list where name='" + data + "'";

显示此异常跟踪:

java.lang.ClassNotFoundException: com.microsoft.jdbc.sqlserver.SQLServerDriver
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:276)
at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:169)
at gui.MainGui.startSqlServerDataSearch(MainGui.java:148)
at gui.MainGui$3.run(MainGui.java:129)
at java.lang.Thread.run(Thread.java:619)

为什么我会得到这个异常?我已经与 sql server 建立了连接(通过加载给定 here 的驱动程序)并且可以看到在 sql server 中定义的数据库。以下快照说明了这一点:

【问题讨论】:

    标签: java sql-server jdbc datasource classnotfoundexception


    【解决方案1】:

    已定义数据库和Class.forName(driver);还不够。

    您需要确保适当的数据库驱动程序 jar 在类路径中。

    【讨论】:

    • 我把从microsoft.com/en-us/download/…下载的jar保存在classpath中
    • 确保类路径中没有任何其他版本的 jar。
    • @Y.E.P 将驱动程序字符串更改为 com.microsoft.sqlserver.jdbc.SQLServerDriver 而不是 com.microsoft.jdbc.sqlserver.SQLServerDriver
    猜你喜欢
    • 2020-10-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-27
    • 2011-06-21
    • 1970-01-01
    相关资源
    最近更新 更多