Groovy访问数据库,间接,方便。 需要提前将ojdbc5.jar放到groovy'的lib文件夹下面,或者确保你Groovy能去读该驱动,例如-cp .;xxx.jar; 等等
-
import groovy.sql.Sql
-
import java.sql.Connection;
-
-
sql = Sql.newInstance(
-
"jdbc:oracle:thin:@xxxxxx:orcl",
-
"OR01",
-
"OR01PW",
-
"oracle.jdbc.driver.OracleDriver")
-
println("执行查询语句");
-
//------------------------------------------------------------------------------
-
//-----从表-------------------------------------------------------------------------
-
sql.connection.autoCommit = false;
-
-
String insertSql = "INSERT INTO auth_token2(id, version, user2_id, token, expiration, description, os_user)"+
-
" VALUES(?, 0, ?, ?, ?, ?, ?)"
-
String getOldTokenSql = "SELECT U.user2_id, AT.token, AT.expiration,"+
-
" AT.description, UU.name FROM auth_token AT LEFT JOIN user U ON AT.user_id = U.id "
-
String deleteOldTokenSql = "DELETE FROM auth_token"
-
-
sql.eachRow(getOldTokenSql) { row ->
-
String tokenId = UUID.randomUUID()
-
String user2Id = row['user2_id']
-
String token = row['token']
-
def expiration = row['expiration'] (该字段为Long类型,可以删除中文注释)
-
String desc = row['description']
-
String name = row['name']
-
-
sql.executeUpdate(insertSql, [tokenId, user2Id, token, expiration, desc, name])
-
println "token ${token}"
-
}
-
-
sql.executeUpdate(deleteOldTokenSql);
-
sql.commit();
(责任编辑:IT) |