Friday, 10 May 2019

Jdbc Connectivity in Scala

Scala Jdbc Connectivity

Open Intellij IDE and click on File --> New--> Project
Select Scala from left panel and then select sbt from right panel and click on Next button.
Now, enter project name and it's related details as shown in below image.
After clicking on Finish button. You will see below screen
After that create one package inside your project src--> main--> scala. 
Right click on scala folder and select new and then package for more info see below image
Now, we have to do some configuration related changes which is as below :
1. Go to Run menu --> Edit Configurations...
Check your Working directory, JRE and Use classpath of module entry whether they are configured correctly or not then click on Apply and OK button. Once configuration setting completed.

Now, open build.sbt file and add below line(for mysql connection) and save it
libraryDependencies += "mysql" % "mysql-connector-java" % "5.1.12"

Build your complete project and refresh it. To reflect all changes.

Once you have done with all above steps. Now, move to package folder and right click on it then click on New --> Scala class as shown in below image
Enter Name and select kind.
We entered name as "TScala_Jdbc_Val" and kind as "object" see below screen for details

I have created table in mysql database which is having below three columns and below values.
id name age
2 Rohan 21
1 Ram 20
3 Rahul 22
4 Rakesh 23
5 Raju 24
1 Rahul 22
Using scala we are going to fetch above table value :
//Code for Jdbc connection with mysql
//
package TScala_JDBC
import java.sql.{Connection, DriverManager}
object TScala_Jdbc_Val {
  def main(args:Array[String]): Unit ={

    // connect to the database named "mysql" on the localhost
    val driver = "com.mysql.jdbc.Driver"        //driver for connecting mysql db
    val url = "url_to_access_database" //url for accessing db/table
    val username = "username"       //username
    val password = "******"             //password

    // there's probably a better way to do this
    var connection:Connection = null       //initialize connection with null value

    try{
      // make the connection
      Class.forName(driver)
      connection = DriverManager.getConnection(url, username, password)
      // create the statement, and run the select query
      val statement = connection.createStatement()
      //store result in a resultset using SELECT operation
      val resultSet = statement.executeQuery("SELECT id, name,age FROM sample")
      while ( resultSet.next() ) {
        val id = resultSet.getInt("id")
        val name = resultSet.getString("name")
        val age = resultSet.getInt("age")
        println( + id + ", " + name + "," + age)
      }
    }
    catch{
      case e => e.printStackTrace
    }

  }
}
//
//
Output :
2, Rohan,21
1, Ram,20
3, Rahul,22
4, Rakesh,23
5, Raju,24
1, Rahul,22
//


No comments:

Post a Comment

Use filter method to filter a Scala collection

Use filter method to filter a Scala collection To use filter on your collection, give it a predicate to filter the collection elements as...