I'm trying to connect to MS SQL Server (running on my machine) from a Java program. I'm getting the following long winded exception:
Exception in thread "main" 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, 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.".
When I check "Properties" and click "View Connection Properties" in the Object Explorer of MS SQL, I find that the "Server is Unavailable." This seems possibly related to the exception message.
How can I make the server available?
Edit:
I am using SQL Server 2008, and I have now enabled TCP/IP, and restarted my instance. I am still told that "Server is unavailable."
Any other ideas?
I ran into this problem as well.
The MSKB article applies to SQL server 2005.
As the "SQL Server Surface Area Configuration" tool has been dropped in lieu of "Facets" - this wasn't obvious to me.
I resolved this by setting the TCPAll port and enabling the relevant IP.
Steps
Open the Sql Server Configuration Manager (Start -> Programs -> Microsoft SQL Server 2008 -> Configuration Tools)
Expand SQL Server Network Configuration -> [Your Server Instance]
Double click TCP/IP
Under Protocol:
Ensure Enabled is Yes
Under IP Addresses:
Scroll to the bottom and set the TCP Port under IPAll, (1433 by default)
Find the IP address you want to connect to and set Enabled and Active to Yes
Before messing about with connections, first check that the SQL Server Service is actually running.
You can do this by either using the SQL Server Configuration Manager (located in the configuration tools folder) or in the standard services console in the Windows control panel.
Once you have checked the service is up and running, you need to ensure that SQL Server has been configured to allow remote connections.
See below for an explanation on how to do this:
http://support.microsoft.com/kb/914277
I am using SQL Server 2012 and have had same issues.
Please note when you activate TCP/IP there are 2 options:
Active - YES - If you just click yes on this it wont work
Enabled - click yes on this too. I did not pay attention to this
and was wondering why its was not working, wasting a lot of time in
the process
You can test listening ports with this:
netstat -an
Depending on the version of SQL Server you're using, it may well be set up only to use named pipes by default.
You need to enable TCP/IP connections to the server. If you can tell us which version of SQL server you're using, we can give more precise instructions for how to enable TCP/IP.
As usual, check the obvious:
1. See if your router has ports 1433 and 1434 enabled (these are the defaults for IP/UDP communication)
2. Ensure that if you have a firewall in place on either the client or the server, that said ports are not blocked (this is the one that was my "gotcha").
3. Go into Sql Configuration Manager and under Sql Server Network Configuration, ensure that TCP/IP is enabled. Also, in the same program under Sql Native Client 1.0 (if installed) that it, too has TCP/IP enabled.
A cheap and sleazy way to ensure that all of your connection information is working is to create a .udl file (anywhere on your system), fill in the appropriate information and hit "Test Connection". If there is something wrong there, you'll get a (fairly) informative message as to the problem.
If you don't know about .udl files, right click in Windows explorer (right side pane) to create a "New Text File". Type any acceptable name but change the extension from .txt to .udl. You will be asked to confirm the extension change, answer yes. Once created, click the file (or double click if you don't use single click) to open it. There are 4 tabs. The first allows you to select the provider. For Visual studio use the Sql Native client, for any other program try the standard Sql Server Provider. On the second tab you can fill in the server name, sql server user name and password (if used), etc. Then test the connection. One other item to note - if you are using Windows Authentication, ensure that those users have been added to the Server Logins.
Open up Sql Server Configuration Manager. ( Start | Programs | Whatever version of sql server | Configuration Tools)
Browse down to 'SQL Server Services' and restart your instance. However you could do this in Management Studio by right clicking on the instance and selecting restart.
If your restart fails then, check out Computer Management | Event Viewer | Application and look for sql server events. it will record successful and error messages here.
Here are my screenshots. If you can't read the words, then download the image or copy and paste the image into Paint.
Inside Glassfish:
"General" Tab:
"Additional Properties" Tab
URL= jdbc:sqlserver://localhost;databaseName=NETEAV
Inside Microsoft SQL Server Management Studio:
Check is the connection is blocked by Firewall or not. If third-party Firewall is active then it may block the TCP connection. After this step-
1. Check TCP enabled on or not
a. Open SQL Server Configuration Manager
b. SQL Server network configuration
c. Protocols for MSSQLSERVER
d. TCP – Enable it
e. Also check listen all – Yes
f. In IP Addresses tab, IPAll – port number should be 1433
First off, check that the sql server service is running. If you're using SQL 2005 or 2008, check Configuration manager (2008) or Surface are configuration tool (2005) to make sure the TCP/IP protocol is enabled and TCP/IP connections are allowed. With SSE(express) these are off by default, which would cause your problem. Also just in case you're running multiple instances, you may need SQL browser service running. If this is the case, you should be able to connect object explorer by using (local) as the server address, since this will use a local/shared memory connection.
From 2005 and up the SQL server browser service has te be running.
That one fooled me many times.
Same error observed while connecting to SQL Server 2014.
I logged in the sql server to ensure that the correct port number was set.
For my case the Port number was not set for the sql instance.
Hence I removed the Port number from the connection string url, then I was able to connect .
Anybody facing this issue, can try this way
Related
I am new to Oracle, and am trying to run a simple example code with Java, but am getting this error when executing the code.. I am able to start up the listener via CMD and am also able to run SQL Plus. Can anyone give me a hand and tell me what I might be doing wrong?
Update:
I am using JDBC.
Database is local, and I actually had it working but it stopped working just today. I'm not really sure why though. Would you mind giving me some procedures to follow by since I don't know much.
Either:
The database isn't running
You got the URL wrong
There is a firewall in the way.
(This strange error message is produced by Oracle's JDBC driver when it can't connect to the database server. 'Network adapter' appears to refer to some component of their code, which isn't very useful. Real network adapters (NICs) don't establish connections at all: TCP protocol stacks do that. It would have been a lot more useful if they had just let the original ConnectException be thrown, or at least used its error message and let it appear in the stack trace.)
I had the same problem, and this is how I fixed it.
I was using the wrong port for my connection.
private final String DB_URL = "jdbc:oracle:thin:#localhost:1521:orcll"; // 1521 my wrong port
go to your localhost
(my localhost address) : https://localhost:1158/em
login
user name
password
connect as --> normal
Below 'General' click on LISTENER_localhost
look at you port number
Net Address (ADDRESS=(PROTOCOL=TCP)(HOST=localhost)(PORT=1522))
Connect to port 1522
Edit you connection
change port 1521 to 1522.
done
Another thing you might want to check that the listener.ora file matches the way you are trying to connect to the DB. If you were connecting via a localhost reference and your listener.ora file got changed from:
HOST = localhost
to
HOST = 192.168.XX.XX
then this can cause the error that you had unless you update your hosts file to accommodate for this. Someone might have made this change to allow for remote connections to the DB from other machines.
I figured out that in my case, my database was in different subnet than the subnet from where i was trying to access the db.
I had this error when i renamed the pc in the windows-properties. The pc-name must be updated in the listener.ora-file
Most probably you have listener configured wrongly, the hostname you specify in connection string must be the same as in the listener.
First check the Firewall and network related issues.
Check if Oracle Listener service is available and running. If not you may use Oracle Net Configuration Assistant tool to add and register new listener.
If the above steps are ok then you need to configure Oracle Listener appropriately. You may use Oracle Net Manager tool or edit “%ORACLE_HOME%\network\admin\listener.ora” file manually.
There are 2 options that need to be considered carefully:
Listening Locations associated with the Listener – Hostname(IP) and Port in Listening Location must exactly match the ones used in the connection string.
For example, if you use 192.168.74.139 as target hostname, then there must be Listening Location registered with the same IP address.
Also make sure the you use the same SID as indicated in Database Service associated with the Listener.
https://adhoctuts.com/fix-oracle-io-error-the-network-adapter-could-not-establish-the-connection-error/
IO Error: The Network Adapter could not establish the connection (CONNECTION_ID=iKQM6lBbSLiArrYuDqud8A==)
if you are facing this issue
1- make sure you have downloaded oracle databases like oracle 11g,19c, 21c, or any latest databases.
2- search for services in your computer or type win+r then services.mis then search for oracleservice you will find orcl or xe or any other sid like oracleserviceorcl;
after that you can test your connection using sql developer, sql plus or cmd
To resolve the Network Adapter Error I had to remove the - in the name of the computer name.
In my case, I needed to specify a viahost and viauser. Worth trying if you're in a complex system. :)
For me the basic oracle only was not installed. Please ensure you have oracle installed and then try checking host and port.
I was having issues with this as well. I was using the jdbc connection string to connect to the database. The hostname was incorrectly configured in the string. I am using Mac, and the same string was being used on Windows machines without an issue. On my connection string, I had to make sure that I had the full url with the appending "organizationname.com" to the end of the hostname.
Hope this helps.
Just try to re-create connection. In my situation one of jdbc connection stopped working for no reason. From console sqlplus was working ok.
It took me 2 hours to realize that If i create the same connection - it works.
I am using sqljdbc4.jar for connecting SQL Server 2008 with java application using Windows authentication. Everything works perfectly up to date until I tried to run the same code with SQL Server 2012.
After research, I came to know that I need to enable the TCP/IP. But my problem is, my applications will be distributed to users in the form of .EXE and users will install the application themselves. Also, all users will have the SQL SERVER 2012 pre-installed, my application is not supposed to install the sql server.
Now my question is, how can I connect to SQL server without enabling TCP/IP manually? I heard about shared memory protocol also, but not sure how its connection string will look like?
I am using below connection url till SQL Server 2008:
jdbc:sqlserver://localhost;instanceName=SQLServer12;databaseName=Test;integratedSecurity=true;SelectMethod=direct;responseBuffering=adaptive
Please give your suggestions so I can try it here. Thanks in advance.
Try adding the property Network Library=dbmslpcn; OR Net=dbmslpcn; to the connection string for Shared Memory. The default value is dbmssocn (TCP/IP).
Network Library: The network library used to establish a connection to an instance of SQL Server. The corresponding network DLL must be installed on the system to which you connect. If you do not specify a network and you use a local server (for example, "." or "(local)"), shared memory is used.
I got this from: https://www.connectionstrings.com/all-sql-server-connection-string-keywords
I have a Java (Swing) desktop connected to a godaddy MySQL database (online database). Till the time database was offline, this was not at all the problem, but now I get this exception when I try to access some menu which involves a database access.
Also, I create a static connection object when my application start, and never close it when the application is running.
This is the description of Exception, please help if you can. Thanks :)
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link fai
lure
Last packet sent to the server was 47 ms ago.
SOME MORE STACK TRACE AND THEN THE CHAINED CAUSE
Caused by: java.io.EOFException: Can not read response from server. Expected to
read 4 bytes, read 0 bytes before connection was unexpectedly lost.
I am assuming that you are on a shared hosting. Connecting to such a hosting publicly is not advisable.
Dealing with the problem this seems a problem with Remote SQL. There are a few things you can do
1. Go to your cPanel or equivalent and navigate to Remote SQL add the address 0.0.0.0 which will allow connections from any public ip that may want to connect with it. If your are using a vpn like AWS you have to manually edit the my.cnf or my.ini depending on weather you are hosted on linux or windows, add
bind-address = 0.0.0.0
under the
[mysqld]
tag
Next you need to create a user which can be accessed from any ip as root user only has permission to be accessed locally
GRANT ALL ON yourdatabasename.* TO user#'%' IDENTIFIED BY 'yourRootPassword';
So I wrote this jar that interacts with a database and also serves as a server. Problem is that I can only interact with that jar when I'm on the lan or run the jar from a different computer. The system with the problem is running windows xp with the firewall disabled and yes it is correctly set up with the router as I can launch a web server and view it remotely. I have no clue what the problem would be as the firewall is disabled and the jar works on other computers on the same network.
Note: I was noticing an exception "java.net.SocketException: Address family not supported by protocol family" earlier
1.Open Network Connections
2.Right-click any local area connection, and then click Properties.
3.Click Install.
4.In the Select Network Component Type dialog box, click Protocol, and then click Add.
5.In the Select Network Protocol dialog box, click Microsoft TCP/IP version 6, and then click OK
6.Click Close to save changes to your network connection.
Lastly Restart your application
See this: http://www.martinahrer.at/2009/04/16/javanetsocketexception-address-family-not-supported-by-protocol-family-bind/
Generally the problem is related to IPv6 support. Try to google your exception text and I am sure you will find an answer quickly.
I have developed an application in java that access remote mysql database. While I am running it by netbeans IDE of system which have running that wamp server. But while i try I make connection in another system to remote system database by netbeans it shows following error.
Unable to add connection, Cannot establish a connection to jdbc:mysql://192.168.1.14:3306/test using(CommunicationsException: Communications link failure
Last packet sent to the server was 0 ms ago.
Please, kindly help me.
Thanks in Advance
From mysql forums
You could be getting this because (1) the URL of the DB is wrong, because (2) the DB isn't set up to accept connections from the web host, or because (3) some intermediate networking component is misconfigured. (1) is your problem; (2) and (3) might be your problem or the web hosting's problem, depending on where the DB is located, what administrative privileges on the DB that you have, and how the networking is set up.
You need to have the right privileges to be able to connect to MySQL remotely. There are several tools available to set it up.
Here is an article outlining several steps of which the grant step is most commonly needed.
mysql> GRANT ALL ON foo.* TO bar#'202.54.10.20' IDENTIFIED BY 'PASSWORD';
There has also been problems reported when connecting to MySQL databases in Windows Vista, but i'm not sure wether this is relevant to this case or not.
Do you have ssh access to the server? I would have run a tunnel with ssh, plink or putty (ssh -L 3305:127.0.0.1:3306 192.168.1.14) and then use this connection url
jdbc:mysql://127.0.0.1:3305/test
It would be easier to assist if we'd known your operating system.
If this is a deployment situation, opening the firewall for port 3306 from your IP address is probably the right thing. In linux you might find the settings in /etc/sysconfig/iptables, but your sysadmin may have other safe guards in place. You must also verify that mysql is actually listening on the IP-address, and not only localhost.
MySQL is standard protected so you can't access it remotely. You'll have to grant MySQL as well as the user connecting to MySQL access from outside the MySQL-machine.
Perhaps a low-level network issue.
Can you ping that IP ?
Can you telnet to that IP/port ?
e.g. telnet 192.168.1.14 3306
Establishing whether you can talk to the machine and whether you can create a basic TCP connection to the MySQL process on that machine will tell you a lot - is your network sound, is a process listening on that port etc.