I'm novice in Grails and I have a doubt. Lets say I have my Datasource.groovy
configured to mysql
database. Everything works fine and my grails app is running.
In the controller, if I want to add/modify the database that I have set in DataSource.groovy
how I can do that?
Again I need to do something like this in each controller :
def db = Sql.newInstance(
'jdbc:mysql://*****',
'root',
'',
'com.mysql.jdbc.Driver'
)
in order to get the db instance that I point to DataSource.groovy
file?
Are there any best practices to do so?
You should be able to do this (I would recommend this is done in a Service rather than a Controller, as it will make your controllers cleaner and you code easier to follow)
import groovy.sql.Sql
class DataSourceAccessingService {
def datasource
def runSomeQuery( String sql ) {
def sql = new Sql( datasource )
...
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With