SQLException error when connecting with java - exception

I have been facing a simple sql connection error, and i m tired of fixing it. I installed sql server 2014 and then sql server 2017, add rules to firewall and even turned off the firewall. services restarted multiple times.
Went to configuration manager, enabled everything, added all the jar required, done clean and build, done all searches on this issue in google, still that same issue comes in again and again. System used are
- Windows 10.
- Sql server management studio 2017.
- Netbean 8.1
code :-
import java.sql.*;
public class DbConnect {
public static void main(String[] args) throws SQLException,ClassNotFoundException {
String url = "jdbc:sqlserver://localhost:1433;databaseName=productlist;user=db2017;password=db2017";
//commented :-String url = "jdbc:sqlserver://DESKTOP-7CI6DU0\\NIT2017:1433;databaseName=productlist;user=db2017;password=db2017";
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection conn = DriverManager.getConnection(url);
System.out.println("test");
Statement sta = conn.createStatement();
String Sql = "select * from productlist";
ResultSet rs = sta.executeQuery(Sql);
while (rs.next()) {
System.out.println(rs.getString("CatName"));
} } }
Below is exception error comes again again, after so much troubleshooting.
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.".
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(SQLServerException.java:171)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(SQLServerConnection.java:1033)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.login(SQLServerConnection.java:817)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerConnection.java:700)
at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.java:842)
at java.sql.DriverManager.getConnection(DriverManager.java:571)
at java.sql.DriverManager.getConnection(DriverManager.java:233)
at DbConnect.main(DbConnect.java:11)
C:\Users\Nitish\AppData\Local\NetBeans\Cache\8.1\executor-snippets\run.xml:53: Java returned: 1
Before giving up, thought to check here...

Your commented-out connection string suggests you are connecting to a named instance. A default SQL Server instance (not-named) listens on port 1433 unless configured otherwise. A named instance uses a dynamic port, which is not 1433, unless configured otherwise. One can have only one default instance per machine and every SQL instance must listen on a different port.
You can identify the port a SQL instance is listening on using SQL Server Configuration Manager or by viewing the SQL Server error log. The error log will include messages for each interface and port SQL Server is listening on. The message will be like "Server is listening on [ 'any' 1433]." The port can be changed using SSCM and will be effective after restarting the SQL Service.
To test port connectivity, you can use TELNET:
TELNET YourServer 1433
You'll see and empty window if the connection is successful, otherwise and error.
If you don't have TELNET installed, you can test the port connectivity with this Powershell command from a command-prompt window:
powershell -Command echo ((new-object Net.Sockets.TcpClient).Client.Connect('YourServer', 1433)) 'success'
You'll see the success message upon successful connection, otherwise a socket exception.
Note that the dynamic port is assigned during installation and the instance will attempt to use the same port upon each startup. However, be aware the port number could change if that port is unavailable at startup. One can configure a static port (including port 1433) using SSCM to avoid the port number changes of a named instance.

Related

Actionscript ServerSocket Verify Connection

In flash, we can create a server socket with this:
protected var socket:ServerSocket = new ServerSocket();
protected function createServer():void
{
socket.bind(1234,"0.0.0.0");
socket.addEventListener(ServerSocketConnectEvent.CONNECT, clientConnectedHandler);
// start listening for connections
socket.listen();
}
With this, any flash application try to connect to the server socket with the port 1234 will get connected. How can I do verification on connection to make sure it is connected from my application before accepting the connection on server socket?
Thank you.
SockerServer instance will throw errors on failure:
RangeError — This error occurs when localPort is less than 0 or greater than 65535.
ArgumentError — This error occurs when localAddress is not a
syntactically well-formed IP address. IOError — when the socket
cannot be bound, such as when: the underlying network socket (IP and
port) is already in bound by another object or process. the
application is running under a user account that does not have the
privileges necessary to bind to the port. Privilege issues typically
occur when attempting to bind to well known ports (localPort < 1024)
this ServerSocket object is already bound. (Call close() before
binding to a different socket.) when localAddress is not a valid local
address.
You can use try..catch statements to handle those errors.
If those errors aren't thrown, opening port went successfully :)

Login failed for user 'sa'

I am trying to connect to a sql server in my asp.net application. I have given connection string in my web.config as follows :
I am getting following error while running the application :
"A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)"
If i use remote server's ip address as data source in connection string, I get following error
"Login falied for user 'sa'"
I researched and found some ports need to be opened between the server and my machine, which I got done by network team, still the same issue. How to resolve this ?
You can not directly. you need to have TCP/IP enabled and configured the TCP/IP Ports on sql server configuration manager at remote server.
you need to have TCP/IP connectivity .Just launch it, enter the DNS host name or IP address in the Server Name' box and hit Connect. The hosting company needs to have enabling TCP/IP on your SQL Server instance, and them providing you with secure access to the IP address that instance is running on.
Most administrators do not allow direct access to the SQL Server from outside the firewall. In that case, if you can connect to the host over VPN then you should be able to connect directly to the server with Enterprise Manager or SQL Management Studio.
Please go through it Configurations-Remote-Server
Are you porting code from Hypersonic or H2? If so, the username sa won't work.

Network error while connecting to sql server

I am using sql server 2008 and jtds driver to establish a connection between sql server and the jsp application. I code i used is
java.sql.SQLException: Network error IOException: Connection refused: connect
String url= "jdbc:jtds:sqlserver://127.0.0.1/sfts";
String id= "sfts";
String pass = "sfts";
try{
Class.forName("net.sourceforge.jtds.jdbc.Driver");
con = java.sql.DriverManager.getConnection(url, id, pass);
}catch(Exception e){
out.println(e);
}
While running this code, it showing error as
java.sql.SQLException: Network error IOException: Connection refused: connect
TCP/IP was set enabled and port number was set to 1433. But still it is showing this error.. What to do now??
Goto AllProgram->SQL Server 2008->configuraton tools->SQL Server Configuration Manager->SQL Server Network Configuration->
Select your server instance and right click on TCP/IP then goto IPAddresses. Change/Place port number for all IP's as 1433. Save it and restart the server.
It Should solve your problem
I use SQL EXPRESS 2014. I went to All Programs->SQL Server 2014->Configuration Tools->SQL Server Configuration Manager->SQL Server Network Configuration->Protocols for SQLEXPRESS
Right clicked on TCP/IP. In the TCP/IP Window, went to the option IPAll, then writed 1433 in TCP Port textbox.
Restarted the SQLEXPRESS Service and the connection worked as expected.
Hope this help others with the same problem.
Make Sure that your SQL Server Browser service is active.
Perform the check as said by #RAVITEJA SATYAVADA. Also make sure that the IP Addresses in the IP Address tab is Enabled.

Connection String for Remote Connections to SQL Server 2008

I am trying to connect remotely to SQL Server 2008 R2 Express on a server running Windows Server 2008 R2. I receive the following error:
A network-related or instance-specific error occurred while
establishing a connection to SQL Server. The server was not found or
was not accessible. Verify that the instance name is correct and that
SQL Server is configured to allow remote connections. (provider: TCP
Provider, error: 0 - A connection attempt failed because the connected
party did not properly respond after a period of time, or established
connection failed because connected host has failed to respond.)
I think the problem might be related to my connection string, because I am not sure exactly what it should be. I understand the connection string should take the form:
Data Source=123.123.123.123\InstanceName;Initial Catalog=MyDBName;user id=MyUserName;password=MyPassword;
The instance name of my SQL Server installation takes the form ComputerName\SQLEXPRESS. This is what I see when I log in to Management Studio on the server. So should the connection string begin with
Data Source=123.123.123.123\ComputerName\SQLEXPRESS
This looks wrong because of all the slashes. I have tried to omit the computer name thus:
Data Source=123.123.123.123\SQLEXPRESS
I also read somewhere that you can specify a port so I have also tried
Data Source=123.123.123.123,1433\SQLEXPRESS
because I understand that 1433 is the TCP port used by SQL Server and
Data Source=123.123.123.123,1434\SQLEXPRESS
because I understand that 1434 is the UDP port used by SQL Server browser.
I have also tried every combination of these and they all give the same error.
These are the steps I have taken to enable remote browsing on the server:
In Management Studio, right click the instance, go to Properties, Security and check SQL Server and Windows Authentication Mode. On the connections tab I have checked "Allow remote connections to this computer".
In SQL Server Configuration Manager I have enabled all four connection options (Shared Memory, Named Pipes, TCPIP and Via) under every node they occur. Under protocols for SQL Express I have tried specifying port 1433 and also leaving it blank with TCP Dynamic Ports set to 0 (which I think is meant to enable dynamic ports).
I have created firewall exceptions for TCP Port 1433, UDP Port 1434, and program exceptions for sqlservr.exe and sqlbrowser.exe.
If someone could tell me which of the four versions of the DataSource part of the connection string I should be using it would be a great help, even if it did not solve the problem. It would mean that everything I try subsequently I would only have to test once instead of four times.
Thanks in advance for any help!
Where did you enter the firewall exceptions, on your machine, on the server, or both?
The second connection string is the only really valid one. Can you telnet to the server on port 1433? Can you connect to the instance from Management Studio remotely and the problem is only from your code where the connection string is? Did you try running the same code on the server? Did you try forcing TCP/IP (vs. named pipes/shared memory etc) adding the following parameter to your connection string:
Network=DBMSSOCN;
This question comes up quite a lot and I'm sure you're hitting something that others have hit before. Have you gone through this article, "How to troubleshoot connecting to the SQL Server Database Engine"? Also there are 20+ questions on here that mention this error message and Express, according to this search result. I suggest double-checking your settings against some of the items in those answers that solved the issue for other users.

SQL Server connection string to a remote server

I have a remote SQL Server Express (2008 R2) with an IP: xx.xxx.xxx.xx and an instance name: myInstance.
I have been trying to connect to a DB (myDB) as user (dbUser) and with password (myPass).
I have tried various string combinations but none works. I am sure I got it wrong because the server is ok, I can ping the IP Address, SQL Server runs on port 1433, which is open. I have also enabled browser service and remote connection on the server.
Please someone give me the correct string...
How to: Configure Express to accept remote connections
There could be many reasons for this, however, you didn't post any useful details such as error messages or what exception was thrown (if any).
Check that Windows Firewall allows access on port 1433 - this is the most likely reason for the failure, since you are using an IP address and not the name of the instance.
I suggest looking at http://connectionstrings.com to check your connection string.