0

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)
Anish B.
  • 7,321
  • 3
  • 12
  • 33
nazanin n
  • 91
  • 1
  • 7
  • 3
    Why is there a space before `jdbc` in your connection url? And it should be `database` not `databaseName` last time I did this, but that could be synonymous or have changed in versions – BeUndead Sep 16 '20 at 14:17
  • Use the site search box, there are many questions with that same error such as [this one](https://stackoverflow.com/questions/1911253/the-infamous-java-sql-sqlexception-no-suitable-driver-found). Update your question with how your situation is different and what you have already tried. – Sander Sep 16 '20 at 14:17

2 Answers2

1

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");
Anish B.
  • 7,321
  • 3
  • 12
  • 33
0

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?

  • my SQL server is 2008 r2 which version I should use? – nazanin n Sep 16 '20 at 14:33
  • https://docs.microsoft.com/en-us/sql/connect/jdbc/microsoft-jdbc-driver-for-sql-server-support-matrix?view=sql-server-ver15 Check out the "SQL version compatibility" I think thats what you are looking for. – Uniqueusername Sep 16 '20 at 14:36