Advertisement
Guest User

Untitled

a guest
Jun 6th, 2017
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.54 KB | None | 0 0
  1. /**
  2. * Created by Ashok Singamaneni using IntelliJ.
  3. */
  4.  
  5. /**
  6. **********application.properties**************
  7. *dev.hostname = nn01.itversity.com:3306
  8. *dev.database = hr
  9. *dev.username = hr_ro
  10. *dev.password = itversity
  11. */
  12.  
  13. /**
  14. *To run the below project, Run -> EditConfiguration -> Arguments -> dev
  15. */
  16.  
  17.  
  18. import java.sql.{Connection, DriverManager}
  19. import com.typesafe.config._
  20.  
  21. object CommisionAmount {
  22.  
  23. //Create a case class, which matches exactly with the output from the sql query
  24. case class EmployeesCommission(first_name: String,
  25. last_name: String,
  26. salary: Double,
  27. commission_pct: Double){
  28. /** override the toString() so that when we say println(EmployeesCommission), we get this toString() executed,
  29. * as case class has companion object .apply() method will get executed by default
  30. */
  31. override def toString(): String = {
  32. "Name: "+first_name+" "+last_name+", Salary: "+salary+", Commission: "+calculateEmployeeCommission()
  33. }
  34.  
  35. //see if employee is eligible for commission or not
  36. def calculateEmployeeCommission(): Any= {
  37. if(commission_pct == 0.0){
  38. "Not_Eligible"
  39. }else salary*commission_pct
  40. }
  41.  
  42. }
  43.  
  44. def main(args: Array[String]): Unit ={
  45.  
  46. //get the properties from application.properties
  47. val conf = ConfigFactory.load().getConfig(args(0))
  48.  
  49. //prepare the parameters for mysql connection
  50. val driver = "com.mysql.jdbc.Driver"
  51. val url = "jdbc:mysql://" + conf.getString("hostname") + "/" + conf.getString("database")
  52. val username = conf.getString("username")
  53. val password = conf.getString("password")
  54.  
  55. var connection:Connection = null
  56. try{
  57. //open connection to mysql
  58. Class.forName(driver)
  59. connection = DriverManager.getConnection(url, username, password)
  60.  
  61. //create the query and execute it
  62. val statement = connection.createStatement()
  63. val resultSet = statement.executeQuery("select first_name, last_name,salary,commission_pct from employees")
  64.  
  65. //loop through the result set and print
  66. while (resultSet.next()){
  67. val emp= EmployeesCommission(resultSet.getString("first_name"),
  68. resultSet.getString("last_name"),
  69. resultSet.getDouble("salary"),
  70. resultSet.getDouble("commission_pct"))
  71. println(emp)
  72. } //There is better way to iterate in functional programming (scala) using Iterator
  73. connection.close()
  74. }catch {
  75. case e => e.printStackTrace()
  76. connection.close()
  77. }
  78. }
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement