简体   繁体   English

在代码上获取 ClassNotFoundException:“Class.forName(”com.microsoft.sqlserver.jdbc.SqlServerDriver“);”

[英]Getting ClassNotFoundException on code: “Class.forName(”com.microsoft.sqlserver.jdbc.SqlServerDriver“);”

This is my first Java application and I'm completely inexperienced with Java and NetBeans .这是我的第一个Java应用程序,我对JavaNetBeans完全没有经验。

I have been trying to connect to sql and get some records for 2 days.我一直在尝试连接到 sql 并获取一些记录 2 天。 The problem is about jdbc driver, let me explain.问题是关于jdbc驱动程序,让我解释一下。 I have downloaded sqljdbc driver and then followed these steps:我已经下载了sqljdbc驱动程序,然后按照以下步骤操作:

Right-click Project->Select Properties->On the left-hand side click Libraries->Under Compile tab - click Add Jar/Folder button and select sqljdbc4.jar file.右键单击项目-> 选择属性-> 在左侧单击库-> 在编译选项卡下 - 单击添加 Jar/文件夹按钮并选择sqljdbc4.jar文件。 Then it should be ok, right?那应该没问题吧?

Then I wrote this code But I cant get rid of this exception:然后我写了这段代码但是我无法摆脱这个异常:

  Exception in thread "main" 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:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:190)
at javaapplication1.JavaApplication1.main(JavaApplication1.java:30)

This is the code这是代码

public static void main(String[] args) throws ClassNotFoundException, SQLException {
    String url = "jdbc:sqlserver://.\\SQLEXPRESS;databaseName=Northwind; Integrated Security = SSPI ";

    Connection con = null;
    Statement stmt = null;

    ResultSet rs = null;
    try {
        Class.forName("com.microsoft.sqlserver.jdbc.SqlServerDriver");

        con = DriverManager.getConnection(url);
        String sql = "Select Top 3 from * person.Contact";
        stmt = con.createStatement();
        rs = stmt.executeQuery(sql);
        while (rs.next()) {
            System.out.println(rs.getString(1));
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

According to this page , the class is called SQLServerDriver and not SqlServerDriver .根据此页面,该类称为SQLServerDriver而不是SqlServerDriver Case is important!案例很重要!

So, try:所以,试试:

Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");

Note that with newer versions of JDBC, it's not necessary to load the driver class explicitly with Class.forName(...) .请注意,对于较新版本的 JDBC,没有必要使用Class.forName(...)显式加载驱动程序类。 The page I linked to explicitly explains that you don't have to do it.我链接到的页面明确说明您不必这样做。 So, you can just remove the whole line and then it should work.因此,您可以删除整行,然后它应该可以工作。

Java: JDBC Connectivity with MSSQL in NetBeans Java:在 NetBeans 中使用 MSSQL 的 JDBC 连接

Steps脚步

  1. Download JDBC from: https://www.microsoft.com/en-in/download/details.aspx?id=11774从以下位置下载 JDBC: https : //www.microsoft.com/en-in/download/details.aspx?id=11774
  2. Run sqljdbc__enu.exe - unpack this zip file in %Program Files (x86)% with the default directory: Microsoft JDBC DRIVER for SQL Server运行 sqljdbc__enu.exe - 将此 zip 文件解压到 %Program Files (x86)% 中,默认目录为:Microsoft JDBC DRIVER for SQL Server
  3. Create your new project in NetBeans在 NetBeans 中创建新项目
  4. Right Click on the project - select Properties - select Libraries from left panel - click Add JAR/Folder button - select your JAR file and open - ok右键单击项目 - 选择属性 - 从左侧面板中选择库 - 单击添加 JAR/文件夹按钮 - 选择您的 JAR 文件并打开 - 确定
  5. Open Sql Server Configuration Manager - select Protocols for SQLEXPRESS under Sql Server Network Configuration - Right Click on TCP/IP - select Properties - change Enable to Yes - Click IP Addresses - Goto IPAll - Change TCP Dynamic Ports to 49169 and TCP Port to 1433 - Apply - Ok - Restart the Computer打开Sql Server配置管理器-在Sql Server网络配置下选择SQLEXPRESS的协议-右键单击TCP/IP-选择属性-将启用更改为是-单击IP地址-转到IPAll-将TCP动态端口更改为49169,将TCP端口更改为1433-应用 - 确定 - 重新启动计算机
  6. Open Run and type Services.msc - Start SQL Server Browser打开运行并键入 Services.msc - 启动 SQL Server 浏览器
  7. Goto project and write code for database connectivity.转到项目并编写用于数据库连接的代码。

Code for Local Database Connectivity:本地数据库连接代码:

String url = "jdbc:sqlserver://YOUR PC NAME;instanceName=SQLEXPRESS;DatabaseName=dbname;integratedSecurity=true";

Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection myCon = DriverManager.getConnection(url);

Statement myStmt = myCon.createStatement();
ResultSet myRs = myStmt.executeQuery("select * from table name");

I have an update about this issue.我有关于这个问题的更新。

  1. Go to this link, find your compatible JDBC driver (I dowloaded 6.0 version).转到链接,找到您兼容的 JDBC 驱动程序(我下载的是 6.0 版本)。
  2. Find the appropriate jar in the file you downloaded (I used jre7\\sqljdbc41.jar).在您下载的文件中找到合适的 jar(我使用的是 jre7\\sqljdbc41.jar)。
  3. For Intellij Idea press Ctrl+Shift+Alt+S and open Project Structure then in the dependencies section add your jar file.对于 Intellij Idea,按Ctrl+Shift+Alt+S并打开项目结构,然后在依赖项部分添加您的 jar 文件。

I hope it works for you too.我希望它也适用于你。

暂无
暂无

声明:本站的技术帖子网页,遵循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 用于“ 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 无法加载com.microsoft.sqlserver.jdbc.SQLServerDriver类? - unable to load class com.microsoft.sqlserver.jdbc.SQLServerDriver? com.microsoft.sqlserver.jdbc.SQLServerDriver类未找到异常(新) - com.microsoft.sqlserver.jdbc.SQLServerDriver class not found exception (NEW) Why is my code giving me the java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver error in my web application? - Why is my code giving me the java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver error in my web application? 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
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM