简体   繁体   English

Grails GORM如何在我的应用程序中加载现有的远程数据库表

[英]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. 我需要使用不是我的MySQL数据库中的一些表。 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? 如何在我的grails 2.4.3应用程序中设置它?

You can create a new Groovy sql instance like below 您可以创建一个新的Groovy sql实例,如下所示

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 可以在这里查看详细信息: http : //groovy.codehaus.org/Tutorial+6+-+Groovy+SQL

You could add mysql/oracle details instead of postgres. 您可以添加mysql / oracle详细信息,而不是postgres。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM