无法通过JDBC连接到SQL Server。找不到适用于jdbc:sqlserver://的驱动程序

水冷却器v2

我只是想在我的HTTP Servlet程序中使用SQL Server数据库,但是我的程序似乎无法连接到数据库。它给了我以下错误:

找不到适用于jdbc:sqlserver:// localhost:1433; databaseName = Bookyard; integratedSecurity = true;的驱动程序

这是我的连接方法。

package practice.bookyard.server.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class Database {

    public static Connection getConnection() {
        String url = "jdbc:sqlserver://(LocalDb)\\MSSQLLocalDB:1433;databaseName=Bookyard;integratedSecurity=true;";
        Connection connection = null;

        try {
            connection = DriverManager.getConnection(url);
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return connection;
    }

}

我之前拥有服务器名称,localhost:1433但我将其更改为SQL Server实例名称,(LocalDb)\\MSSQLLocalDB:1433但它似乎仍然使用旧名称。

另外,我不确定在连接到SQL Server localdb时如何提供正确的连接字符串。

我正在使用适用于Java EE,Mars 2的Eclipse,并且从该网站下载了用于SQL 6.0的Microsoft JDBC驱动程序

我运行安装,解压缩了结果文件夹的内容。然后,在我以JDK 1.8为目标时,将sqljdbc42.jar文件添加到了构建路径。

在此处输入图片说明

更新

根据Scary Wombat的建议,我还将sqljdbc42.jar文件的路径添加到了我的类路径中。

在此处输入图片说明

但是,我仍然遇到相同的错误。

我非常有信心这是一个反射问题,因为类型加载器无法从我的连接字符串解析驱动程序类型。这意味着我使用的连接字符串语法错误。

我将连接字符串更改为如下:

String url = "jdbc:sqlserver://localhost:1433;
instance=(LocalDb)\\MSSQLLocalDB;
databaseName=Bookyard;integratedSecurity=true;";

However, I still not only get the same error but the exception message I receive still has my old connection string. So, clearly, there's also some caching going on, I just don't know where. Who is caching my connection string and how do I refresh / clear that cache?

Could you please tell me how to provide a SQL Server instance name if I am connecting to localdb and not on the main SQL Server instance?

Water Cooler v2

As @ScaryWombat and @JozefChocholacek had hinted, it turned out to be a class path issue. Apparently, you have to copy and paste just the sqljdbc42.jar file, and this file only directly into the WEB-INF\lib folder and not within any sub-folder.

I did that it still gave me that error.

那是因为WEB-INF当我在中查看文件夹结构时,它Project Explorer仍然具有旧的文件夹结构。因此,我在中的WEB-INF文件夹上单击鼠标右键Project Explorer然后选择Refresh命令。

我还更新了我的环境变量,CLASSPATH将其指向WEB-INF文件夹,该错误消失了。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

java.sql.SQLException:找不到适用于jdbc:microsoft:sqlserver的驱动程序

找不到适用于jdbc的驱动程序:postgresql://192.168.1.8:5432 / NexentaSearch

SQLException:找不到适用于jdbc:oracle:thin:@ // localhost:1521 / orcl的驱动程序

找不到适用于'jdbc:mysql:// localhost:3306 / mysql的驱动程序

Java:找不到适用于jdbc:h2的驱动程序

如何解决java.sql.SQLException:找不到适用于jdbc:derby的驱动程序:

在应用程序映像上找不到适用于jdbc:sqlite的驱动程序

java.sql.SQLException:找不到适用于jdbc:mysql:// localhost:3306 / database的驱动程序

SQLException:找不到适用于jdbc:sqlserve:z / localhost:1433; databaseName = isTaskDB; user = is; password = 1234的驱动程序

我还想念什么?我收到一个错误:找不到适用于jdbc的驱动程序

如何解决:找不到适用于jdbc:sqlserver的驱动程序

为什么找不到适用于jdbc:derby:derbyDB的驱动程序

SQLException:找不到适用于url = jdbc:derby的驱动程序

找不到适用于jdbc:mysql:// localhost:3306 / rom的驱动程序(Payara 5,Windows 10)

如何修复“找不到适用于jdbc:mysql // localhost:3306 / gaming_site的驱动程序”错误

春季战争文件“ java.sql.SQLException:找不到适用于jdbc:postgresql的驱动程序:”

如何解决:找不到适用于jdbc:mysql:// localhost:3306 / sampledb的驱动程序

找不到适用于jdbc:derby:myDBtest的驱动程序

Scala错误:java.sql.SQLException:找不到适用于jdbc:calcite的驱动程序:

如何修复NetBeans中找不到适用于jdbc:mysql:// localhost:3306 / XXXX的合适驱动程序

找不到适用于jdbc:oracle:thin:@localhost:1521:xe错误的驱动程序

无法使用融合的CLI设置CLASSPATH:java.sql.SQLException:找不到适用于jdbc:oracle:thin的驱动程序

找不到合适的驱动程序(Postgres JDBC):java.sql.SQLException:找不到适用于jdbc:postgresql:// rds-service:5432 / postgres的驱动程序

找不到适用于jdbc:mysql:// localhost:3306 / finalproject的驱动程序

java.sql.SQLException:找不到适用于jdbc:sqlite的驱动程序

找不到适用于jdbc:mysql / localhost:3306 / world的驱动程序

在具有Maven(Java)的服务器上找不到适用于jdbc:mysql的驱动程序

找不到适用于jdbc:mariadb(Netbeans)的驱动程序

无法使用 Spring JDBC 通过 SSL 连接到 SQL Server