Tag Archives: groovy

Sql queries under transaction in Groovy

trans
When you wish to perform some sql queries in transaction, use withTransaction method:

this.class.classLoader.rootLoader.addURL(
  new URL("file:///$jbossPath/lib/ojdbc6.jar"))
Sql sql = Sql.newInstance("ds.url", "ds.user.name", 
  "ds.user.password", "oracle.jdbc.driver.OracleDriver")

sql.withTransaction {
  sql.eachRow("SELECT * FROM USERS") {
    sql.executeUpdate("update USERS set PASSWORD = ? where USER_ID = ?", 
      ["123456", it.USER_ID])
  }
}

Hashing password in Java and Groovy

hash
If you have to get a hash from string, you can use next:

In java:

public static String getPasswordHash(String value) {
  byte[] bytesOfPassword = value.getBytes("UTF-8");
  MessageDigest md = MessageDigest.getInstance("SHA-256");
  md.update(bytesOfPassword);
  byte[] bytesOfEncryptedPassword = md.digest();
  return new String(bytesOfEncryptedPassword);
}

In Groovy (with Base64 encoding!):

def sha256Hash = { text ->
  java.security.MessageDigest.getInstance("SHA-256") 
    .digest(text.getBytes("UTF-8")).encodeBase64().toString()
}

Convert String to GString in Groovy

groovy-logo-medium
Just use this to convert from string to groovy string:

String someStr = "here must be"
def gstring = "${someStr}"