java.sql.SQLException: No suitable driver found for jdbc:microsoft:sqlserver java.sql.SQLException: No suitable driver found for jdbc:microsoft:sqlserver sql-server sql-server

java.sql.SQLException: No suitable driver found for jdbc:microsoft:sqlserver


Your URL should be jdbc:sqlserver://server:port;DatabaseName=dbname
and Class name should be like com.microsoft.sqlserver.jdbc.SQLServerDriver
Use MicrosoftSQL Server JDBC Driver 2.0


Following is a simple code to read from SQL database.Database names is "database1".Table name is "table1".It contain two columns "uname" and "pass".Dont forget to add "sqljdbc4.jar" to your project. Download sqljdbc4.jar

public class NewClass {    public static void main(String[] args) {        Connection conn = null;        String dbName = "database1";        String serverip="192.168.100.100";        String serverport="1433";        String url = "jdbc:sqlserver://"+serverip+"\\SQLEXPRESS:"+serverport+";databaseName="+dbName+"";        Statement stmt = null;        ResultSet result = null;        String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";        String databaseUserName = "admin";        String databasePassword = "root";        try {            Class.forName(driver).newInstance();            conn = DriverManager.getConnection(url, databaseUserName, databasePassword);            stmt = conn.createStatement();            result = null;            String pa,us;            result = stmt.executeQuery("select * from table1 ");            while (result.next()) {                us=result.getString("uname");                pa = result.getString("pass");                              System.out.println(us+"  "+pa);            }            conn.close();        } catch (Exception e) {            e.printStackTrace();        }    }}


For someone looking to solve same by using maven. Add below dependency in POM:

<dependency>    <groupId>com.microsoft.sqlserver</groupId>    <artifactId>mssql-jdbc</artifactId>    <version>7.0.0.jre8</version></dependency>

And use below code for connection:

String connectionUrl = "jdbc:sqlserver://localhost:1433;databaseName=master;user=sa;password=your_password";try {    System.out.print("Connecting to SQL Server ... ");    try (Connection connection = DriverManager.getConnection(connectionUrl))        {        System.out.println("Done.");    }} catch (Exception e) {    System.out.println();    e.printStackTrace();}

Look for this link for other CRUD type of queries.