Guest User

Untitled

a guest
Jun 15th, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.14 KB | None | 0 0
  1.  
  2. package com.reumann.examples
  3.  
  4. import groovy.sql.Sql
  5.  
  6. class PersistenceGroovy {
  7.  
  8. def getSystems() {
  9. def sql = Sql.newInstance("jdbc:jtds:sybase://d1npp.nielsenmedia.com:2025;DatabaseName=NPP_GUI", "npp_user", "npp_user", "net.sourceforge.jtds.jdbc.Driver")
  10. def results = []
  11. sql.eachRow("SELECT nppSystemID, statusCodeID, nppSystemName, archiveDays, versionNumber, internalModeFlag FROM NPPSystem") {
  12. results.add( it.toRowResult() ) //or could do results << it.toRowResult()
  13. }
  14. //iterate
  15. results.each {
  16. print "nppSystemID: $it.nppSystemID"
  17. println " --- nppSystemName: $it.nppSystemName"
  18. }
  19. return results
  20. }
  21.  
  22. def getSystems2() {
  23. def sql = Sql.newInstance("jdbc:jtds:sybase://d1npp.nielsenmedia.com:2025;DatabaseName=NPP_GUI", "npp_user", "npp_user", "net.sourceforge.jtds.jdbc.Driver")
  24. def results = []
  25. sql.eachRow("SELECT nppSystemID, statusCodeID, nppSystemName, archiveDays, versionNumber, internalModeFlag FROM NPPSystem") {
  26. //we can act like SystemNPP has a constuctor even though we didn't make one:
  27. results << new SystemNPP(nppSystemID: it.nppSystemID, statusCodeID: it.statusCodeID, nppSystemName: it.nppSystemName, archiveDays: it.archiveDays,
  28. versionNumber: it.versionNumber, internalModeFlag: it.internalModeFlag )
  29. }
  30. //iterate
  31. results.each {
  32. print "nppSystemID: $it.nppSystemID"
  33. println " --- nppSystemName: $it.nppSystemName"
  34.  
  35. }
  36. return results
  37. }
  38.  
  39. }
  40.  
  41. /*
  42. * We can just use the rowResult as is, which is nice and easy when you just plan to access by the db field names directly.
  43. * However if we wanted to make an object we could as shown below, and note we don't need to create getters setters. Behind the
  44. * scenes groovy will make them and code that needs to call them will. Here is that example:
  45. *
  46. *
  47. */
  48. class SystemNPP {
  49. def nppSystemID
  50. def statusCodeID
  51. def nppSystemName
  52. def archiveDays
  53. def versionNumber
  54. def internalModeFlag
  55. }
Add Comment
Please, Sign In to add comment