Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sql query for insert in grails

Tags:

sql

grails

How to execute plain sql in grails . I need to use sql query for inserting new record in database .

How can we achieve this with out using HQL and gorm relations .

thanks

like image 560
srinath Avatar asked Feb 11 '10 13:02

srinath


People also ask

What is the use of '@' in SQL?

The @CustID means it's a parameter that you will supply a value for later in your code. This is the best way of protecting against SQL injection. Create your query using parameters, rather than concatenating strings and variables.

What is insert syntax?

INSERT INTO Syntax It is possible to write the INSERT INTO statement in two ways: 1. Specify both the column names and the values to be inserted: INSERT INTO table_name (column1, column2, column3, ...) VALUES (value1, value2, value3, ...


1 Answers

groovy.sql.Sql simplifies the details of doing JDBC queries. In a Grails app you'd use the constructor that takes a DataSource:

import groovy.sql.Sql
...
class FooService {

   def dataSource
   ...
   def runSqlQuery(...) {
      Sql sql = new Sql(dataSource)
      sql.executeInsert("insert into ...")
      ...
   }
}

See these links for usage tips:

http://docs.codehaus.org/display/GROOVY/Tutorial+6+-+Groovy+SQL

http://www.ibm.com/developerworks/java/library/j-pg01115.html

like image 131
Burt Beckwith Avatar answered Oct 26 '22 04:10

Burt Beckwith