0

I'm trying to connect to an external database and no matter what I do I keep getting the following error:

java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver
    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 DbCon.main(DbCon.java:10)

My code to set up the driver:

import java.sql.*;

public class DbCon {

    public static void main(String[] args) {

        try {
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
        } catch (ClassNotFoundException e1) {
            e1.printStackTrace();
        }
    }
}

I've tried omitting the driver entirely, and just trying to connect with:

String connectionUrl = "jdbc:sqlserver://xxx.xxx.xxx.xxx:1433;databaseName=xxxx;user=xxxx;password=xxxx;";
        try {
            Connection con = DriverManager.getConnection(connectionUrl);
        } catch (SQLException e) {
            e.printStackTrace();
        }

This only gets me the following error:

java.sql.SQLException: No suitable driver found for xxxx
    at java.sql.DriverManager.getConnection(Unknown Source)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at DbCon.main(DbCon.java:15)

I've tried adding the jar to Eclipse's classpath, I've added the folder to the project's build path as an external class folder.

Is there something I'm missing?

8
  • 1
    possible duplicate of stackoverflow.com/questions/15241960/… Commented Nov 29, 2016 at 0:19
  • Try simply omitting the call to Class.forName and just call DriverManager.getConnection with your connection URL. With modern versions of Java and JDBC drivers the Class.forName is often unnecessary. Commented Nov 29, 2016 at 0:27
  • @Scary I've looked at that, tried those suggestions. Commented Nov 29, 2016 at 0:35
  • @Gord When I try that, I get the error "No suitable driver found" Commented Nov 29, 2016 at 0:35
  • Did you check the contents of the jar file? What class is available? Commented Nov 29, 2016 at 0:38

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.