Advertisement
Guest User

Untitled

a guest
May 25th, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.48 KB | None | 0 0
  1. //externalizing the properties--we are doing this to avoid hardcoding and reusability of the code for all
  2. //environments
  3.  
  4. /**
  5. * Created by Tanwir on 23-05-2017.
  6. */
  7. import java.sql.DriverManager
  8. import java.sql.Connection
  9. //1.first we define the dependendency of the typesafe in build.sbt. This is used for externalizing the properties
  10. //2. After that we will create a folder under the main folder of the main folder as reference folder and create a file in the
  11. //reference folder named application. properties. This file will contain all the details of the connection e.g, username,
  12. //password etc. We can use the same file for defining the connection for all the environment, beit dev, prod or UAT.
  13.  
  14. import com.typesafe.config._
  15. case class EmployeesCommission(first_name: String,
  16. last_name: String,
  17. salary: Double,
  18. commission_pct:Double) {
  19. override def toString(): String = {
  20. s"first_name: " + first_name + ";" + "last_name: " +last_name + ";"+ "commission_amount:" + getCommissionAmount()
  21. }
  22. def getCommissionAmount(): Any ={
  23. if(commission_pct==null){
  24. "Not Eligible"
  25. }else salary * commission_pct
  26. }
  27.  
  28. }
  29. object CommisionAmount {
  30.  
  31. def main(args: Array[String]): Unit ={
  32. val props = ConfigFactory.load()
  33. //3.We call the ConfigFactory.load method to loadad the application.properties values
  34. //we store the values in props.
  35. val driver = "com.mysql.jdbc.Driver"
  36. val host = props.getConfig(args(0)).getString("host")
  37. //4.We use the props variable to access the connection variables.
  38. //here we could have directly used props.getString("host") to get the host name, but as we are using the same
  39. //application.properties file to define the connection of different environments(dev,prod,UAT) we use the getConfig(args).
  40. //here getConfig(args(0)) means the argument 0 refers to
  41. //the development environment variables(e.g,dev.host = nn01.itversity.com), if we are defining the production
  42. //connection values after the development connection values then the argument will be 1 and so on for other
  43. //environment definition. we need to configure the dev argument in intellij so that while reffering dev.host or dev.db in the code it
  44. //recognises the statement. To do that go to run-->edit configurations-->program arguments-->type dev
  45.  
  46. val port = props.getConfig(args(0)).getString("port")
  47. val db = props.getConfig(args(0)).getString("db")
  48. val url = "jdbc:mysql://" + host + ":" + port + "/" + db
  49. val username = props.getConfig(args(0)).getString("user")
  50. val password = props.getConfig(args(0)).getString("pw")
  51. Class.forName(driver)
  52.  
  53. val connection = DriverManager.getConnection(url, username, password)
  54. val statement = connection.createStatement()
  55. val resultSet = statement.executeQuery(s"SELECT first_name, last_name, " +
  56. "salary, commission_pct from employees")
  57.  
  58. Iterator.continually((resultSet, resultSet.next)).
  59. takeWhile(_._2).
  60. map(_._1).map(rec=>{
  61. EmployeesCommission(rec.getString("first_name"),
  62. rec.getString("last_name"),
  63. rec.getDouble("salary"),
  64. rec.getDouble("commission_pct"))
  65. }).foreach(rec=>{
  66. println(rec)
  67. })
  68.  
  69. // while ( resultSet.next() ) {
  70. // val e = EmployeesCommission(resultSet.getString("first_name"),
  71. // resultSet.getString("last_name"),
  72. // resultSet.getDouble("salary"),
  73. // resultSet.getDouble("commission_pct"))
  74. // println(e)
  75. // }
  76.  
  77. }
  78.  
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement