如何解决java.sql.SQLException: No suitable driver found for SQLServer 2008 R2?

huangapple go评论84阅读模式
英文:

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

问题

我有一个连接到SQLServer的示例代码如下:

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("已连接");

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

当我执行这段代码时,我得到了以下异常:

java.sql.SQLException: 无法找到适用的驱动程序以连接到 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)
英文:

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)

答案1

得分: 1

我认为SQLServer的jar文件可能不支持,或者连接字符串中有拼写错误。

从这里下载与SQLServer 2008 R2兼容的jar文件:

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

步骤:

  • 点击下载:

如何解决java.sql.SQLException: No suitable driver found for SQLServer 2008 R2?

  • 如果显示,选择sqljdbc_6.0.8112.200_enu.tar.gzsqljdbc_6.0.8112.200_enu.zip

如何解决java.sql.SQLException: No suitable driver found for SQLServer 2008 R2?

  • 点击下一步开始下载。

  • 下载完成后,解压内容。现在,进入sqljdbc_6.0/enu/jre8sqljdbc_6.0/enu/jre7,根据你正在使用的jdk版本,复制jar文件。

如何解决java.sql.SQLException: No suitable driver found for SQLServer 2008 R2?

将该jar文件添加到项目的类路径中。

修复这一行:

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

通过去除空格,修改为以下内容。

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

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 :

如何解决java.sql.SQLException: No suitable driver found for SQLServer 2008 R2?

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

如何解决java.sql.SQLException: No suitable driver found for SQLServer 2008 R2?

  • 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.

如何解决java.sql.SQLException: No suitable driver found for SQLServer 2008 R2?

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");

答案2

得分: 0

似乎您需要一个支持您的SQL数据库类型和版本的JDBC驱动程序。也许您的驱动程序版本是错误的?
您应该查看这个链接:https://javarevisited.blogspot.com/2016/09/javasqlsqlexception-no-suitable-driver-mysql-jdbc-localhost.html
在第689行和第270行,DriverManager在做什么?

英文:

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?

huangapple
  • 本文由 发表于 2020年9月16日 22:11:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/63921910.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定