简体   繁体   中英

ClassNotFoundException for “com.microsoft.sqlserver.jdbc.SQLServerDriver”

This is a repeated question but I did everything right as mentioned. I have added following maven repo dependency to pom.xml of my project:

  <dependency>
    <groupId>com.microsoft.sqlserver</groupId>
    <artifactId>mssql-jdbc</artifactId>
    <version>6.1.0.jre8</version>
    <scope>test</scope>
</dependency>

And this is my java code running on 1.8.0_144:

    package com.demo;

    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.Statement;

    public class Test {
    Connection con;
    Statement st;
    PreparedStatement ps;

     Test(){
          try {
                            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            con =       DriverManager.getConnection("jdbc:sqlserver://localhost:1521;user=SYSTEM;password=oracle;sid=xe");
            if(con != null) {
                System.out.println("Connected to MSsql !!");
            }

            //st = con.createStatement();


        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    public static void main(String[] args) {

        new Test();
        System.out.println(System.getProperty("java.version"));
    }
}

Most probably test scope is not appropriate, if you're running your code not as a maven test.

Try to change the scope to <scope>compile</scope> .

If it does not help, please post or submit the whole project, so the case would be reproducible.

You can read about the scopes here http://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html#Dependency_Scope .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM