Search code examples
mysqlgrailsgroovygrails-orm

Grails GORM how to load existing remote DB tables in my application


I need to use some tables from a MySQL database which is not mine. I can connect to the database remotely (with known host, db name, user, password). I know the structure of the database but I don't want to create new tables, modify a structure of existing tables etc.

The only thing I want to do is to load records (rows) from this DB in my application and modify its column values or add a new row...

Can you give me some advice please? How to set it in my grails 2.4.3 application?


Solution

  • You can create a new Groovy sql instance like below

    import groovy.sql.Sql
    ...
    final Sql sql = Sql.newInstance("jdbc:postgresql://localhost:5432/<db-name>",
                    <user>, 
                    <password>, 
                    "org.postgresql.Driver")
    sql.eachRow( 'select * from tableName' ) { println "$it.id -- ${it.firstName} --" }
    

    Detailed info can be seen here: http://groovy.codehaus.org/Tutorial+6+-+Groovy+SQL

    You could add mysql/oracle details instead of postgres.