mysql - Play - Execute an sql statement without writing the database configuration in application.conf -
i writing scala application using playframework. don't want write database configuration in application.conf file. instead have function similar 1 given below. want send configuration parameters arguments of function. want create connection , execute statement. btw, database mysql database.
def run_sql(sql: string, host: string, user: string, pass: string, port: string, dbname: string): unit = { // **** create connection **** db.withconnection { conn => { val statement = conn.createstatement(resultset.type_forward_only, resultset.concur_read_only) try { statement.execute(sql) } catch { case e: exception => logger.warn("error executing dml %s. %s".format(sql, e.getmessage)) } } } }
is possible? if how? hope have made question clear. if have confusion please ask. in advance.
// **** create connection ****
here can write
val driver = play.current.configuration.getstring("rds.driver").getorelse("") val host = play.current.configuration.getstring("rds.host").getorelse("") val port = play.current.configuration.getstring("rds.port").getorelse("") val user = play.current.configuration.getstring("rds.user").getorelse("") val password = play.current.configuration.getstring("rds.password").getorelse("") val url = "mysql://" + user + ":" + password + "@" + host + ":" + port + "/" + dbname // there's better way class.forname(driver) val connection: connection = drivermanager.getconnection(url, user, password)
Comments
Post a Comment