DBConnection

PostgreSQL Connection Test

Scala code for DB Connection

DBConnectionTest.scala

import java.sql.{Connection, DriverManager}

object DBConnectionTest extends App{

  println("-- PostgreSQL Connection Test --")
  val url = "jdbc:postgresql://localhost:5307/DBschema?user=userName&password=userNamePwd"

  var connection:Connection = null
  Class.forName("org.postgresql.Driver")
  connection = DriverManager.getConnection(url)

  if (connection != null) println("!!! DB Connection Successful !!!")
  else println("Failed to connect")

  val statement = connection.createStatement()
  val resultSet = statement.executeQuery("SELECT job_id, workflow_name FROM reactorx_job limit 1")
  resultSet.next()
  val job_id = resultSet.getString("job_id")
  val workflow_name = resultSet.getString("workflow_name")
  println(s"Fetched one row from DB -- job_id : $job_id & workflow_name $workflow_name")

  connection.close()
}

Java Code for DB Connection

PostgresTest.Java

Run Result

Please Note

Mac/Unix -> separator is ':'

Windows -> separator is ';'

Last updated

Was this helpful?