SQLServer - SQLServerException with jdbc access - java

I have installed on my pc ms sqlserver express 2012, I am trying to reach it via jdbc but I am not successfull, I get the error:
com.microsoft.sqlserver.jdbc.SQLServerException: 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
I am accessing via the following url: "jdbc:sqlserver://localhost:1433;database=mytest;"
I enabled the tcp/ip via the Sql server configuration manager, but if I run netstat-an I can't see the 1433 port listening.
I disabled windows firewall too...
I am lost :(
Any help?
Thank you!
****UPDATE**
I could overcome the problem by enabling port 1433 on IpAll under Sql Server configuration manager - Sql Server Network configuration - Protocols for SQLEXPRESS - tcp-ip Properties - Ip Addresses. But now I get another error:com.microsoft.sqlserver.jdbc.SQLServerException: Login failed for user 'sa'. ClientConnectionId
Any ideas? thanks!

The answer for the error:
com.microsoft.sqlserver.jdbc.SQLServerException: Login failed for user 'sa'. ClientConnectionId
is:
Go to Object explorer, click on properties (right click on Sql Server name), and on the security tab click on Sql Server and Windows authentication mode.
This worked for me.

Related

Java web app can't connect to sql server [duplicate]

I want to connect Java class file with SQL server 2012. I have logged in with SQL server authentication, but I am receiving an error when connecting.
Error:
The TCP/IP connection to the host 127.0.0.1, 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.".
My code:
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); //1. Register your driver
//2. get the connection object
//Connection con = DriverManager.getConnection("jdbc:sqlserver://localhost;databaseName=aysha","sa","admin");
Connection con = DriverManager.getConnection("jdbc:sqlserver://127.0.0.1;databaseName=aysha","user=sa","password=admin");
//"jdbc:sqlserver://127.0.0.1:1433; Instance=SQL2008;" + "databaseName=MB;user=sa;password=123;";
//Connection con = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=aysha","sa" , "password");
//3. Prepare a statement
Statement stmt = con.createStatement();
//4. Write the query`
String sql = "Select * from employee";
//5. Execute the statement and
ResultSet rs = stmt.executeQuery(sql);
//6. Process the result set
while (rs.next())
{
System.out.println(rs.getInt(1));
}
Open SQL Server Configuration Manager, and then expand SQL Server 2012 Network Configuration.
Click Protocols for InstanceName, and then make sure TCP/IP is enabled in the right panel and double-click TCP/IP.
On the Protocol tab, notice the value of the Listen All item.
Click the IP Addresses tab:
If the value of Listen All is yes, the TCP/IP port number for this instance of SQL Server 2012 is the value of the TCP Dynamic Ports item under IPAll.
If the value of Listen All is no, the TCP/IP port number for this instance of SQL Server 2012 is the value of the TCP Dynamic Ports item for a specific IP address.
Make sure the TCP Port is 1433.
Click OK.
Easy Solution
Got to Start->All Programs-> Microsoft SQL Server 2012-> Configuration Tool -> Click SQL Server Configuration Manager ->Expand SQL Server Network Configuration-> Protocol ->Enable TCP/IP Right box
Double Click on TCP/IP and go to IP Adresses Tap and Put port 1433 under TCP port.
The error is self explanatory:
Check if your SQL server is actually up and running
Check SQL server hostname, username and password is correct
Check there's no firewall rule blocking TCP connection to port 1433
Check the host is actually reachable
A good check I often use is to use telnet, eg on a windows command prompt run:
telnet 127.0.0.1 1433
If you get a blank screen it indicates network connection established successfully, and it's not a network problem. If you get 'Could not open connection to the host' then this is network problem
Go to Start->All Programs-> Microsoft SQL Server 2012-> Configuration Tool -> Click SQL Server Configuration Manager.
If you see that SQL Server/ SQL Server Browser State is 'stopped'.Right click on SQL Server/SQL Server Browser and click start.
In some cases above state can stop though TCP connection to port 1433 is assigned.
Open %windir%\System32 folder and find SQLServerManagerXX.msc
For example:
C:\Windows\System32\SQLServerManager14.msc
Go to protocols settings then enable TCP/IP port is 1433 by default
As the error says, you need to make sure that your sql server is running and listening on port 1433. If server is running then you need to check whether there is some firewall rule rejecting the connection on port 1433.
Here are the commands that can be useful to troubleshoot:
Use netstat -a to check whether sql server is listening on the desired port
As gerrytan mentioned in answer, you can try to do the telnet on the host and port
important:
after any changes or new settings you must restart SQLSERVER service. run services.msc on Windows
If you are using a named instance, the port you using likely is 1434, instead of 1433, so please check that out using telnet or netstat aforementioned too.
Open Firewall GUI,Open TCP 1433 rule,go to scope tab and add your IP address under Remote IP Address and the problem is solved.

Issues with connecting to sql server db via linux

Im trying to execute a jar file from linux.The jar contains java code for connecting to sql server. When I try to execute the jar, Im getting the below error
com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection to the host servername, port 1433 has failed. Error: "null. 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.".
The connect properties file seems to have a valid username/password. I have checked the sql server configuration manager and the TCP/IP protocol is enabled. Im able to execute the same jar file in winodws and the sql server connection is established fine without any issues. Please let me know the steps I have to take to get this issue resolved.
Any help on this is much appreciated. Thanks

Connecting to SQL server via Java program

I am trying to connect to SQL server using Java program.It runs on port 1433 by default. While trying to run the program, I get SocketTimedOutexception.
So, I used this query to check if it's listening to tcp port and it's output is null.
SELECT local_tcp_port
FROM sys.dm_exec_connections
WHERE session_id = ##SPID
GO
Results in NULL.
Since I am trying to connect on Local host, I use went to SQL server configuration manager
and ENABLED tcp/ip for MSSQLSERVER.(It was disabled by default) and then went to properties--> ip ddress and enabled IP12 since it was referring to 127.0.0.1 and restrted sql server.
Even now I am not able to connect to SQL server and it gives
java.net.SocketTimeOutException
Help me fix this.

java and access error when connecting to the database

Today I encountered the following problem. When connecting to a database, the following error.Appeal to the remote database by connecting through vpn.
Added necessary firewall ports and even tried to disconnect its(did not help)
Added record to $JAVA_HOME/jre/lib/security/java.policy
grant codeBase "file:/-" {
permission java.security.AllPermission;
};(did not help)
If trying to connect with same data through sql management studio, it works and there is access. from the development environment no connection
Example(string connection = jdbc:sqlserver://DEVMSSQL14.test.com:1433;SCHEMA=test;DATABASENAME=test, sql managment studia = DEVMSSQL14.test.com,1433)
on another computer it works without problems, the same version of the project
It remains only to "Permission denied: connect".Maybe somebody faced with similar
Error:
The TCP/IP connection to the host DEVMSSQL14.test.com, port 1433 has failed. Error: "Permission denied: 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.".
In the error message, you have shown like DEVMSSQL14.test.com, 1433 has failed.
I believe, it is correct. I dont think. this host name is correct. which says "TEST" as domain
Open the cmd, try to ping DEVMSSQL14.test.com
Success case :
If you are able to ping, then make sure the services are running for sql in services
Failure case:
Get the right host name to establish the connection
When i tried, i got this message, because i may be out of network.
Pinging DEVMSSQL14.test.com [208.64.121.161] with 32 bytes of data:
Request timed out.
Request timed out.
Try setting a System Property while running the code.
The catch is to introduce this line in code: System.setProperty("java.net.preferIPv4Stack", "true");
Or to pass it in VM option: -Djava.net.preferIPv4Stack=true

JDBC connection failed, error: TCP/IP connection to host failed

I want to connect Java class file with SQL server 2012. I have logged in with SQL server authentication, but I am receiving an error when connecting.
Error:
The TCP/IP connection to the host 127.0.0.1, 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.".
My code:
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); //1. Register your driver
//2. get the connection object
//Connection con = DriverManager.getConnection("jdbc:sqlserver://localhost;databaseName=aysha","sa","admin");
Connection con = DriverManager.getConnection("jdbc:sqlserver://127.0.0.1;databaseName=aysha","user=sa","password=admin");
//"jdbc:sqlserver://127.0.0.1:1433; Instance=SQL2008;" + "databaseName=MB;user=sa;password=123;";
//Connection con = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=aysha","sa" , "password");
//3. Prepare a statement
Statement stmt = con.createStatement();
//4. Write the query`
String sql = "Select * from employee";
//5. Execute the statement and
ResultSet rs = stmt.executeQuery(sql);
//6. Process the result set
while (rs.next())
{
System.out.println(rs.getInt(1));
}
Open SQL Server Configuration Manager, and then expand SQL Server 2012 Network Configuration.
Click Protocols for InstanceName, and then make sure TCP/IP is enabled in the right panel and double-click TCP/IP.
On the Protocol tab, notice the value of the Listen All item.
Click the IP Addresses tab:
If the value of Listen All is yes, the TCP/IP port number for this instance of SQL Server 2012 is the value of the TCP Dynamic Ports item under IPAll.
If the value of Listen All is no, the TCP/IP port number for this instance of SQL Server 2012 is the value of the TCP Dynamic Ports item for a specific IP address.
Make sure the TCP Port is 1433.
Click OK.
Easy Solution
Got to Start->All Programs-> Microsoft SQL Server 2012-> Configuration Tool -> Click SQL Server Configuration Manager ->Expand SQL Server Network Configuration-> Protocol ->Enable TCP/IP Right box
Double Click on TCP/IP and go to IP Adresses Tap and Put port 1433 under TCP port.
The error is self explanatory:
Check if your SQL server is actually up and running
Check SQL server hostname, username and password is correct
Check there's no firewall rule blocking TCP connection to port 1433
Check the host is actually reachable
A good check I often use is to use telnet, eg on a windows command prompt run:
telnet 127.0.0.1 1433
If you get a blank screen it indicates network connection established successfully, and it's not a network problem. If you get 'Could not open connection to the host' then this is network problem
Go to Start->All Programs-> Microsoft SQL Server 2012-> Configuration Tool -> Click SQL Server Configuration Manager.
If you see that SQL Server/ SQL Server Browser State is 'stopped'.Right click on SQL Server/SQL Server Browser and click start.
In some cases above state can stop though TCP connection to port 1433 is assigned.
Open %windir%\System32 folder and find SQLServerManagerXX.msc
For example:
C:\Windows\System32\SQLServerManager14.msc
Go to protocols settings then enable TCP/IP port is 1433 by default
As the error says, you need to make sure that your sql server is running and listening on port 1433. If server is running then you need to check whether there is some firewall rule rejecting the connection on port 1433.
Here are the commands that can be useful to troubleshoot:
Use netstat -a to check whether sql server is listening on the desired port
As gerrytan mentioned in answer, you can try to do the telnet on the host and port
important:
after any changes or new settings you must restart SQLSERVER service. run services.msc on Windows
If you are using a named instance, the port you using likely is 1434, instead of 1433, so please check that out using telnet or netstat aforementioned too.
Open Firewall GUI,Open TCP 1433 rule,go to scope tab and add your IP address under Remote IP Address and the problem is solved.

Categories

Resources