Vaadin 8 on Grails 3
  • Introduction
  • Project setup
    • Environment setup
      • Unix based systems
      • Windows
    • Creating Project
      • Command line
      • IntelliJ IDEA
    • Plugin Configuration
      • UI class
      • URL mapping
      • Production mode
      • Async support
      • Themes
      • SASS compilation
      • Widgetset compilation
      • Servlet class
      • Spring component scan
      • UI provider
      • Open session in view
    • Clean up
    • Best Practices
  • Database
    • GORM
      • Create Domain Model
      • Transactions
      • LazyInitializationException
      • Open Session In View I.
      • Open Session In View II.
      • Table Container
    • Groovy SQL
      • Create Sql Bean
      • Execute SQLs
    • MyBatis
      • MyBatis Configuration
      • Reading Data with MyBatis
    • JdbcTemplate
      • Create JdbcTemplate Beans
      • Using JdbcTemplate
    • Clean Up When Using Alternatives
  • UI
Powered by GitBook
On this page

Was this helpful?

  1. Database
  2. Groovy SQL

Execute SQLs

PreviousCreate Sql BeanNextMyBatis

Last updated 5 years ago

Was this helpful?

Example code is available on .

TODO: steps how to create entities...

In order to execute a query with just get dataSource bean from the application context, which we have created it in Create Sql article.

import com.vaadin.ui.UI
import com.vaadin.ui.VerticalLayout
import com.vaadin.server.VaadinRequest
import com.vaadin.ui.Label
import com.vaadin.grails.Grails
import groovy.sql.GroovyResultSet
import groovy.sql.Sql

class MyUI extends UI {

    @Override
    protected void init(VaadinRequest vaadinRequest) {
        VerticalLayout layout = new VerticalLayout()

        Sql sql = Grails.get(Sql)
        sql.eachRow("SELECT * FROM User") { GroovyResultSet result ->
            String firstName = result.getString('username')
            layout.addComponent(new Label(firstName))
        }

        setContent(layout)

        // Sql sql = new Sql(dataSource: Grails.applicationContext.getBean('dataSource'))
        // execute your queries
        // sql.close()
    }
}

Constructing SQL strings in Java or Groovy code is not easy and it is, for some of us, annoying. I recommend to use library to construct SQL strings.

github.com/vaadin-on-grails/groovy-sql
Groovy Sql
jOOQ