我试图连接到安装在远程系统上的ms sql server 2008.但它显示错误。以下是我试过的方式
import java.io.File;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
public class mssql {
public static void main(String[] args) {
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver").newInstance();
Connection connection=DriverManager.getConnection("jdbc:sqlserver://192.168.1.220:1433;databaseName=sales;integratedSecurity=true;");
if(!(connection==null))
{
System.out.println("connected");
}
//
} catch (Exception e) {
e.printStackTrace();
}
}
}
这是我得到的错误
com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection to the host 192.168.1.220, 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.".
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(SQLServerException.java:190)
at com.microsoft.sqlserver.jdbc.SQLServerException.ConvertConnectExceptionToSQLServerException(SQLServerException.java:241)
at com.microsoft.sqlserver.jdbc.SocketFinder.findSocket(IOBuffer.java:2243)
at com.microsoft.sqlserver.jdbc.TDSChannel.open(IOBuffer.java:491)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(SQLServerConnection.java:1309)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.login(SQLServerConnection.java:991)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerConnection.java:827)
at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.java:1012)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at mssql.main(mssql.java:14)
针对此问题的任何解决方案
答案 0 :(得分:1)
对于listening
连接,2008 SQL实例是否配置为TCP
?
启动,Microsoft SQL Server 2008,配置工具,SQL Server配置管理器
SQL Server网络配置
[实例名称]的协议
应列出四个项目:
对于您的环境,应该启用哪些环境以及哪些应该禁用?大多数设置都要求启用共享内存和TCP / IP,其他设置已禁用。
答案 1 :(得分:0)
public class mssql {
public static void main(String[] args) {
try {
//Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver").newInstance(); modify
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
Connection connection=DriverManager.getConnection("jdbc:sqlserver://192.168.1.220:1433;databaseName=sales;integratedSecurity=true;");
if(!(connection==null))
{
System.out.println("connected");
}
//
} catch (Exception e) {
e.printStackTrace();
}
}
}
然后你看看这篇文章:
答案 2 :(得分:0)
尝试从命令行进行连接:
sqlcmd -S 192.168.1.220 -E
另外,请检查:
答案 3 :(得分:0)
-first使您的sql server能够从配置管理器接收tcp连接。 -add sqljdbc库来项目。 - 将库的.dll文件添加为vm参数ex:-Djava.library.path = ...... - 使用下面的代码作为示例:
String connectionUrl = "jdbc:sqlserver://localhost;" +
"databaseName=Timesheet;integratedSecurity=true;";
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
con = DriverManager.getConnection(connectionUrl);
String SQL = "select top 100 * from paycal";
stmt = con.createStatement();
rs = stmt.executeQuery(SQL);
int i =0;
while (rs.next()) {
jTable1.setValueAt(rs.getString("dayname"), i, 0);
jTable1.setValueAt(rs.getString("dater"), i, 1);
i++;
}