Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //externalizing the properties--we are doing this to avoid hardcoding and reusability of the code for all
- //environments
- /**
- * Created by Tanwir on 23-05-2017.
- */
- import java.sql.DriverManager
- import java.sql.Connection
- //1.first we define the dependendency of the typesafe in build.sbt. This is used for externalizing the properties
- //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
- //reference folder named application. properties. This file will contain all the details of the connection e.g, username,
- //password etc. We can use the same file for defining the connection for all the environment, beit dev, prod or UAT.
- import com.typesafe.config._
- case class EmployeesCommission(first_name: String,
- last_name: String,
- salary: Double,
- commission_pct:Double) {
- override def toString(): String = {
- s"first_name: " + first_name + ";" + "last_name: " +last_name + ";"+ "commission_amount:" + getCommissionAmount()
- }
- def getCommissionAmount(): Any ={
- if(commission_pct==null){
- "Not Eligible"
- }else salary * commission_pct
- }
- }
- object CommisionAmount {
- def main(args: Array[String]): Unit ={
- val props = ConfigFactory.load()
- //3.We call the ConfigFactory.load method to loadad the application.properties values
- //we store the values in props.
- val driver = "com.mysql.jdbc.Driver"
- val host = props.getConfig(args(0)).getString("host")
- //4.We use the props variable to access the connection variables.
- //here we could have directly used props.getString("host") to get the host name, but as we are using the same
- //application.properties file to define the connection of different environments(dev,prod,UAT) we use the getConfig(args).
- //here getConfig(args(0)) means the argument 0 refers to
- //the development environment variables(e.g,dev.host = nn01.itversity.com), if we are defining the production
- //connection values after the development connection values then the argument will be 1 and so on for other
- //environment definition. we need to configure the dev argument in intellij so that while reffering dev.host or dev.db in the code it
- //recognises the statement. To do that go to run-->edit configurations-->program arguments-->type dev
- val port = props.getConfig(args(0)).getString("port")
- val db = props.getConfig(args(0)).getString("db")
- val url = "jdbc:mysql://" + host + ":" + port + "/" + db
- val username = props.getConfig(args(0)).getString("user")
- val password = props.getConfig(args(0)).getString("pw")
- Class.forName(driver)
- val connection = DriverManager.getConnection(url, username, password)
- val statement = connection.createStatement()
- val resultSet = statement.executeQuery(s"SELECT first_name, last_name, " +
- "salary, commission_pct from employees")
- Iterator.continually((resultSet, resultSet.next)).
- takeWhile(_._2).
- map(_._1).map(rec=>{
- EmployeesCommission(rec.getString("first_name"),
- rec.getString("last_name"),
- rec.getDouble("salary"),
- rec.getDouble("commission_pct"))
- }).foreach(rec=>{
- println(rec)
- })
- // while ( resultSet.next() ) {
- // val e = EmployeesCommission(resultSet.getString("first_name"),
- // resultSet.getString("last_name"),
- // resultSet.getDouble("salary"),
- // resultSet.getDouble("commission_pct"))
- // println(e)
- // }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement