Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Created by Ashok Singamaneni using IntelliJ.
- */
- /**
- **********application.properties**************
- *dev.hostname = nn01.itversity.com:3306
- *dev.database = hr
- *dev.username = hr_ro
- *dev.password = itversity
- */
- /**
- *To run the below project, Run -> EditConfiguration -> Arguments -> dev
- */
- import java.sql.{Connection, DriverManager}
- import com.typesafe.config._
- object CommisionAmount {
- //Create a case class, which matches exactly with the output from the sql query
- case class EmployeesCommission(first_name: String,
- last_name: String,
- salary: Double,
- commission_pct: Double){
- /** override the toString() so that when we say println(EmployeesCommission), we get this toString() executed,
- * as case class has companion object .apply() method will get executed by default
- */
- override def toString(): String = {
- "Name: "+first_name+" "+last_name+", Salary: "+salary+", Commission: "+calculateEmployeeCommission()
- }
- //see if employee is eligible for commission or not
- def calculateEmployeeCommission(): Any= {
- if(commission_pct == 0.0){
- "Not_Eligible"
- }else salary*commission_pct
- }
- }
- def main(args: Array[String]): Unit ={
- //get the properties from application.properties
- val conf = ConfigFactory.load().getConfig(args(0))
- //prepare the parameters for mysql connection
- val driver = "com.mysql.jdbc.Driver"
- val url = "jdbc:mysql://" + conf.getString("hostname") + "/" + conf.getString("database")
- val username = conf.getString("username")
- val password = conf.getString("password")
- var connection:Connection = null
- try{
- //open connection to mysql
- Class.forName(driver)
- connection = DriverManager.getConnection(url, username, password)
- //create the query and execute it
- val statement = connection.createStatement()
- val resultSet = statement.executeQuery("select first_name, last_name,salary,commission_pct from employees")
- //loop through the result set and print
- while (resultSet.next()){
- val emp= EmployeesCommission(resultSet.getString("first_name"),
- resultSet.getString("last_name"),
- resultSet.getDouble("salary"),
- resultSet.getDouble("commission_pct"))
- println(emp)
- } //There is better way to iterate in functional programming (scala) using Iterator
- connection.close()
- }catch {
- case e => e.printStackTrace()
- connection.close()
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement