Failure while connecting to MS SQL server - java

Trying to connect o MS SQL server that runs on the same Win 10 machine with my project in Eclipse:
String url = "jdbc:sqlserver://GOLD\\SQLEXPRESS;databaseName=PrintLog;";
Connection conn = DriverManager.getConnection(url ,"user","passwd");
But got exception:
com.microsoft.sqlserver.jdbc.SQLServerException: The connection to the host GOLD, named instance sqlexpress failed. Error: "java.net.SocketTimeoutException: Receive timed out". Verify the server and instance names and check that no firewall is blocking UDP traffic to port 1434. For SQL Server 2005 or later, verify that the SQL Server Browser Service is running on the host.
Computer name is GOLD. I can connect to DB from C# without problem. Authentication is enabled both windows and SQL server. I'm expecting to get Windows authentication while connect
SQL server properties:
How to find the problem?
UPD
I got better result by adding port number to connect string:
String url = "jdbc:sqlserver://GOLD\\SQLEXPRESS:1433;databaseName=PrintLog;";
Does it means that default port number is different?

Related

Failed to connect to host MyServerName

Hi guys I am trying to connect NetBeans IDE with SqlServer, everything I did is correct, setting ports to 1433 in SQL Server Configurtion Manager and here is my code:
public Db(String login,String password, String dbname) throws SQLException{
try {
String url = "jdbc:sqlserver://MJRLGUE\\SWING;databaseName="+dbname+";integratedSecurity = false;";
Driver monDriver = new SQLServerDriver();
DriverManager.registerDriver(monDriver);
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
con = DriverManager.getConnection(url,login,password);
this.connecte = true;
System.out.println("Succes");
} catch (ClassNotFoundException e) {
System.out.println("echec driver "+e.toString());
this.connecte = false;
}
}
and the error message:
Try again com.microsoft.sqlserver.jdbc.SQLServerException: Failed to connect to host MJRLGUE, named instance swing. Error: "java.net.SocketTimeoutException: Receive timed out". Verify the server name and the instance name, and make sure that no firewalls block UDP traffic to port 1434. For SQL Server 2005 or later, verify that the SQL Server Browser service is running Execution on the host.
Exception in thread "main" java.lang.NullPointerException
at Model.Test.remplirDirecteur(Test.java:79)
at Main.main(Main.java:17)
C:\Users\Ghassane\AppData\Local\NetBeans\Cache\8.1\executor-snippets\run.xml:53: Java returned: 1
BUILD FAILED (total time: 29 seconds)
my instance name in SqlServer: MJRLGUE\SWING
code to connect the database: db= new Db("sa","ensak","SuiviMarche");
Check that you can ping the sql server from the machine running the code.
If you can you may need to to configure the sql server to allow remote connections, as you have said you already checked the firewall and I assumed the instance is running on the standard port of 1433
Also, check that you can you use SMSS to connect to the sql server from the machine running the code.
Edit:
Looking at your error message you are not using the standard port of 1433 as the message says you are trying to connect to 1434.
make sure that no firewalls block UDP traffic to port 1434
try using jdbc:sqlserver://localhost:1433 as your host.
check your url and make sure server is running properly and port number and service free from firewall.

Connecting to SQL Server results in error "The driver received an unexpected pre-login response"

I am unable to connect to sql server, getting following error.
exception com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection
to the host SSALES, port 1433 has failed. Error: "The driver received an unexpe
cted pre-login response. Verify the connection properties and check that an inst
ance of SQL Server is running on the host and accepting TCP/IP connections at th
e port. This driver can be used only with SQL Server 2000 or later.".
my local Environment:-
java 5,
sql server enterprise manager version 8,
RunApp.java
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
conn = java.sql.DriverManager.getConnection("jdbc:sqlserver://abc;d‌​atabaseName={db};use‌​r=name;password=pass‌​worf;");
Statement stmt=conn.createStatement();
ResultSet rs=stmt.executeQuery("SELECT * FROM testdb.testtabel where active = 'a'");
running through cmd:
set path="C:\Program Files\Java\jdk5\bin";
javac -classpath "D:\Web Development\ABC\lib\mail-1.4.3.jar;D:\Web Development\ABC\lib\json-simple.jar;D:\Web Development\ABC\lib\sqljdbc.jar;" com\test\RunApp.java
java -classpath "D:\Web Development\ABC\lib\mail-1.4.3.jar;D:\Web Development\ABC\lib\json-simple.jar;D:\Web Development\ABC\lib\sqljdbc.jar;" com.test.RunApp
Your URL is wrong (see Building the Connection URL), it should be (note the use of an escaped backslash instead of forward slash):
"jdbc:sqlserver://localhost\\SQLEXPRESS;databaseName={db};user=MyUserName;password=MyPassword;"
Also make sure that {db} is replaced with the actual name of the database.
You are trying to connecting by instance name, and some googling on the error message suggests this error can occur if the instance name is wrong or if the SQL Server Browser service is not running. So you need to have the SQL Server Browser service enabled (it is disabled by default).
Otherwise you will need to remove the instance name from the URL, and instead specify the correct port number in the JDBC url (usually 1433, but it can depend on the configuration: double check).

The TCP/IP connection to the host localhost, port 1433 has failed

I'm getting this 2 errors repeatedly when I'm trying to connect eclipse with sql server through jdbc. Could anyone help me with this or explain why am I getting this?
1. The TCP/IP connection to the host localhost, port 1433 has failed.
2. The driver could not establish a secure connection to SQL Server by using Secure Sockets Layer (SSL) encryption.
Thank you.
String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
Class.forName(driver).newInstance();
String connString ="jdbc:sqlserver://localhost:1433/databaseName=Engg_Street;instance=SQLSERVER;encrypt=true; trustServerCertificate=true";
String username = "Vijayalakshmi";
String password = "";
conn = DriverManager.getConnection(connString,username,password);
You need to Go to Start > Microsoft SQL Server > Configuration Tools > SQL Server Configuration Manager
When it opens Go to
SQL Server Configuration Manager > SQL Server Network Configuration > Protocols for SQLExpress
Where you'll find the Protocol TCP/IP, if disabled then Enable it Click on TCP/IP, You'll find its properties.
In this properties Remove All the TCP Dynamic Ports and Add value of 1433 to all TCP Port
and restart your SQL Server Services > SQL Server
And Its Done...
If you are running SQLSERVER EXPRESS:
Use "SQL Server Configuration Manager" or write "mmc.exe" on Win+R and search "SQL Server Network Configuration", in the "Protocols for SQLEXPRESS" enable the option TCP/IP
I hope than this works for somebody!!
For this kind of issue - you can resolve by checking whether all sql server related services are running or not.
TO check Start -> Run -> services.msc
check services related to sql as
SQL Server (MSSQLSERVER)
SQL Server Agent (MSSQLSERVER)
SQL Server Analysis Services (MSSQLSERVER)
SQL Server Browser
SQL Server Integration Services 10.0
SQL Server Reporting Services (MSSQLSERVER)
SQL Server VSS Writer
Start all the above services. and set to start up type to Automatic.
SQL Active Directory Helper Service is not required to keep running.
It can used to deal with error SQL server connection error 2
and
with
he TCP/IP connection to the host localhost, port 1433 has failed. Error: "Connection refused:
It can used to deal with error SQL server connection error 2 and with he TCP/IP connection to the host localhost, port 1433 has failed. Error: "Connection refused: https://www.youtube.com/watch?v=R8_jjkeulEw
or please check if the table name is duplicated, i changed the table name --> problem solved

Error:The TCP/IP connection to the host has failed. java.net.ConnectException: Connection refused: connect

I am using
Windows 7
Netbeans IDE 7.1.2
SQL Server Management Studio Express 2005
JDK1.6
I am getting the below error while connecting to the database:
com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection to the host has failed. java.net.ConnectException: Connection refused: connect
My connection string is:
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection connection = DriverManager.getConnection(
"jdbc:sqlserver://127.0.0.1:1433;databaseName=dbcm;" +
"user=sa;password=sa");
I've checked below things:
In SQL Server Configuration Manager, Protocols for SQLEXPRESS : TCP/IP Enabled
In fact I made enable remaining 3 too.
SQL Server(SQLExpress) and SQL Browser Service both are running.
I followed the steps below to successfully connect to an SQLExpress instance using JDBC:
Configure TCP/IP communication with SQL Express
Open SQL Server Configuration Manager.
Go to SQL Server Network Configuration -> Protocols for SQLEXPRESS
Set the status of TCP/IP protocol to "Enabled" (if it is already not).
Open Properties window for TCP/IP, go to IP Addresses section.
Go to the bottom of this property page and set the TCP Port under IPAll to 1433.
Connect to the SQLExpress instance using Microsoft's JDBC driver for SQL Server
JDBC URL: jdbc:sqlserver://localhost;instance=SQLEXPRESS;databaseName=<your DB>;user=<your User>;password=<your Passwd>

Connecting to external database using java

I have a java program that connects to a MS SQL database. The program works perfectly when running through eclipse however I get an error when I run it through AIX:
java.sql.SQLException: Network error IOException: A remote host refused an attempted connect operation.
I am using jtds to connect:
String connectionString = "jdbc:jtds:sqlserver://"+dropez_ip_address+"/"+dropez_db_name;
ResultSet rs = null;
Statement stmt = null;
try{
Class.forName("net.sourceforge.jtds.jdbc.Driver");
Connection conn = DriverManager.getConnection(connectionString, dropez_db_username, dropez_db_password);
stmt = conn.createStatement();
}catch(Exception e){}
From jTDS documentation:
Why do I get java.sql.SQLException: "Network error IOException: Connection refused: connect" when trying to get a connection?
The "Connection refused" exception is thrown by jTDS when it is unable to connect to the server. There may be a number of reasons why this could happen:
The server name is misspelled or the port number is incorrect.
SQL Server is not configured to use TCP/IP. Either enable TCP/IP from SQL Server's Network Utility app or have jTDS connect via named pipes (see the URL format for information on how to do this).
There is a firewall blocking port 1433 on the server.
To check whether TCP/IP is enabled and the port is not blocked you can use "telnet 1433". Until telnet doesn't connect, jTDS won't either. If you can't figure out why, ask your network administrator for help.
My bet is your firewall does not allow the AIX host to connect.

Categories