clark
clark

Reputation: 65

Not able connect database using JDBC

I am trying to connect with my SQL server in databricks notebook with JDBC using spark sql.

val jdbcHostname = "database.windows.net"
val jdbcPort = 1433
val jdbcDatabase = "Database1"

// Create the JDBC URL without passing in the user and password parameters.
val jdbcUrl = s"jdbc:sqlserver://${jdbcHostname}:${jdbcPort};database=${jdbcDatabase}"

// Create a Properties() object to hold the parameters.
import java.util.Properties
val connectionProperties = new Properties()

connectionProperties.put("user", s"${User1}");
connectionProperties.put("password", s"${abc$@123}");

But error showing up in properties,

error: ';' expected but '@' found. connectionProperties.put("password", s"${dcu$@123}");

I am not able to find what the issue is, can someone help if you know.

Upvotes: 0

Views: 177

Answers (1)

Alex Ott
Alex Ott

Reputation: 87069

The s"${xxx}" syntax is used to refer to a value in variable or an expression, but it your case it looks like a raw password, and in Scala variable names may not contain special characters, like in your expression: abc$@123. The solution would be to remove s from the beginning of the string...

P.S. Better & more secure solution would be to put password into the Databricks secret scope and obtain it with dbutils.secrets.get.

Upvotes: 1

Related Questions