英文:
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
步骤:
- 点击下载:
- 如果显示,选择
sqljdbc_6.0.8112.200_enu.tar.gz
或sqljdbc_6.0.8112.200_enu.zip
。
-
点击下一步开始下载。
-
下载完成后,解压内容。现在,进入
sqljdbc_6.0/enu/jre8
或sqljdbc_6.0/enu/jre7
,根据你正在使用的jdk版本,复制jar文件。
将该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 :
- Select
sqljdbc_6.0.8112.200_enu.tar.gz
orsqljdbc_6.0.8112.200_enu.zip
if shown.
-
Click on Next to start downloading.
-
After downloading, extract the content. Now, go into
sqljdbc_6.0/enu/jre8
orsqljdbc_6.0/enu/jre7
and copy the jar based on the jdk you are using.
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?
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论