我尝试使用 Excel 作为数据库,并使用了 fillo 依赖项。 我尝试过使用单个参数,效果很好。但是当我尝试使用多个参数时,它会弹出以下错误:
" The method getField(String) in the type Recordset is not applicable for the arguments (String[])", error is on "multi_data".
这是代码:
public static void read_by_excel(String query, String field, String ... multi_data) throws FilloException
{
ArrayList<Object> excel_data = new ArrayList<Object>();
Fillo fillo = new Fillo();
Connection connection = fillo.getConnection("C:\\Users\\Vishrut\\Desktop\\read_openxl.xlsx");
String strQuery = query;
Recordset rs = connection.executeQuery(strQuery);
while (rs.next())
{
excel_data.add(rs.getField(field)+rs.getField(multi_data));
}
for (Object data: excel_data)
{
System.out.println(data);
}
rs.close();
connection.close();
}
这里是如何称呼的。 “department”后面可以带多个参数。
function_class.read_by_excel("select * from Sheet1 where employee_id = 60546", "department","salary");
最佳答案
我认为错误消息很清楚,getField(String)
方法不支持传递数组。
从您的代码中,multi_data 参数是一个数组
String ... multi_data
然后将其传递给 getField 方法
rs.getField(multi_data)
这是我的做法:更改方法签名
public static void read_by_excel(Integer emplyoee_id, String ...fields){
//...
}
然后替换
rs.getField(field) + rs.getField(multi_data)
由
Stream.of(fields).map(f -> rs.getField(f)).collect(Collectors.joining());
请注意,我假设 getField
返回 String
(fillo 没有公共(public) API 文档,因此无法验证)。请注意,确保您确实想做 rs.getField(field) + rs.getField(multi_data)
值之间没有分隔符。考虑使用joining(",")
或其他任何东西。
关于java - Recordset 不适用于参数 (String[]) -- 接受多个参数时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48461001/