我正在尝试从表中获取整个数据,结果应该是逗号分隔的字符串。这是我的代码,工作正常。
import groovy.sql.Sql;
import java.sql.ResultSet;
def temp="";
def temp1="";
sql = Sql.newInstance("jdbc:oracle:thin:@localhost:1521:XE","username", "password", "oracle.jdbc.driver.OracleDriver")
sql.eachRow("select * FROM employee") {
temp1=it.toRowResult().values().join(", ")
if(temp=="")
{
temp=temp1;
}
else
{
temp=temp+"\n"+temp1
}
}
请建议在Groovy中是否有更好的方法来处理此要求。
答案 0 :(得分:0)
保持简单的解决方案如下:
import groovy.sql.Sql;
import java.sql.ResultSet;
Sql sql = Sql.newInstance("jdbc:oracle:thin:@localhost:1521:XE","username", "password", "oracle.jdbc.driver.OracleDriver")
StringBuilder builder = new StringBuilder()
sql.eachRow("select * FROM employee") { row ->
builder.append( "${row.employeeId}," ) // no idea what your column names are
}
return builder.toString() // should trim trailing comma but I'll save that for you
您也可以使用inject()执行此操作,但我之前没有尝试过SQL结果集。