java.sql.SQLException: No suitable driver found for jdbc:microsoft:sqlserver(java.sql.SQLException:找不到适合 jdbc:microsoft:sqlserver 的驱动程序)
问题描述
我在尝试运行此程序时遇到此异常.这是微软的例子之一.我已通过项目属性将 sqljdbc4.jar 添加到 netbeans 中的类路径中,用于编译和运行.我还测试了可以通过使用下面的导入语句找到该类 - 编译期间没有错误,所以它必须找到 jar.
I'm getting this exception when I try to run this program. It's one of the Microsoft examples. I've added the sqljdbc4.jar to the classpath in netbeans for both compile and Run, via the project properties. I also tested that the class could be found by using an import statement below - no error during compile, so it must be finding the jar.
它是否与 sqldbc4.jar 引用的 dll 或某些 sql dll 相关?
Could it be related to a dll or some sql dll that the sqldbc4.jar references?
这是确切的例外,下面是确切的代码,除了密码.
This is the exact exception, and below is the exact code, except for password.
例外:
run:
java.sql.SQLException: No suitable driver found for jdbc:microsoft:sqlserver://localhost:1433;databaseName=HealthCareDatabase
Error Trace in getConnection() : No suitable driver found for jdbc:microsoft:sqlserver://localhost:1433;databaseName=HealthCareDatabase
Error: No active Connection
at java.sql.DriverManager.getConnection(DriverManager.java:602)
at java.sql.DriverManager.getConnection(DriverManager.java:185)
at javaapplication1.Connect.getConnection(Connect.java:35)
at javaapplication1.Connect.displayDbProperties(Connect.java:50)
at javaapplication1.JavaApplication1.main(JavaApplication1.java:23)
BUILD SUCCESSFUL (total time: 1 second)
代码:
package javaapplication1;
import com.microsoft.sqlserver.jdbc.SQLServerDriver;
import java.*;
public class Connect {
private java.sql.Connection con = null;
private final String url = "jdbc:microsoft:sqlserver://";
private final String serverName = "localhost";
private final String portNumber = "1433";
private final String databaseName = "HealthCareDatabase";
private final String userName = "larry";
private final String password = "xxxxxxx";
// Constructor
public Connect() {
}
private String getConnectionUrl() {
return url + serverName + ":" + portNumber + ";databaseName=" + databaseName ;
}
private java.sql.Connection getConnection() {
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
con = java.sql.DriverManager.getConnection(getConnectionUrl(), userName, password);
if (con != null) {
System.out.println("Connection Successful!");
}
} catch (Exception e) {
e.printStackTrace();
System.out.println("Error Trace in getConnection() : " + e.getMessage());
}
return con;
}
public void displayDbProperties() {
java.sql.DatabaseMetaData dm = null;
java.sql.ResultSet rs = null;
try {
con = this.getConnection();
if (con != null) {
dm = con.getMetaData();
System.out.println("Driver Information");
System.out.println(" Driver Name: " + dm.getDriverName());
System.out.println(" Driver Version: " + dm.getDriverVersion());
System.out.println("
Database Information ");
System.out.println(" Database Name: " + dm.getDatabaseProductName());
System.out.println(" Database Version: " + dm.getDatabaseProductVersion());
System.out.println("Avalilable Catalogs ");
rs = dm.getCatalogs();
while (rs.next()) {
System.out.println(" catalog: " + rs.getString(1));
}
rs.close();
rs = null;
closeConnection();
} else {
System.out.println("Error: No active Connection");
}
} catch (Exception e) {
e.printStackTrace();
}
dm = null;
}
private void closeConnection() {
try {
if (con != null) {
con.close();
}
con = null;
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws Exception {
Connect myDbTest = new Connect();
myDbTest.displayDbProperties();
}
}
推荐答案
你的 URL 应该是 jdbc:sqlserver://server:port;DatabaseName=dbname
和类名应该像 com.microsoft.sqlserver.jdbc.SQLServerDriver
使用 MicrosoftSQL Server JDBC 驱动程序2.0
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
这篇关于java.sql.SQLException:找不到适合 jdbc:microsoft:sqlserver 的驱动程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:java.sql.SQLException:找不到适合 jdbc:microsoft:sqlserver 的驱动程序
基础教程推荐
- 如何在不安装整个 WTP 包的情况下将 Tomcat 8 添加到 Eclipse Kepler 2022-01-01
- 如何强制对超级方法进行多态调用? 2022-01-01
- 如何使用 Eclipse 检查调试符号状态? 2022-01-01
- 如何对 HashSet 进行排序? 2022-01-01
- Java 中保存最后 N 个元素的大小受限队列 2022-01-01
- 由于对所需库 rt.jar 的限制,对类的访问限制? 2022-01-01
- 在螺旋中写一个字符串 2022-01-01
- Spring Boot Freemarker从2.2.0升级失败 2022-01-01
- 如何使用 Stream 在集合中拆分奇数和偶数以及两者的总和 2022-01-01
- 首次使用 Hadoop,MapReduce Job 不运行 Reduce Phase 2022-01-01