Advertisement
Guest User

Untitled

a guest
Jun 21st, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.14 KB | None | 0 0
  1. /**
  2. * Created by itversity on 21/02/17.
  3. */
  4.  
  5. import java.sql.DriverManager
  6. import com.typesafe.config._
  7.  
  8. case class EmployeesCommission(first_name: String,
  9. last_name: String,
  10. salary: Double,
  11. commission_pct: Double) {
  12. override def toString(): String = {
  13. s"first_name: " + first_name + ";" + "last_name: " + last_name +
  14. ";" + "commission amount:" + getCommissionAmount()
  15. }
  16.  
  17. def getCommissionAmount(): Any = {
  18. if(commission_pct == 0.0) {
  19. "Not eligible"
  20. } else salary * commission_pct
  21. }
  22. }
  23.  
  24. object CommissionAmount {
  25. def main(args: Array[String]): Unit = {
  26. val props = ConfigFactory.load()
  27. val driver = "com.mysql.jdbc.Driver"
  28. val host = props.getConfig(args(0)).getString("host")
  29. val port = props.getConfig(args(0)).getString("port")
  30. val db = props.getConfig(args(0)).getString("db")
  31. val url = "jdbc:mysql://" + host + ":" + port + "/" + db
  32. val username = props.getConfig(args(0)).getString("user")
  33. val password = props.getConfig(args(0)).getString("pw")
  34.  
  35. Class.forName(driver);
  36. val connection = DriverManager.getConnection(url, username, password)
  37. val statement = connection.createStatement()
  38. val resultSet = statement.executeQuery(s"SELECT first_name, last_name, " +
  39. "salary, commission_pct FROM employees")
  40.  
  41. // Functional way of processing resultset
  42. Iterator.
  43. continually((resultSet.next(), resultSet)).
  44. takeWhile(_._1).
  45. map(r => {
  46. EmployeesCommission(
  47. r._2.getString("first_name"),
  48. r._2.getString("last_name"),
  49. r._2.getDouble("salary"),
  50. r._2.getDouble("commission_pct")
  51. )
  52. }).
  53. toList.
  54. foreach(println)
  55.  
  56. // Traditional way of processing the data
  57. // while (resultSet.next()) {
  58. // val e = EmployeesCommission(resultSet.getString("first_name"),
  59. // resultSet.getString("last_name"),
  60. // resultSet.getDouble("salary"),
  61. // if(resultSet.getDouble("commission_pct").isNaN) -1.0 else resultSet.getDouble("commission_pct"))
  62. // println(e)
  63. // }
  64.  
  65.  
  66. }
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement