简体   繁体   中英

“No suitable driver found” when trying to connect to SQL Server via JDBC

I need to use JDBC to connect to SQL Server with Windows Authentication.

Here is the code I wrote:

String connect="jdbc:sqlserver://datababasehost:portnumber/databasename";       
Connection con=  (Connection) DriverManager.getConnection(connect,"username","password");

This is throwing an exception indicating "No suitable driver found".

What am I missing?

(From comments to the question...)

Here is the name of the jar file, mysql-connector-java-5.1.39-bin.jar

The MySQL JDBC driver cannot be used to connect to Microsoft SQL Server. The "No suitable driver found" error is occurring because Java cannot find an available JDBC driver that can handle a connection URL of the form

jdbc:sqlserver://...

You need to replace the MySQL JDBC driver with a JDBC driver that can connect to SQL Server, such as the Microsoft JDBC Driver for SQL Server .

There is a very nice introduction: HOW TO: Get Started with Microsoft JDBC

In short:

  1. Download a driver from Microsoft JDBC Drivers 6.0, 4.2, 4.1, and 4.0 for SQL Serve

  2. Install it somewhere

  3. Make sure that the driver JAR is on your CLASSPATH

  4. In your program, register the driver

     Driver d = (Driver)Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance(); 
  5. Continue with the code, you already have.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM