0

This is my code to connect to MS SQL Server from JAVA.. It is producing the error : java.sql.SQLException: No suitable driver found for.

The driver I have downloaded from Microsoft's site.

mssql-jdbc-7.4.1.jre12.jar mssql-jdbc-7.4.1.jre12-shaded.jar

Few details :

Server Name : DESKTOP-2LFJRMD DB / Instance Name : Test_Learning

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.SQLException;

public class MakeConnectionToSQL {
    public static void main(String[] args) {
        String varConnectionString;
        Connection varConnection;
        String varUser;
        String varPass;
        varConnectionString = " jdbc:sqlserver: " + 
        " //DESKTOP-2LFJRMD\\ " +
        " databaseName=Test_Learning";
        varUser = "sa";
        varPass = "Mukesh@1507";
        try
        {
            varConnection = DriverManager.getConnection
            (
            varConnectionString, 
            varUser, 
            varPass
            );
            System.out.println("Yesss, Connected");
        }
        catch(SQLException e)
        {
            System.out.println("Oh No, Connection Failed");
            e.printStackTrace();
        }
    }
}

1 Answers1

0

Register jdbc driver for SQL Server using :

   DriverManager.registerDriver(new com.microsoft.sqlserver.jdbc.SQLServerDriver());

or

   Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Somil Garg
  • 474
  • 4
  • 12
  • Hello All.. I got connection working, but somehow.. the error messages are pretty strange, seeing the log.. it does not mention anything about the version of JRE.. - and that is where the problem was lying.. i had downloaded SQL driver for 12 and was using JRE 8.. and so, the connection was not working.. the moment i changed the JRE path from 8-12, it all started working.. strange. - but, thanks all. Thanks, Mukesh L. – Mukesh Lekhrajani Oct 26 '19 at 11:31