问题
I have this error/exception-
SQL Exception: com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection to the host localhost, port 1433 has failed. Error: "connect timed out. Verify the connection properties, check that an instance of SQL Server is running on the host and accepting TCP/IP connections at the port, and that no firewall is blocking TCP connections to the port.".
and my code is-
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String connectionUrl = "jdbc:sqlserver://localhost:1433";
Connection con = DriverManager.getConnection(connectionUrl,"","");
System.out.println("ok");
} catch (SQLException e) {
System.out.println("SQL Exception: "+ e.toString());
}
catch (ClassNotFoundException cE) {
System.out.println("Class Not Found Exception: "+ cE.toString());
}
how can i sovle it,please help me.
回答1:
This could mean almost anything - basically, it was impossible to connect to port 1433 on localhost. Could be a network problem, could be that the database is not started, could be that it is running but not bound to port 1433.
In a pinch, you can always open a shell and run
telnet localhost 1433
to see if it is possible to make a connection at all.
回答2:
I had this error.
I fixed it by going into the SQL Server Configuration Manager.
Under SQL Server 2005 Configuration -> Protocols
for SQLEXPRESS. Look at the IP Address
tab of the Properties
of the TCP/IP Protocol
.
At the very bottom of the protocol list (under IPALL), the TCP Port field was blank. I put 1433 there, saved, restarted the SQL service and it worked great :)
回答3:
It's telling you it can't connect.
Check that sql server is available on your local machine on that port.
Check that sql server is accepting connections with the blank name and password you're providing in your getConnection() invocation.
Check that your jdbc url is in the correct form, they're sometimes manufacturer/driver specific.
回答4:
The error just means that java was not able to connect to your database. Either database is not running or your firewall is blocking connections to it. If you are sure your DB server is up on your localhost, try after disabling your windows firewall.
回答5:
Lets look at some of the possibilities:
The database server is not listening on localhost:1433 : you probably would get a "connection refused" rather than a "connection timed out"
The database server is catatonic (i.e. in a state where it is not responding to requests) : you could get a "connection timed out".
The database server is massively overloaded : you could get a "connection timed out", but you would probably notice that the machine was very slow.
The local software firewall is configured to block all requests to port 1433 : you could get either "connection refused" or "connection timed out" ... or some other response. (It depends exactly what the firewall does with unwanted traffic.)
The loopback interface (for IP
127.0.0.x
) is not configured : you are more likely to get "no route to host" or "no route to network".The DNS entry for
localhost
is misconfigured (i.e.localhost
doesn't resolve to127.0.0.x
) - lots of things could happen ... depending on how it is misconfigured.
Here are some things you could do to diagnose the problem ... depending on your system:
Look at the configured networks using
ipconfig
orifconfig
.Look at "/etc/hosts" to check the entry for
localhost
Try to connect to the service using
telnet -p 1433
Use "ps" or the task manager to see what processes are running.
Check the local firewall configs to ensure that
1433
is open for (at least) TCP from the configuredlocalhost
IP address (probably127.0.0.1
).
回答6:
I had a problem similar to this, and solved it by adding the line:
System.setProperty("java.net.preferIPv4Stack", "true");
The problem was it used IPv6 instead of IPv4 to access the db host (I used it for a DB on a remote host, not localhost). Hope it helps.
来源:https://stackoverflow.com/questions/5521050/java-to-sql-server-connectivity-exception