This is what I've written as part of my code:
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String host1 = "<URL/Connection path string>";
String uname1= "<User name>";
String pwd1 = "<Password>";
Connection con1 =DriverManager.getConnection(host1,uname1,pwd1);
Statement stmt1 = con1.createStatement();
ResultSet rs = stmt1.executeQuery("<Query>");
I've used the following jar files: jtds-1.3.0, sqljdbc-4.1.5605, sqljdbc4-4.0
when I ran the code, I got the following error:
Exception in thread "main" com.microsoft.sqlserver.jdbc.SQLServerException: The connection to the host <host-name>, named instance <instance-name> 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.
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(SQLServerException.java:190)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.getInstancePort(SQLServerConnection.java:3589)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.primaryPermissionCheck(SQLServerConnection.java:1225)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.login(SQLServerConnection.java:972)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerConnection.java:827)
at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.java:1012)
at java.sql.DriverManager.getConnection(DriverManager.java:571)
at java.sql.DriverManager.getConnection(DriverManager.java:215)
at first_doc.main(first_doc.java:28)
as far as I know, there is no firewall that's blocking traffic.
Also, do I need to add Port number as part of the Connection URL?
Edit: I use SQL server version 2008
I cannot find Protocols for SQLEXPRESS in the Configuration Manager
These are the steps that you need to do. If you already did this please check again.
Download latest MSSQL JDBC driver from here: http://msdn.microsoft.com/en-us/sqlserver/aa937724.aspx
Referenced the 2 JAR files in my project:
sqljdbc.jar and sqljdbc4.jar
(I'm not yet sure if both of the above are required or just one..)
Make sure the SQL Server Browser windows service is running
Open SQL Server Configuration Manager and go to Protocols for SQLEXPRESS under SQL Server Network Configuration. Right-click on TCP/IP and choose Properties.
Set Enabled = YES.
While you're there, click on IP Addresses tab and find the section IP All.
Set TCP Port to 1433.
Add sqljdbc_auth.dll to your PATH Environment Variable. In my case:
D:\Java\sqljdbc_4.0\enu\auth\x64
Copy the sqljdbc_auth.dll to your JDK directory. In my case:
C:\Program Files\Java\jdk1.7.0_04\bin
Related
In my following programme I am trying to connect MSSQL Server using jdbc in windows authentication. But getting following error
import java.io.*;
import java.sql.*;
import java.util.GregorianCalendar;
import java.util.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
class Cms_truncate
{
public static void main(String[] args)
{
Calendar cal = new GregorianCalendar();
//String name="cmscim";
//String filename = "D:\\programs\\Tomcat 6.0\\webapps\\timescape\\canteen_scheduller\\CMS_CSV\\cms_cim\\"+ name+"-"+cal.get(Calendar.YEAR) +"-" +(cal.get(Calendar.MONTH)+1) + "-"+cal.get(Calendar.DATE)+".csv";
Connection conn = null;
String url = "jdbc:sqlserver://localhost:1433;databasename=CMS_TIMES_MAIN;integratedSecurity=true";
String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
String userName = "";
String password = "";
Statement stmt;
try
{
Class.forName(driver);//.newInstance();
conn = DriverManager.getConnection(url,userName,password);
String query = "select * from cim where sapId=10025331";
stmt = conn.createStatement();
int flag = stmt.executeUpdate(query);
System.out.println("flag = "+flag);
conn.close();
System.out.println("");
} catch (Exception e) {
e.printStackTrace();
}
}
}
I am using SQL Server in windows authentication mode.
Do I need to do set up other things to connect MSSQL using jdbc in windows authentication.
ERROR:
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. 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.".
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(SQLServerException.java:190)
at com.microsoft.sqlserver.jdbc.SQLServerException.ConvertConnectExceptionToSQLServerException(SQLServerException.java:241)
at com.microsoft.sqlserver.jdbc.SocketFinder.findSocket(IOBuffer.java:2243)
at com.microsoft.sqlserver.jdbc.TDSChannel.open(IOBuffer.java:491)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(SQLServerConnection.java:1309)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.login(SQLServerConnection.java:991)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerConnection.java:827)
at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.java:1012)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at Cms_truncate.main(Cms_truncate.java:28)
Using windows authentication:
String url ="jdbc:sqlserver://PC01\inst01;databaseName=DB01;integratedSecurity=true";
Using SQL authentication:
String url ="jdbc:sqlserver://PC01\inst01;databaseName=DB01";
Try following these steps:
Add the integratedSecurity=true to JDBC URL like this:
Url: jdbc:sqlserver://<<Server>>:<<Port>>;databasename=<<DatabaseName>>;integratedsecurity=true
Make sure to add the sqljdbc driver 4 or above version (sqljdbc.jar) in your project build path:
java.sql.DatabaseMetaData metaData = connection.getMetaData();
System.out.println("Driver version:" + metaData.getDriverVersion());
Add the VM argument for your project:
Find the sqljdbc_auth.dll file from DB installed server (C:\Program Files\sqljdbc_4.0\enu\auth\x86), or download from this link.
Place the dll file in your project folder and specify the VM argument like this:
VM Argument: -Djava.library.path="<<DLL File path till folder>>"
NOTE: Check your java version 32/64 bit then add 32/64 bit version dll file accordingly.
You need to add sqljdbc_auth.dll in your C:/windows/System32 folder. You can download it from http://www.microsoft.com/en-us/download/details.aspx?displaylang=en&id=11774 .
From your exception trace, it looks like there is multiple possibility for this problem
1). You have to check that your port "1433" is blocked by firewall or not. If you find that it is blocked then you should have to write "Inbound Rule". It if found in control panel -> windows firewall -> Advance Setting (Option found at Left hand side) -> Inbound Rule.
2). In SQL Server configuration Manager, your TCP/IP protocol will find in disable mode. So, you should have to enable it.
You need to enable the SQL Server TCP/IP Protocol in Sql Server Configuration Manager app. You can see the protocol in SQL Server Network Configuration.
For the current MS SQL JDBC driver (6.4.0) tested under Windows 7 from within DataGrip:
as per documentation on authenticationScheme use fully qualified domain name as host e.g. server.your.domain not just server; the documentation also mentions the possibility to specify serverSpn=MSSQLSvc/fqdn:port#REALM, but I can not provide you with details on how to use this. When specifying a fqdn as host the spn is auto-generated.
set authenticationScheme=JavaKerberos
set integratedSecurity=true
use your unqualified user-name (and password) to log in
As this is using JavaKerberos I would appreciate feedback on whether or not this works from outside Windows. I believe that no .dll is needed, but as I used DataGrip to create the connection I am uncertain; I would also appreciate Feedback on this!
i was getting error as "This driver is not configured for integrated authentication" while authenticating windows users by following jdbc string
jdbc:sqlserver://host:1433;integratedSecurity=true;domain=myDomain
So the updated connection string to make it work is as below.
jdbc:sqlserver://host:1433;authenticationScheme=NTLM;integratedSecurity=true;domain=myDomain
note: username entered was without domain.
If you want to do windows authentication, use the latest MS-JDBC driver and follow the instructions here:
https://msdn.microsoft.com/en-us/library/gg558122(v=sql.110).aspx
You should copy your .dll => here "mssql-jdbc_auth-9.4.0.x64.dll" in that folder => sqljdbc_9.4\enu\auth\x64 to your Java JDK/bin.
Shortly: => sqljdbc_9.4\enu\auth\x64\mssql-jdbc_auth-9.4.0.x64.dll
to Program Files\Java\jdk-16\bin
If it doesnt work, you can open your SQL Server 2019 Configuration Manager -> SQL Server Network Configuration , all protocol's name should be Enabled. After that you should click on (TCP/IP) / IP Addresses, you will see that IPALL-> TCP PORT should be 1433.
After struggling a lot, I finally found a solution, here we go -
Download the file jtds-1.3.1.jar and ntlmauth.dll and save it in Program File -> Java -> JDK -> jre -> bin.
Then use the following code -
String pPSSDBDriverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
Class.forName(pPSSDBDriverName);
DriverManager.registerDriver(new com.microsoft.sqlserver.jdbc.SQLServerDriver());
conn = DriverManager.getConnection("jdbc:jtds:sqlserver://<ur_server:port>;UseNTLMv2=true;Domain=AD;Trusted_Connection=yes");
stmt = conn.createStatement();
String sql = " DELETE FROM <data> where <condition>;
stmt.executeUpdate(sql);
Nop, you have a connection error, please check your IP server adress or your firewall.
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. 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.".
ping yourhost (maybe the ping service was blocked, but try anyway).
telnet yourhost 1433 (maybe blocked).
Contact the sysadmin with the results.
The code below works with Sql Server 2008 but not with Sql Server 2012 and 2014 (v 11 or 12)
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String url="jdbc:sqlserver://myhost\\myinstance:1433;database=mydb";
connection = DriverManager.getConnection(url,"username", "pwd");
I'm using Java Runtime 1.7
I'm using the Microsoft Drivers as you can see in the Shell line below.
I've tried putting
sqljdbc_auth.dll (I've tried both 32 and 64 bit) in the jar drivers folder,
in JRE bin folder or even passed as java.library.path
I've checked in the configuration manager (on the Sql Server) that TCP/IP is enabled and responds at port 1433.
The firewall is off.
Here is the error I get:
c:\test\TestConn\Test2\bin>"C:\Program Files\Java\jre7\bin\java"
-classpath C:\libsqlsvr\sqljdbc_6.0\ita\sqljdbc4.1.jar;.
-Djava.library.path=C:\libsqlsvr\sqljdbc_6.0\ita\auth\x64 Test
com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection to the hot myhost , port 1433 has failed. Error: "connect timed out. 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.".
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(S
QLServerException.java:190)
at com.microsoft.sqlserver.jdbc.SQLServerException.ConvertConnectExcepti
onToSQLServerException(SQLServerException.java:241)
at com.microsoft.sqlserver.jdbc.SocketFinder.findSocket(IOBuffer.java:22
43)
at com.microsoft.sqlserver.jdbc.TDSChannel.open(IOBuffer.java:491)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(SQLSer
verConnection.java:1309)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.login(SQLServerConne
ction.java:991)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerCon
nection.java:827)
at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.
java:1012)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at Test.testsqlsvr(Test.java:135)
at Test.main(Test.java:24)
Do you know what could cause the problem?
Since you're using HOST:PORT identification of the SQL Server in JDBC, you should provide a valid IP or hostname in the HOST part, not the instance name (i.e. no "myhost\myinstance", but the actual network hostname / IP).
The JDBC connection goes through normal TCP, so the HOST:PORT is an ordinary network address, no Microsoft magic. HOSTNAME\INSTANCE uses a dispatcher service (SQL Server Browser) that resides on a known port (UDP 1434) and forwards the connection according to the \INSTANCE part, and is not combined with port.
I have a local MS SQL Server and I am trying to connect to it using JTDS in java. Here is the connection string:
Class.forName("net.sourceforge.jtds.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:jtds:sqlserver://localhost:1433/stock","sa","password");
And the server properties:
name: USER-PC\SQLEXPRESS
root directory: c:\Program Files\Microsoft SQL Server\MSSQL11.SQLEXPRESS\MSSQL
I am getting a Exception in thread "main" java.sql.SQLException: Network error IOException: Connection refused: connect error.
How can I remedy this?
Check the following:
You have enabled mixed mode security, i.e. so you can connect with username/password (rather than using Windows authentication)
The TCP/IP protocol is enabled. Open SQL Server Configuration Manager, then in SQL Server Network config select Protocols and Enable TCP/IP.
Try passing just one String to getConnection using this format:
DriverManager.getConnection("jdbc:jtds:sqlserver://localhost:1433/stock;instance=SQLEXPRESS;user=sa;password=password")
If you are using this format (naming the instance) then the SQL Server Browser service needs to be running (in Services). If this is greyed out then the Service is probably disabled, so enable it and then start it.
Port 1433 is the port used by the default instance. You're using SQLEXPRESS, which is a named instance. Your connection string should include something like:
sqlserver://localhost\SQLEXPRESS
SQL Server Browser service is disable by default. If you're developing .Net app, you do not need to start SQLBrowser, but if you're using JTDS in java, you will need it started.
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 can successfully ping the server but am not able to telnet into the server. I am also not able to telnet from my windows desktop.
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){}
Here is some documentation from jTDS regarding the issue, but I am still not able to resolve the issue.
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.
If you can't telnet on port 1433, you are blocked by a firewall somewhere in the middle between your machine and the server. That's not a java related problem.
May it be that when you say "it runs perfectly under eclipse but not AIX" you are taking about 2 different computers ? If so, the one with eclipse is not firewalled, the one where you deploy your app is blocked.
But again, nothing to do with java. It's a level 3 error (TCP layer) of TCP-IP model.
Regards,
Stéphane
Your SQL Server database probably doesn't have the TCP/IP protocol enabled, to enable it:
From the Microsoft SQL Server 2005 -> Configuration Tools, open the 'Microsoft SQL Server Configuration Manager'.
Expand ‘SQL Server 2005 Network Configuration’, and then click ‘Protocols for ’.
Right-click ‘TCP/IP’ and then click ‘Enable’. The icon for the protocol will change to show that the protocol is enabled.
For SQL Server 2008:
I am using sqljdbc4.jar in the Build path of eclipse project.
I am trying to connect to SQL Server 2008 in a java program
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String connectionUrl = "jdbc:sqlserver://localhost:1433;" +
"databaseName=sis;user=sample;password=sample;";
connection = DriverManager.getConnection(connectionUrl);
But I am getting the following error.
The TCP/IP connection to the host has failed. java.net.ConnectException: Connection refused: connect
I tried the following
Firewall off
Enabled TCP/IP in SQL Server configuration manager --> CLient protocols
SQL Server browser service is also running
Please let me know what other things should I try.
Thanks
Firewall off which way, this way??? open/enable port localhost:1433
run cmd and paste
netsh firewall set portopening protocol = TCP port = 1433
name = SQLPort mode = ENABLE scope = SUBNET profile = CURRENT
2/ change Sql Server authentication from
Windows authe... to Sql Server and Windows authentification (Mixed mode)
3) your version is SQL Server 2008 or SQL Server 2008 Express, if Express, then search on downloaded package (contains excelent help inc examples) how to connect Express on Localhost, there is difference localhost\sqlexpress
here and here and here
4) no idea how to connected, there are TCP/IP and Names Pipes too
EDIT:
5) in Management Studio you have to add new User (SQL autenthification) and assign to the decision Database (whatever or Model too) is there ??? aren't you