groovy-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Paul King <pa...@asert.com.au>
Subject Re: Convert JDBC Result sets to xml
Date Sun, 31 Jul 2016 12:53:00 GMT
You probably want to call toRowResult():

@Grab('org.hsqldb:hsqldb:2.3.2')
@GrabConfig(systemClassLoader=true)

import groovy.sql.Sql

def sql = Sql.newInstance('jdbc:hsqldb:mem:MyDb', 'sa', '',
'org.hsqldb.jdbcDriver')

sql.execute '''
  DROP TABLE MyTable IF EXISTS;
  CREATE TABLE MyTable (
    id   INTEGER,
    name   VARCHAR(64),
  );
'''
sql.execute '''
  INSERT INTO MyTable (id, name) VALUES (1, 'Test1')
  INSERT INTO MyTable (id, name) VALUES (2, 'Test2')
'''

def mb = new groovy.xml.MarkupBuilder()
mb.root {
  sql.eachRow('select * from MyTable') { next ->
    row {
      next.toRowResult().each { k, v ->
        "$k"(v)
      }
    }
  }
}

sql.close()

On Sun, Jul 31, 2016 at 7:43 PM, GroovyBeginner
<groovybeginner@gmail.com> wrote:
> I was trying to create the list initially in that case and then to xml with
> the following code
>
> import groovy.sql.Sql;
> import java.sql.ResultSet;
>
> Sql sql = Sql.newInstance("jdbc:oracle:thin:@localhost:1521:XE","username",
> "password", "oracle.jdbc.driver.OracleDriver")
>
> def emptyList = []
>
> sql.eachRow("select * FROM A") { row ->
>    emptyList.add("${row}")
> }
> and the result which am getting is like
> [groovy.sql.GroovyResultSetExtension@5a632bc3,
> groovy.sql.GroovyResultSetExtension@5a632bc3]. How do I convert this to the
> actual result. Also am a doing the correct approach initially by including
> all the results into a empty list and then converting it to xml.
>
> Please suggest me if there is any better approach of converting the db
> results into xml.
>
>
>
>
>
> --
> View this message in context: http://groovy.329449.n5.nabble.com/Convert-Oracle-Result-sets-to-xml-tp5734358p5734377.html
> Sent from the Groovy Dev mailing list archive at Nabble.com.

Mime
View raw message