cancel
Showing results for 
Search instead for 
Did you mean: 
Data Engineering
cancel
Showing results for 
Search instead for 
Did you mean: 

An example how to connect to SQL Server data using windows authentication

gillzer84
New Contributor

We use SQL Server to store data. I would like to connect to SQL to pull manipulate and sometimes push data back. I've seen some examples online of connecting but I cannot successfully re-create.

3 REPLIES 3

Junee
New Contributor III

You can use jTDS library from maven, add this to your cluster. Once installed, you can write the below code to connect to your Database.

Code in Scala will be:

import java.util.Properties
 
val driverClass = "net.sourceforge.jtds.jdbc.Driver"
val serverAdd = ""
val databaseName = ""
val domain = "" // use domain of your active directory
val database_port = ""
 
val jdbcUsername = "" 
val jdbcPassword = ""
 
val conStr = "jdbc:jtds:sqlserver://"+ serverAdd + ":" + database_port +"/" + databaseName + ";useNTLMv2=true;domain=" + domain  + ";"
 
val connectionProperties = new Properties()
connectionProperties.put("user",s"${jdbcUsername}")
connectionProperties.put("password",s"${jdbcPassword}")
connectionProperties.setProperty("Driver", driverClass)
 
 
val dataFrame = spark.read.jdbc(url = conStr,  
                                table = "(select * from table_name) a",
                                properties = connectionProperties)

Would someone provide example with IntegratedSecurity instead of user name and passworld?

*password*

Welcome to Databricks Community: Lets learn, network and celebrate together

Join our fast-growing data practitioner and expert community of 80K+ members, ready to discover, help and collaborate together while making meaningful connections. 

Click here to register and join today! 

Engage in exciting technical discussions, join a group with your peers and meet our Featured Members.