I am trying to connect to a MS SQL Server database using Java but I keep running into the same error.
I am using MS SQL Server for developers and am maniging it through SSMS. When I installed it I set is so it would use my windows credentials as password.
The JDBC driver is added to my project as a Maven dependency.
From what I understand reading the Microsoft doc's I should be able to use the following connection string:
String connectionUrl ="jdbc:sqlserver://localhost;integratedSecurity=true";
However this does not seem to work for me as it returns SQLState 08001. After reading up on this state I understand that it has something to do with failing to make the connection.
I also tried to form my connection string using my windows login name, which results in the same:
String connectionUrl = "jdbc:sqlserver:localhost:1433;databaseName=DBNAME;user=John Doe;password=0123456789";
I also tried to create a login using T-SQL to use as credentials using the same string as the previous (only changing the username and password):
CREATE ROLE [USER]
GRANT SELECT, INSERT ON SCHEMA :: [dbo] TO USERNAME
CREATE LOGIN USERNAME_LOGIN WITH PASSWORD = '1234567890'
CREATE USER USER_USERNAME FROM LOGIN USERNAME_LOGIN
ALTER ROLE [USER] ADD MEMBER USER_USERNAME
I could really use some help to understand what I am doing wrong and what I can do to make it work. If you require me to check anything please let me know how as I am still pretty new to this.
Edit
As requested here is the info coming from getMessage():
No suitable driver found for
jdbc:sqlserver:localhost:1433;databaseName=.....
I am guessing this would also be the moment to mention the Maven dependency I am importing:
<!-- https://mvnrepository.com/artifact/com.microsoft.sqlserver/mssql-jdbc -->
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
<version>7.3.0.jre8-preview</version>
</dependency>
I think the question now becomes, do I have the wrong driver as a dependency for MS SQL Server 2017?
I turned out I did not had TCP/IP connections enabled for MS SQL Server.
To enable this in MS SQL Server 2017:
Start SQLServerManager14.msc from the windows application menu.
Go to SQL Server Network Configuration.
Go to Protocols for YOURINSTANCE.
Set TCP/IP to enabled.
Related
The best documentation I have been able to find about how to create a JDBC URL for an SQL Server instance within Cloud SQL is located here: https://github.com/GoogleCloudPlatform/cloud-sql-jdbc-socket-factory
The only problem is, there is no example or instructions for creating the JDBC URL for SQL Server.
I have tried some obvious possible solutions, none of which have worked - for example:
jdbc:sqlserver:///<DATABASE_NAME>;socketFactory=com.google.cloud.sql.sqlserver.SocketFactory;cloudSqlInstance=<INSTANCE_CONNECTION_NAME>;user=<USER_NAME>;password=<PASSWORD>
I have a mysql server in the same project that I am able to connect to with no issue (after updating the jdbc Driver and JDBC URL). SQL Admin API is enabled and GOOGLE_APPLICATOIN_CREDENTIALS is pointing to the correct service account.
Relevant pom.xml
<dependency>
<groupId>com.google.cloud.sql</groupId>
<artifactId>cloud-sql-connector-jdbc-sqlserver</artifactId>
<version>1.2.0</version>
</dependency>
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
<version>8.4.0.jre8</version>
</dependency>
If i'm reading the git repo correctly it actually looks like JDBC support for SQL Server was just added in the 1.2.0 release - has anyone been able to get this to work?
https://github.com/GoogleCloudPlatform/cloud-sql-jdbc-socket-factory/pull/263
Error I am receiving
ERROR: The TCP/IP connection to the host /<DATABASE_NAME>, port 1433 has failed. Error: "/<DATABASE_NAME>. 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."
Thanks!
The root cause was that the version of SQLServerDriver I was using does not appear to compatible with com.google.cloud.sql.sqlserver.SocketFactory. I was able to switch to the net.sourceforge.jtds.jdbc.Driver which worked as expected. More info in this thread: https://github.com/GoogleCloudPlatform/cloud-sql-jdbc-socket-factory/issues/373
I have an application running on a server and I'm using H2 database for that,
This my database URL:
jdbc:h2:./cafeDB;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE;AUTO_SERVER=TRUE;AUTO_SERVER_PORT=7080
And I want to connect to this database using Intellj idea's database tools, this is my configuration:
But when I try to connect, It keeps asking me the DB's username and password, and even when I put the correct information and submit it says:
The specified database user/password combination is rejected: [28000][28000] Wrong user name or password [28000-200]
Am I doing something wrong?
I am trying to connect to my database using JDBC driver. I have to use my another windows domain credential to connect and getting login failed error. Thought of testing it with my actual windows credential and it worked. I have used integratedSecurity = true.
String connectionUrl = "jdbc:sqlserver://server;databaseName=DB;integratedSecurity=true";
My desired connection string to use my another windows domain credential looks like the below:
String connectionUrl = "jdbc:sqlserver://server;databaseName=DB;user=domain\\user;password=test!123;"
I did the following things and in need of help:
Placed the sqljdbc_auth inside jdk/bin and jre/bin
SQL Server has been kept under the mixed auth mode (both Windows and SQL Auth).
Using my another domain ID, I tried to execute a query in MS SQL Studio and it worked.
Using the Microsoft JDBC driver, you can:
Connect with SQL Auth, using user and password
Connect with Window Auth, using integratedSecurity=true, which will use the Windows user that is running the Java program
Connect with Kerberos Auth, but that is complex to setup: Using Kerberos Integrated Authentication to Connect to SQL Server
If you want to do a simple NTLM connect using domain, user and password, then you need to use the jTDS JDBC driver.
I want to be able to connect to a SQL Server using jdbc and windows authentication.
I saw some answers on the internet saying i should add the following property to the connection string:
integratedSecurity=true;
And also add
sqljdbc_auth.dll
To the java path.
But this, as far as i understand applies only when i'm connecting from a Windows machine.
When i try this on a Linux machine i get:
java.sql.SQLException: This driver is not configured for integrated authentication
My question is how do I do it from a Linux machine.
Thanks
Well, eventually I answer my own question:
This is not possible to use Windows authentication from a linux machine using the Microsoft JDBC driver.
This is possible using the jTDS JDBC driver using the following connection string:
jdbc:jtds:sqlserver://host:port;databaseName=dbname;domain=domainName;useNTLMv2=true;
Thank you all for all the comments
TL;DR
It is not possible to use native Windows Authentication for JDBC connections to MSSQL from a JVM running on Linux.
This MSDN article explains the authentiation methods with JDBC on Linux, potential errors, and available options:
https://blogs.msdn.microsoft.com/psssql/2015/01/09/jdbc-this-driver-is-not-configured-for-integrated-authentication/
...in the JDBC 4.0 driver, you can use the authenticationScheme
connection property to indicate how you want to use Kerberos to
connect to SQL. There are two settings here.
NativeAuthentication (default) – This uses the sqljdbc_auth.dll and is specific to the Windows platform. This was the only option
prior to the JDBC 4.0 driver.
JavaKerberos – Makes use of the Java API’s to invoke kerberos and does not rely on the Windows Platform. This is java specific and not
bound to the underlying operating system, so this can be used on both
Windows and Linux platforms.
...
The following document outlines how to use Kerberos with the JDBC
Driver and walks through what is needed to get JavaKerberos working
properly.
Using Kerberos Integrated Authentication to Connect to SQL Server
http://msdn.microsoft.com/en-us/library/gg558122%28v=sql.110%29.aspx
For those who are using DBeaver the way to connect to the SQL Server Database is:
In order to connect to the SQL Server from Linux Debian using DBeaver
1.- Select SQL Server jTDS driver
2.- Enter the connection information
3.- Go to Driver Properties tab and add the domain, user, password
Just as a note, in some post I found that they needed to change the property USENTLMV2 to TRUE but it worked for me either by putting the USERTLNMV2 in true or false.
A problem that I found was that when I was trying to connect to the database using my user and password the next error was thrown:
Login failed. The login is from an untrusted domain and cannot be used with Windows authentication.
This error was thrown because of my user was about to expire. I tried with another AD user and it could connect.
I know this is kind of an older topic but in case Google sends people here:
There are two main JDBC drivers for SQL Server. One is from Microsoft and the other from jTDS. jTDS can, amazingly, connect using Windows auth (NTLM) from other platforms, including Linux, as described here: http://jtds.sourceforge.net/faq.html#windowsAuth. It can, of course, also use SQL-authenticated logins. SQL-authenticated logins are no harder to use from any OS than any other, so don't forget about those an option.
The version provided by Microsoft is the one from which #mjn provided a quote from the documentation. It is able to connect using Windows authentication by specifying integratedSecurity=true, authenticationScheme=javaKerberos, and authentication=NotSpecified.
It is tricky to get this working even if you don't go out of your way to find more confusion, so always keep in mind which driver you are using - and tell us in these posts so that you can get more specific help.
This JDBC URL is validated to work with latest Microsoft SQL Server JDBC driver:
jdbc:sqlserver://[server]:[port];database=[db\;trustServerCertificate=true;integratedSecurity=true;user=[user without domain];password=[pw];authenticationScheme=NTLM;domain=[domain];authentication=NotSpecified
Example:
jdbc:sqlserver://mysql.myorg.com:1433;database=mydb;trustServerCertificate=true;integratedSecurity=true;user=myuser;password=mypwd;authenticationScheme=NTLM;domain=ad.myorg.com;authentication=NotSpecified
I was able to connect to a SQL Server 2016 Data Mart and JDBC connection Microsoft JDBC Driver using Windows Authentication using the following script on a Ubuntu Linux Docker Image running on Windows 10.
# initializes spark session
from pyspark.sql import SparkSession
spark = SparkSession\
.builder\
.master('local[*]')\
.appName('FDM')\
.config("spark.driver.extraClassPath","pyspark_jars/*")\
.config('spark.executor.memory', '4g')\
.config('spark.driver.memory', '16g')\
.config('spark.executor.cores', '4')\
.getOrCreate()
jdbc_url = '''jdbc:sqlserver://SERVER;databaseName=DBNAME;trustServerCertificate=true;integratedSecurity=true;user=USERID;password=PASSWORD;authenticationScheme=NTLM;domain=US;authentication=NotSpecified'''
spark_df = spark.read\
.format("jdbc")\
.option("url", jdbc_url)\
.option("driver","com.microsoft.sqlserver.jdbc.SQLServerDriver")\
.option("query", 'select top(1000) * from SCHEMA.TABLE')\
.option("fetchsize", 100000)\
.load()
spark_df.write.csv('TEST.csv', mode = "overwrite", header=True)
I'm trying to connect to a MS SQL Server, using a linux client. I've tried both SQuirreL and DBeaver, but having no luck in either case. I've tried a few different drivers.
The connection string I am given looks something like this:
jdbc:oracle:thin:#ldap://<server>:<port>/<database>,cn=OracleContext,dc=<specific_dc>,dc=<specifc_dc>,dc=<specific_dc>
This seems to be an invalid URL, and I've tried various combinations of things (like using jdbc:sqlserver:// ... protocol, etc. It doesn't seem to like the #ldap in the connection string, and I've replaced the commas with semicolons. But, I'm new to connecting to SQL Server, much less using LDAP.
Any thoughts/help?
Thanks!
For those who are using DBeaver the way to connect to the SQL Server Database with an AD user is using the jTDS driver.
I am using DBeaver 6.0 in Debian 9. The user is an AD user.
In order to connect to the SQL Server from Linux Debian using DBeaver
1.- Select SQL Server jTDS driver
2.- Enter the connection information
3.- Go to Driver Properties tab and add the domain, user, password
Just as a note, in some post I found that they needed to change the property USENTLMV2 to TRUE but it worked for me either by putting the USERTLNMV2 in true or false.
A problem that I found was that when I was trying to connect to the database using my user and password the next error was thrown:
This error was thrown because of my user was about to expire. I tried with another AD user and it could connect.
cts:
datasource:
url: jdbc:jtds:sqlserver://dbserver:1433;database=DB;domain=yourdomain.com;useNTLMv2=true
driver-class-name: net.sourceforge.jtds.jdbc.Driver
username: username
password: password
hikari:
connection-test-query: SELECT 1
maximumPoolSize: 2
minimumIdle: 1