[Solved] java.sql.SQLException: No suitable driver

java.sql.SQLException: No suitable driver” occurred while connecting with Database by using JDBC api DriverManager.getConnection();

Sample Code

package com.fioit.examples.jdbc.connection;

import java.sql.DriverManager;
import java.sql.SQLException;

public class SQLServerConnection {

	public static void main(String[] args) {
		String db_url      = "jdbc:microsoft:sqlserver://HOST:1433;DatabaseName=DATABASE";
		String db_driver   = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
		String db_username = "USERNAME";
		String db_password = "PASSWORD";
		try
		{
		//Class.forName(db_driver);
		DriverManager.getConnection(db_url,db_username,db_password);
		}
//		catch(ClassNotFoundException ex)
//		{
//			ex.printStackTrace();
//		}
		catch(SQLException ex)
		{
			ex.printStackTrace();
		}

	}
}

Output


java.sql.SQLException: No suitable driver found for jdbc:microsoft:sqlserver://HOST:1433;DatabaseName=DATABASE
    at java.sql.DriverManager.getConnection(DriverManager.java:689)
    at java.sql.DriverManager.getConnection(DriverManager.java:247)
    at com.fioit.examples.jdbc.connection.SQLServerConnection.main(SQLServerConnection.java:16)

Issue

java.sql.SQLException: No suitable driver” occurred while connecting with Database by using JDBC api DriverManager.getConnection();

Solutions

  1. Include SQL deriver jar in your class path. For example for above case add SQLSerer driver jar in class path.
  2. Before Java 8 version: include below line for loading SQL driver

Class.forName(“sql.vendor.driver”);

For example: un comment Class.forName() line froom sample code.

In Java 8 or after version no need to write Class.forName() because it will load driver automatically based on connection URL.

More Issues Solution

For more other JAVA/JDBC issues solution follow link JAVA/JDBC Issues.

Advertisements

This site uses Akismet to reduce spam. Learn how your comment data is processed.