java如何设置SQL server连接URL?
我在连接我在电脑中为一个小应用程序创建的SQL Server数据库时遇到问题。服务器名为'INSPIRONN5110-D'。我使用Windows身份验证登录到服务器。已从microsoft下载驱动程序并将其内容复制到program files文件夹,并将路径(C:\program files\microsoft JDBC driver 4.0 for SQL Server\sqljdbc_4.0\chs\sqljdbc4.jar)添加到path和classpath变量(不确定要使用哪一个)中,但仍会出现此错误
Creating Connection.
com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection to the host INSPIRONN5110-D, port 1433 has failed. Error: "connect timed out. Verify the connection properties. Make sure that an instance of SQL Server is running on the host and accepting TCP/IP connections at the port. Make sure that TCP connections to the port are not blocked by a firewall.".
.....
下面是我尝试连接到数据库的方法
private Connection connectToDB() {
String connectionUrl = "jdbc:sqlserver://INSPIRONN5110-D; databaseName=MemoryHelp;";
Connection con = null;
try{
// Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
System.out.println("Creating Connection.");
con = DriverManager.getConnection(connectionUrl);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
// } catch (ClassNotFoundException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
} finally{
System.out.println("Connection created.");
}
return con;
}
已尝试将连接url更改为“jdbc:sqlserver://localhost; databaseName=MemoryHelp;"
现在获取此错误:
Creating Connection.
com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection to the host localhost, port 1433 has failed. Error: "Connection refused: connect. Verify the connection properties. Make sure that an instance of SQL Server is running on the host and accepting TCP/IP connections at the port. Make sure that TCP connections to the port are not blocked by a firewall.".
....
终于解决了。必须转到SQL Server Configuration Manager并启用到服务器的tcp/ip连接,然后复制sqljdbc_身份验证。从驱动程序文件夹到system32文件夹的dll文件,并使用以下连接URL可以使其工作:
String connectionUrl = "jdbc:sqlserver://localhost;databaseName=MemoryHelp;integratedSecurity=true";
# 1 楼答案
您没有提供服务器的ip或主机名,也没有提供端口。为了连接到服务器,您需要提供这些
在您的情况下,如果服务器与应用程序位于同一台计算机上。。那么应该是: jdbc:sqlserver://localhost:3306
端口3306只是我假设您正在使用的默认端口,这取决于您托管服务器的w.e端口