0
我想在groovy中編寫一個DB util類,它需要一些強制性和可選的db參數,並將結果作爲映射列表返回。如何在groovy中編寫通用數據庫util類?
Groovy類
class DBUtil {
private final String ORACLE="oracle"
private final String DB2="db2"
private final String SYBASE="sybase"
private final String SQLSERVER="sqlserver"
private final String MSSQLSERVER="mssqlserver"
private final String ORACLE_DRIVER="jdbc:agra:oracle"
private final String DB2_DRIVER="jdbc:agra:db2"
private final String SQLSERVER_DRIVER="jdbc:agra:sqlserver"
private final String SYBASE_DRIVER="jdbc:agra:sybase"
private final String ORACLE_DRIVER_CLASS="com.agra.jdbc.oracle.OracleDriver"
private final String DB2_DRIVER_CLASS="com.agra.jdbc.db2.DB2Driver"
private final String SQLSERVER_DRIVER_CLASS="com.agra.jdbc.sqlserver.SQLServerDriver"
private final String SYBASE_DRIVER_CLASS="com.agra.jdbc.sybase.SybaseDriver"
private HashMap<String,String> dbparams
private HashMap<String,String> sqlStatements
private String url
private String username
private String password
private String driver
private String driverClass
public void setDbParams(HashMap<String,String> dbparams) {
this.dbparams=dbparams
}
DBUtil(dbparams, sqlStatements) {
this.dbparams=dbparams
this.sqlStatements=sqlStatements
}
private void validateDBParams() {
if (dbparams != null) {
try {
if (dbparams?.containsKey("driver")) {
driver=dbparams?.get("driver")
driverClass=dbparams?.get("driverClass")
}
username=dbparams?.get("username")
password=dbparams?.get("password")
switch(dbparams?.get("dbtype")) {
case ORACLE:
if (!dbparams?.containsKey("driver")) {
driver=ORACLE_DRIVER
driverClass=ORACLE_DRIVER_CLASS
}
break
case DB2:
if (!dbparams?.containsKey("driver")) {
driver=DB2_DRIVER
driverClass=DB2_DRIVER_CLASS
}
break
case SYBASE:
if (!dbparams?.containsKey("driver")) {
driver=SYBASE_DRIVER
driverClass=SYBASE_DRIVER_CLASS
}
break
case SQLSERVER:
case MSSQLSERVER:
if (!dbparams?.containsKey("driver")) {
driver=SQLSERVER_DRIVER
driverClass=SQLSERVER_DRIVER_CLASS
}
break
}
url=driver+"://"+dbparams?.get("connectstring")
} catch (Exception e) {
e.printStackTrace()
}
}
}
public List<Map> execute() {
List<Map> resultSet;
def sql
try {
if (dbparams != null && sqlStatements != null) {
validateDBParams()
//println "$url, $username, $password, $driverClass"
sql = Sql.newInstance(url, username, password, driverClass)
sqlStatements.each {key, value->
List<Map> sqlResult = new ArrayList<Map>()
sql.eachRow(value) { row->
println row
}
//resultSet.add(sqlResult)
//println sqlResult
}
}
}
catch(SQLException se) {
println "Exception encountered in DBUtil execute() $se"
}
return resultSet
}
static main(args) {
HashMap<String,String> dbparams = ["dbtype":"oracle", "username":"johnkc", "password":"johnc", "connectstring":"apple:1521;SID=ORCL;",
"driver":"jdbc:agra:oracle","driverClass":"com.agra.jdbc.oracle.OracleDriver"]
HashMap<String,String> sqlStatements = ["sql1":"select name, value from v\$parameter",
"sql2":"select POO_NAME, POO_VALUE from PO_OPTION"]
DBUtil db = new DBUtil(dbparams, sqlStatements)
db.execute()
}
}
執行後,它給了我下面的結果(鑰匙可超過2)
[NAME:lock_name_space, VALUE:[null]]
[NAME:processes, VALUE:1000]
[NAME:sessions, VALUE:1105]
[POO_NAME:username, POO_VALUE:[email protected]]
[POO_NAME:jdbcurl, POO_VALUE:[email protected]]
[POO_NAME:dbType, POO_VALUE:[email protected]]
[POO_NAME:ConnectionString, POO_VALUE:[email protected]]
我怎樣才能把上面的輸出以下格式(地圖列表)?
[ "sql1": [ {row1}, {row2}... ],
"sql2": [ {row1}, {row2}... ],
...
]
其中ROW1 ... = {列1:值1,列2:值2,欄3:值3 ...}
更新
下面裏面sqlStatements.each
片段讓我對每個列名
def tableColumns=[:]
sql.rows(value, {meta ->
def columns=[]
int colCount=meta.columnCount
(1..colCount).each{
columns.add(meta.getColumnName(it))
}
tableColumns.put(key, columns)
})
更新我的解決方案
public List<Map> execute() {
List<Map<String,Object>> resultSet=new ArrayList<HashMap<String,Object>>()
def sql
try {
if (dbparams != null && sqlStatements != null) {
boolean validParams=validateDBParams()
if (validParams) {
sql = Sql.newInstance(url, username, password, driverClass)
sqlStatements.each {key, value->
def sqlResultMap=[:]
def tableColumns=[:]
sql.rows(value, {meta ->
def columns=[]
int colCount=meta.columnCount
(1..colCount).each{
columns.add(meta.getColumnName(it))
}
tableColumns.put(key, columns)
})
def rows=[]
sql.eachRow(value) {row->
def dbrow=[:]
tableColumns.get(key).each{ columnname->
dbrow.put(columnname, row."$columnname")
}
rows.add(dbrow)
}
sqlResultMap.put(key, rows)
resultSet.add(sqlResultMap)
}
}
}
}
catch(SQLException se) {
println "Exception encountered in DBUtil execute() $se"
}
return resultSet
}
它給出了一張地圖列表。
謝謝您的回答和+1對再保建議。此外,我需要返回類型的地圖列表'列表