简体   繁体   English

java.lang.ClassNotFoundException:com.sqlserver.jdbc.Driver

[英]java.lang.ClassNotFoundException: com.sqlserver.jdbc.Driver

I'm trying to connect with MS SQL Server using a Java Application. 我正在尝试使用Java应用程序与MS SQL Server连接。 This is the code I'm using: 这是我正在使用的代码:

public static void main(String[] args)  {
    try {  
        Class.forName("com.sqlserver.jdbc.Driver");                                   
        Connection con=DriverManager.getConnection("jdbc:sqlserver:<SERVER>","user","password");  
        Statement stmt = con.createStatement();  
        ResultSet rs = stmt.executeQuery("SELECT TOP 1 [MsgTextArabic] FROM 
     [FactBulkSMS].[dbo].[Messages] order by SendingDateTime desc");

        while(rs.next())  
            System.out.println(rs.getInt(1)+""+rs.getString(2)+""+rs.getString(3));
    }
    catch(Exception e) { 
        System.out.println(e);
        e.printStackTrace();
    }

When running this code, the following Exception is thrown: 运行此代码时,将引发以下异常:

java.lang.ClassNotFoundException: com.sqlserver.jdbc.Driver

java.lang.ClassNotFoundException: com.sqlserver.jdbc.Driver
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Unknown Source)
    at test.main(test.java:13)

The Exception is complaining, that Class.forName("com.sqlserver.jdbc.Driver"); 异常在抱怨, Class.forName("com.sqlserver.jdbc.Driver"); is unable to find the JDBC driver. 无法找到JDBC驱动程序。

According to the Docs, you need to add the path where you put the driver into the ClassPath Variable. 根据文档,您需要添加将驱动程序放入ClassPath变量的路径。 From Eclipse this can be done by importing the .jar files into the project and referencing them. 在Eclipse中,可以通过将.jar文件导入项目并引用它们来完成此操作。

Source and further information: https://docs.microsoft.com/en-US/sql/connect/jdbc/using-the-jdbc-driver 来源和更多信息: https : //docs.microsoft.com/zh-CN/sql/connect/jdbc/using-the-jdbc-driver

It seems that the JVM can not find the class com.sqlserver.jdbc.Driver , this is most likely because the JAR has not been included in your project's classpath. 看来JVM找不到类com.sqlserver.jdbc.Driver ,这很可能是因为JAR尚未包含在项目的类路径中。

Since you are using eclipse I would recommend adding the JAR via Eclipse's "build path". 由于您使用的是eclipse,因此建议您通过Eclipse的“构建路径”添加JAR。

Start by checking which is the correct JAR for your set-up from the Microsoft docs HERE You can then download the JAR from the Maven repo 通过检查这是从Microsoft文档您的设置正确的JAR启动HERE然后,您可以从下载JAR Maven的回购

Then inside Eclipse, Right click your project >> Build Path >> Configure Build Path. 然后在Eclipse中,右键单击您的项目>>构建路径>>配置构建路径。 Select the Libraries tab, then on the right column click on, "Add External JARs" 选择“库”选项卡,然后在右列上单击“添加外部JAR”

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

相关问题 java.lang.ClassNotFoundException:com.microsoft.jdbc.sqlserver.SQLServerDriver:我是否正在加载正确的驱动程序? - java.lang.ClassNotFoundException: com.microsoft.jdbc.sqlserver.SQLServerDriver : Am I loading the right driver? java.lang.ClassNotFoundException:com / microsoft / sqlserver / jdbc / SQLServerDriver - java.lang.ClassNotFoundException: com/microsoft/sqlserver/jdbc/SQLServerDriver java.lang.ClassNotFoundException:com.microsoft.sqlserver.jdbc.SQLServerDriver - java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver java.lang.ClassNotFoundException:com.jdbc.mysql.Driver-Java - java.lang.ClassNotFoundException: com.jdbc.mysql.Driver - Java java.lang.ClassNotFoundException:com.mysql.jdbc.Driver - java.lang.ClassNotFoundException: com.mysql.jdbc.Driver 错误:java.lang.ClassNotFoundException:com.mysql.jdbc.Driver? - error: java.lang.ClassNotFoundException: com.mysql.jdbc.Driver? java.lang.ClassNotFoundException:com.sql.jdbc.Driver - java.lang.ClassNotFoundException: com.sql.jdbc.Driver java.lang.ClassNotFoundException: com.mysql.jdbc.Driver in Eclipse - java.lang.ClassNotFoundException: com.mysql.jdbc.Driver in Eclipse Java.lang.ClassnotfoundException com.mysql.jdbc.Driver - Java.lang.ClassnotfoundException com.mysql.jdbc.Driver java.lang.ClassNotFoundException:com.mysql.jdbc.Driver - java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM