我只是想在我的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?
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] 删除。
我来说两句