Skip to content

Instantly share code, notes, and snippets.

@AnantMishra30
Last active July 22, 2020 09:12
Show Gist options
  • Save AnantMishra30/286190a606fb8d346baf4f5dd1efd3bf to your computer and use it in GitHub Desktop.
Save AnantMishra30/286190a606fb8d346baf4f5dd1efd3bf to your computer and use it in GitHub Desktop.
Connector program for MSSql in Scala
import java.sql.DriverManager
import java.sql.Connection
object Connector {
def main(args: Array[String]): Unit = {
DriverManager.registerDriver(new com.microsoft.sqlserver.jdbc.SQLServerDriver());
// we can form this below URL in different forms to encrypt our password and DBName
val url = "jdbc:sqlserver://SERVER_INSTANCE;databaseName=DB_NAME;user=USER_NAME;password=PASSWORD;"
// there's probably a better way to do this
var connection:Connection = null
try {
// make the connection
connection = DriverManager.getConnection(url)
// create the statement, and run the select query
val statement = connection.createStatement()
val resultSet = statement.executeQuery("SELECT * FROM TABLE_NAME")
while ( resultSet.next() ) {
println(resultSet.getString(1) + " || " + resultSet.getString(2))
}
} catch {
case e => e.printStackTrace
}
connection.close()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment