您可以返回 aList<String>
或使用 char 将多个字符串分隔在一个String
. IMO最好返回一个List<String>
:
public static List<String> getResult(String mycondition) {
List<String> results = new ArrayList<String>();
ResultSet rsData = sql.RunSelect("select col1 from my_table where con='"
+myCondition+"'");
while (rsData.next()) {
results.add(rsData.getString("col1"));
}
return results;
}
此外,这种方法容易发生SQL 注入。请注意,您的参数应与查询分开发送。可能你可以改进你的sql.RunSelect
方法来使用PreparedStatement
而不是Statement
. 这是代码骨架的基本示例:
public ResultSet runSelect(String query, Object ... params) {
//assumes you already have your Connection
PreparedStatement pstmt = con.prepareStatement(query);
int i = 1;
for(Object param : params) {
pstmt.setObject(i++, param);
}
return pstmt.executeQuery();
}
所以现在你可以修改你的方法
public static List<String> getResult(String mycondition) {
List<String> results = new ArrayList<String>();
//using the new runSelect method
ResultSet rsData = sql.runSelect(
"select col1 from my_table where con=?", mycondition);
while (rsData.next()) {
results.add(rsData.getString("col1"));
}
return results;
}