JDBC can't connect to SQL Server / Azure SQL Database - java

I use
JTDS version: jtds-1.3.1
MS SQL JDBC version: mssql-jdbc-8.4.0.jre8 even mssql-jdbc-9.2.0.jre8
Java 8 Update 251
For simplicity, the code will the same as:
Android studio can't connect to database in Azure sql server
I have a problem to connect to SQL Server or Azure SQL.
I want to connect with this syntax:
val connectionString = "jdbc:jtds:sqlserver://192.168.xx.xx:1433;databaseName=myData;user=x1;password=p1;encrypt=true;trustServerCertificate=true;"
With this JTDS connection string, I can connect to my SQL Server DB on localhost, but with MS-JDBC, I failed to connect.
val connectionString = "jdbc:sqlserver://192.168.xx.xx:1433;databaseName=myData;user=x1;password=p1;encrypt=true;trustServerCertificate=true;"
I get the following errors:
com.microsoft.sqlserver.jdbc.SQLServerException: The Driver could not
establish a secured connection to SQL Server by using Secure Sockets
Layer (SSL) encryption. Error: "Socket is closed". ClientConnectionId:
xxxxxxxxxxxxxxxxx
An interesting thing happens when I connect with JTDS version to SQL Azure with the command:
jdbc:jtds:sqlserver://xxxxx.database.windows.net:1433;database=xxxxx;user=username;password=password;encrypt=true;trustServerCertificate=false;hostNameInCertificate=*.database.windows.net;loginTimeout=30;
I failed to connect to SQL Azure with the following errors
java.sql.SQLException: Reason: Login Failed due to client TLS Version
being less than minimal TLS Version allowed by the server
I already tried:
SSLProtocol=TLSv1.2 (TLS, TLSv1.0, TLSv1.1) => none work
ssl=require
Is it a bug with JTDS, a bug with JDBC, o isr my connection string invalid? I can connect to SQL Azure with my dotnet apps (C#).

I just fixed this in an Azure Managed Instance using the PowerShell script below:
Set-AzSqlInstance -Name 'InstanceName' -ResourceGroupName 'ResourceGroupName' -MinimalTlsVersion "None" -force

Related

In Jmeter Unable to make JDBC Connection to SSMS getting Cannot create PoolableConnectionFactory

I am trying to make a JDBC connection to SQL server but I am getting this error
Cannot create PoolableConnectionFactory (The driver could not establish a secure connection to SQL Server by using Secure Sockets Layer (SSL) encryption. Error: "SQL Server did not return a response. The connection has been closed. ClientConnectionId:ed21ad88-5a8b-47fc-837e-e93b3caf2fca".)
using SSMS (SQL server 18) using java version "18.0.1.1"
I checked in related content in stackoverflow but still no use
SQL Server JDBC Error on Java 8: The driver could not establish a secure connection to SQL Server by using Secure Sockets Layer (SSL) encryption
enter image description here
enter image description here
It looks like there is a problem with SSL handshake between the JDBC driver and MSSQL database, you can enable SSL debugging by adding the next line to system.properties file (lives in "bin" folder of your JMeter installation)
javax.net.debug=all
As a workaround you can add encrypt=false parameter to your JDBC URL so the connection would be plaintext, the error should go away, however if you're testing the database your setup should be as close to the production as possible so I would rather recommend checking out what's going on with the secure communications.

Can't connect from Java to ms-sql server using jdk 1.8.0_231

I am trying to connect to ms-sql database from java using jdk1.8.0_231 and fail with this exception:
The driver could not establish a secure connection to SQL Server by using Secure Sockets Layer (SSL) encryption. Error: "SQL Server did not return a response. The connection has been closed.
Does anyone know how to overcome this?
When I use 1.8.0_111, the connection is successful but I must use the 231 version since in my project I need to connect to other database with encrypted password that is not supported by the 111 version.
Any suggestions?

Azure SQL database connection issue from an Azure VM by Java JDBC

We have a java project in an Azure virtual machine (VM), and need connect to Azure SQL db by JDBC connection, so we use the JDBC connection string provided by Azure SQL db as follows:
"jdbc:sqlserver://ZZZdbserver.database.windows.net:1433;database=ZZZ;user=*****;password=*****;encrypt=true;trustServerCertificate=false;hostNameInCertificate=*.database.windows.net;loginTimeout=30;"
but we got an exception:
"java.security.cert.CertificateException: Failed to validate the server name in a certificate during Secure Sockets Layer (SSL) initialization.
The server name is *.database.windows.net, the name in certificate is cr2.eastus1-a.control.database.windows.net."
then we updated the JDBC connection string to:
"jdbc:sqlserver://ZZZdbserver.database.windows.net:1433;database=ZZZ;user=*****;password=*****;encrypt=true;trustServerCertificate=false;hostNameInCertificate=cr2.eastus1-a.control.database.windows.net;loginTimeout=30;"
but we got another exception:
org.apache.commons.dbcp.SQLNestedException: Cannot create PoolableConnectionFactory (Cannot open server "ZZZ1" requested by the login. The login failed.)
where "ZZZ1" is our username for the Azure VM.
Any idea to solve this issue?
By default, Azure VM has not blocked the outbound connection to the Internet. You could first verify the network connectivity from your VM to the Azure SQL database via running the command telnet ZZZdbserver.database.windows.net 1433 in CMD or Test-NetConnection -computer ZZZdbserver.database.windows.net -port 1433 in Powershell. If it fails, you may check if there is any firewall blocking this database connection or port or your application is listening on that port or your SQL database is online.
Additionally, if the network connectivity has succeeded, you could verify if the username or password is correct when you are using to connect to your database. Make sure there is not any typo. Also, the username should have enough privileges to access this database. You can try to access this database via Server admin login in the properties of the SQL database. Moreover, you could refer to this sample code to use Java to connect to access the Azure SQL database.
When using the Microsoft JDBC Driver for SQL Server to connect to an Azure SQL Database. You should note this:
Appending the server name to the userId in the connection string
Prior to the 4.0 version of the Microsoft JDBC Driver for SQL Server,
when connecting to an Azure SQL Database, you were required to append
the server name to the UserId in the connection string. For example,
user#servername. Beginning in version 4.0 of the Microsoft JDBC Driver
for SQL Server, it's no longer necessary to append #servername to the
UserId in the connection string.
Using encryption requires setting hostNameInCertificate
Prior to the 7.2 version of the Microsoft JDBC Driver for SQL Server,
when connecting to an Azure SQL Database, you should specify
hostNameInCertificate if you specify encrypt=true (If the server name
in the connection string is shortName.domainName, set the
hostNameInCertificate property to *.domainName.). This property is
optional as of version 7.2 of the driver.
Hope this helps.
If I need set encrypt=true, and hostNameInCertificate=cr2.eastus1-a.control.database.windows.net. Where do I need get the certificate for cr2.eastus1-a.control.database.windows.net from Azure SQL DB's service?

MyEclipse Professional on connecting with ms sql server is giving ssl error through db browser

I am using MyEclipse professional and trying to connect with the MS SQL SERVER 2016, i am using the jar file sqljdbc4-4.0 and driver
“class name= com.microsoft.sqlserver.jdbc.SQLServerDriver”
My connection URL is jdbc:sqlserver://hamdan:1433;databaseName=DWProject
where hamdan is the SQL server name, after giving the credentials i got the error of error while performing database login with the mysqlserver driver: the driver couldn't establish a secure connection to SQL server by using secure socket layer(SSL) encryption. Error java.lang.runtimeException, could not generate DH keypair.
However i have also set Force Encryption to "NO" in the protocol for sql server's properties in sql server configuration.
when i am changing the jar with the jar present on this link https://www.microsoft.com/en-us/download/details.aspx?id=11774 which is jdbc 6.0 and using jar for jre 8 which is sqljdbc42, i get error of driver class not found while using new database connection driver.
Any help will be highly appreciated, i am on this from more than one day,
I am new to eclipse.
This is the problem in windows 10, it encrypts the link between eclipse and SQL Server, so you should use latest myeclipse or try in different OS

connecting to local MS SQL Server

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.

Categories

Resources