Advertisement
Guest User

Untitled

a guest
Nov 8th, 2016
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.47 KB | None | 0 0
  1. @GrabConfig(systemClassLoader=true)
  2. @Grab(group='com.h2database', module='h2', version='1.3.176')
  3.  
  4. import java.sql.*
  5. import groovy.sql.Sql
  6. import org.h2.jdbcx.JdbcConnectionPool
  7.  
  8. println("More groovy...")
  9.  
  10. def sql = Sql.newInstance("jdbc:h2:things", "sa", "sa", "org.h2.Driver") // DB files for 'things' in current directory (./hello.h2.db)
  11. //def sql = Sql.newInstance("jdbc:h2:file:things", "sa", "sa", "org.h2.Driver") // 'file' keyword is optional
  12. //def sql = Sql.newInstance("jdbc:h2:~/things", "sa", "sa", "org.h2.Driver") // DB files for 'things' in current user home directory (~/things.h2.db)
  13. //def sql = Sql.newInstance("jdbc:h2:/var/h2/things", "sa", "sa", "org.h2.Driver") // DB files for 'things' in specified directory (/var/h2/things.h2.db)
  14.  
  15. sql.execute("DROP TABLE IF EXISTS things")
  16.  
  17. def createTbl = '''
  18. CREATE TABLE things (
  19. id INT PRIMARY KEY,
  20. thing1 VARCHAR(50),
  21. thing2 VARCHAR(100)
  22. )
  23. '''
  24.  
  25. sql.execute(createTbl)
  26. sql.execute("INSERT INTO things VALUES(:id, :thing1, :thing2)", [id: 0, thing1: 'I am thing1', thing2: 'I am thing2'])
  27. sql.execute("INSERT INTO things VALUES(:id, :thing1, :thing2)", [id: 1, thing1: 'foo', thing2: 'bar'])
  28. sql.execute("INSERT INTO things VALUES(:id, :thing1, :thing2)", [id: 2, thing1: 'Alisa', thing2: 'Yeoh'])
  29.  
  30. def query = "SELECT * FROM things"
  31.  
  32. sql.eachRow(query) { row ->
  33. println "$row.id - ${row.thing1}::$row.thing2"
  34. }
  35.  
  36. //Housekeeping
  37. sql.close()
  38.  
  39. println("More java-ish...")
  40.  
  41. def dropTblTester = "DROP TABLE IF EXISTS tester"
  42.  
  43. def createTestTbl = '''
  44. CREATE TABLE tester (
  45. id INT PRIMARY KEY,
  46. thing1 VARCHAR(50),
  47. thing2 VARCHAR(100)
  48. )'''
  49.  
  50. JdbcConnectionPool cp = JdbcConnectionPool.create("jdbc:h2:test", "sa", "sa")
  51. Connection conn = cp.getConnection()
  52. conn.createStatement().execute(dropTblTester)
  53. conn.createStatement().execute(createTestTbl)
  54.  
  55. conn.createStatement().execute("INSERT INTO tester (id, thing1, thing2) VALUES (0, 'x', 'y')")
  56. conn.createStatement().execute("INSERT INTO tester (id, thing1, thing2) VALUES (1, 'a', 'b')")
  57. conn.createStatement().execute("INSERT INTO tester (id, thing1, thing2) VALUES (2, 'e', 'f')")
  58. Statement stmt = conn.createStatement()
  59. ResultSet rs = stmt.executeQuery("SELECT * FROM tester")
  60.  
  61. while (rs.next()) {
  62. int id = rs.getInt("id")
  63. String t1 = rs.getString("thing1")
  64. String t2 = rs.getString("thing2")
  65. println("$id::$t1::$t2")
  66. }
  67. //Housekeeping
  68. conn.close()
  69. cp.dispose()
  70.  
  71. // Expect:
  72.  
  73. /*
  74. More groovy...
  75. 0 - I am thing1::I am thing2
  76. 1 - foo::bar
  77. 2 - Alisa::Yeoh
  78. More java-ish...
  79. 0::x::y
  80. 1::a::b
  81. 2::e::f
  82. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement