I need to connect my MySQL database from Groovy script. Since the intend of this script is to use for db update with minor change in it; I need to able to supply the necessary connection parameters with jar file. I would like to input connection parameters such as dbUrl, dbUsername, and dbPassword dynamically so that I can input those as per database. Below code successfully connect to database, but it is manual. I have to create jar file for each database if I have to use for other database than 'test'.
I am relatively new to groovy, any help/suggestion would be appreciated.
How do I proceed with this?
import groovy.sql.Sql
import java.sql.*
import java.io.File
Sql sql
def cities = []
try{
def url= 'jdbc:mysql://localhost:3306/test'
def username = 'username'
def password = 'password'
def driver = 'org.mariadb.jdbc.Driver'
sql = Sql.newInstance( url, username, password, driver)
sql.eachRow("select * from City where city are not null"){cities += it.city}
}catch(ClassNotFoundException | SQLException e){
e.printStackTrace
}