2015年3月14日 星期六

groovy jdbc stored procedure return resultset

import groovy.sql.Sql
import java.sql.*
import java.io.*
import oracle.jdbc.driver.*
import org.compiere.util.Env
import org.compiere.util.DB
int client=Env.getContextAsInt(A_Ctx, "AD_Client_ID")
int org=Env.getContextAsInt(A_Ctx, "AD_Org_ID")
int uid=Env.getContextAsInt(A_Ctx, "#AD_User_ID")
def now= System.currentTimeMillis()
//  sql = Sql.newInstance( 'jdbc:jtds:sqlserver://serverName/dbName-CLASS;domain=domainName', 'username',  'password', 'net.sourceforge.jtds.jdbc.Driver' )
sql = Sql.newInstance("jdbc:oracle:thin:@127.0.0.1:1521:ORCL","adempiere", "adempiere","oracle.jdbc.driver.OracleDriver")
// sql.eachRow( 'SELECT Value, Name FROM AD_Rule' ) { println "$it.Value -- ${it.Name} --" }
// row = sql.firstRow('SELECT Value, Name FROM AD_Rule')
// println "Row: Value= ${row.Value} and Name = ${row.Name}"
ptable= sql.dataSet("AD_Rule_T")
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection conn = null;
conn = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:ORCL","adempiere", "adempiere");
CallableStatement call = conn.prepareCall ("{ ? = call 讀取規則設定.AD_Rule_Listing (?)}");
call.registerOutParameter (1, OracleTypes.CURSOR);
call.setString (2, "%");
call.execute ();
ResultSet rset = (ResultSet)call.getObject (1);
while (rset.next ()){
vname=rset.getString ("Name");
vvalue=rset.getString ("Value");
sql.query("SELECT nextidBYNAMEfunc('AD_Rule', 'N') as id , Generate_UUID() as uu FROM DUAL ")
{ResultSet rsuu -> if (rsuu.next()) { id = rsuu.getInt('id'); uu = rsuu.getString('uu'); }}
ptable.add(ad_client_id:client, ad_org_id:org,  isactive: 'Y', createdby:uid,  updatedby:uid, AD_Rule_ID:id,  AD_Rule_UU:uu, Value:vvalue, Name:vname)
}
rset.close();
call.close();
conn.close();
return "成功..."

沒有留言:

張貼留言