Skip to content

Instantly share code, notes, and snippets.

@lindacmsheard
Last active May 15, 2020 14:59
Show Gist options
  • Save lindacmsheard/3cebaef9e9877d29a1635b8dadd4cccb to your computer and use it in GitHub Desktop.
Save lindacmsheard/3cebaef9e9877d29a1635b8dadd4cccb to your computer and use it in GitHub Desktop.

Here’s a scala notebook cell to run an arbitrary TSQL batch using JDBC:

%scala

import java.util.Properties
import java.sql.DriverManager

val jdbcUsername = dbutils.secrets.get(scope = "kv", key = "sqluser")
val jdbcPassword = dbutils.secrets.get(scope = "kv", key = "sqlpassword")
val driverClass = "com.microsoft.sqlserver.jdbc.SQLServerDriver"

// Create the JDBC URL without passing in the user and password parameters.
val jdbcUrl = s"jdbc:sqlserver://dbrownesql.database.windows.net:1433;database=AdventureWorks;encrypt=true;trustServerCertificate=false;hostNameInCertificate=*.database.windows.net;loginTimeout=30;"

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

connectionProperties.put("user", s"${jdbcUsername}")
connectionProperties.put("password", s"${jdbcPassword}")
connectionProperties.setProperty("Driver", driverClass)

val connection = DriverManager.getConnection(jdbcUrl, jdbcUsername, jdbcPassword)
val stmt = connection.createStatement()
val sql = "delete from sometable where someColumn > 4"

stmt.execute(sql)
connection.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment