nazanin n
nazanin n

Reputation: 131

How to resolve java.sql.SQLException: No suitable driver found for SQLServer 2008 R2?

I have a sample code to connect to SQLServer is given below :

Connection conn=null;
try {
    Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
     conn= DriverManager.getConnection(" jdbc:sqlserver://localhost:1433;instance=SQLEXPRESS;databaseName=Test" );

    System.out.println("connected");


} catch (ClassNotFoundException e) {
    e.printStackTrace();
} catch (SQLException e) {
    e.printStackTrace();
}

When I execute this code, I'm getting an exception given below :

java.sql.SQLException: No suitable driver found for jdbc:sqlserver://localhost:1433;databaseName=Test
    at java.sql.DriverManager.getConnection(DriverManager.java:689)
    at java.sql.DriverManager.getConnection(DriverManager.java:270)
    at JDBCSample.main(JDBCSample.java:14)

Upvotes: 0

Views: 3958

Answers (2)

Anish B.
Anish B.

Reputation: 16469

I think the jar for SQLServer is not supporting or typo in the connection string.

Download SQLServer 2008 R2 compatible jar from here :

https://www.microsoft.com/en-in/download/details.aspx?id=11774

Steps :

  • Click on Download :

enter image description here

  • Select sqljdbc_6.0.8112.200_enu.tar.gz or sqljdbc_6.0.8112.200_enu.zip if shown.

enter image description here

  • Click on Next to start downloading.

  • After downloading, extract the content. Now, go into sqljdbc_6.0/enu/jre8 or sqljdbc_6.0/enu/jre7and copy the jar based on the jdk you are using.

enter image description here

Add the jar in the classpath of the project.

Fix this line

Connection conn = DriverManager.getConnection(" jdbc:sqlserver://localhost:1433;databaseName=Test" );

to this by removing space.

Connection conn = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=Test");

Upvotes: 1

Uniqueusername
Uniqueusername

Reputation: 45

Seems like you need an JDBC driver supporting your type and version of your SQL Database. Maybe your version of the driver is the wrong one? You should check this out: https://javarevisited.blogspot.com/2016/09/javasqlsqlexception-no-suitable-driver-mysql-jdbc-localhost.html What is the DriverManager doing at lines 689 and 270?

Upvotes: 0

Related Questions