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

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?

Related

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

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

Oracle DB connection issue

When I try to connect java application to oracle db using jdbc thin client, my system name was sent to db server without host name (eg. 102XXXXX) But when I try with oracle client(PL/SQL developer), my system name was sent to db server with domain name appended (eg. North_America/102XXXXX). And in oracle server, my machine is whitelisted with domain name (eg. North_America/102XXXXX). So I got account locked exception when try to run the java application but have successful connection from PL/SQL developer. I am not sure how to solve this issue from my side. I need both connection. Anyone has any idea?
Note that you can also customize what the Oracle JDBC thin driver sends for the system name during authentication by setting this property "v$session.machine".

Error occured while connecting to cloud sql from android studio

I am trying to connect to cloud sql from app engine using cloud back-end java servlet in android studio. But I got error in getting connection from DriverManager.
Connection conn = DriverManager.getConnection("jdbc:google:mysql://<id:<sql-id>/database",
"myuser","password");
and driver is
Class.forName("com.mysql.jdbc.GoogleDriver");
Please assist me if fixing this error...
The com.mysql.jdbc.GoogleDriver driver and jdbc:google:mysql connection string only work from Google App Engine. When connecting from your development workstation you should use the plain MySQL java connector (ie com.mysql.jdbc.Driver and jdbc:mysql). You will need to authorize you workstation's IP address and add an IP address to your Cloud SQL instance to connect to.
For more information follow the guide at https://cloud.google.com/sql/docs/external.
Actually i got it,problem is with some_user database permission for using database in appengine(Localhost for cloud sql database).
FIX:
Type code in cloud sql database and execute
grant all privileges on databasename.* to 'user'#'localhost' identified by 'password';
This should help.........

Connecting to AS400/iSeries using Oracle WebLogic Server

I have encountered a problem when setting up a Data Source in Oracle WebLogic Server that is an AS400. When setting it up and testing the connection I get the error message:
"The application requester cannot establish the connection"
When checking on IBMs website I found the following:
What does "The application requester cannot establish the connection"
mean?
The Toolbox JDBC driver uses the system name, user ID, and password
provided in the call to DriverManager.getConnection() to establish a
connection to the IBM i database. If any of these are not provided,
the Toolbox JDBC driver will display a sign-on prompt. If the Toolbox
JDBC driver can not sign on to the system for any reason, it will
throw an SQLException with the message "The application requester
cannot establish the connection". Here is a list of potential causes
for this exception: The system name is incorrect. One way to verify
this is to use ping with the specified system name. If ping fails,
then there is a TCP/IP configuration problem between your client and
the system. The user ID or password is incorrect. The IBM i database
host server is not started. You can start this by running STRHOSTSVR
*DATABASE on the IBM i command line. You can verify if the IBM i database host server is running
using the utilities.JPing application.
(http://www-03.ibm.com/systems/power/software/i/toolbox/faq/jdbc.html#faqC6)
When I ping the server, it fails - which also makes sense. However, when I use just a plain java file that opens a connection, fires an sql and closes the connection like
connection1 = DriverManager.getConnection("server", "user", "password")
Statement statement1 = connection1.createStatement();
ResultSet result1 = statement1.executeQuery("SQLquery")
i get a perfect connection and correct result. How is this possible? How can I connect via WebLogic and ODI?
PS: I already installed the AS400 driver according to this manual

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