Grails: Create dynamic SQL-Connection

后端 未结 2 1635
醉酒成梦
醉酒成梦 2021-01-06 15:30

For my application I need dynamic database connections at runtime. I know, there are ways to create multiple datasources but they are not that dynamically I think. Scenari

2条回答
  •  粉色の甜心
    2021-01-06 16:29

    As long as you have the JDBC drivers for all the datasources on your classpath, you can create an instance of groovy.sql.Sql that will connect to whatever database you like, e.g.

    Sql sql = Sql.newInstance('jdbc:hsqldb:mem:testDB', 'sa', 'myPassword', 
        'org.hsqldb.jdbc.JDBCDriver')
    
    // now use the Sql instance to execute a query, or whatever....
    

提交回复
热议问题