【发布时间】:2020-09-09 17:43:12
【问题描述】:
我正在尝试使用 select distinct 查询从我的 SQL 数据库中获取一个表,然后将其放入一个数组中。但是,当我尝试将数组保存为变量时出现错误。
代码:
private ResultSet query(String query) throws SQLException {
assert testConn();
try {
Statement statement = connection.createStatement();
return statement.executeQuery(query);
} catch (SQLException e) {
e.printStackTrace();
}
throw new SQLException();
}
private boolean testConn(){
try {
return connection.isValid(1);
} catch (SQLException e){
return false;
}
}
@Test
void test_rs(){
try {
ResultSet rs = query("select distinct client_id from email_filtering_scores;");
Array a = rs.getArray("client_id");
String[] set = (String[])a.getArray();
System.out.println(Arrays.deepToString(set));
} catch (SQLException e) {
e.printStackTrace();
}
}
我不知道为什么会抛出 SQLException,因此感谢您的帮助。
提前致谢。
附言。我不知道使用 jdbc 的最佳实践是什么,所以如果我的代码有什么令人震惊的地方,我会喜欢一些建议。
【问题讨论】:
-
我最初尝试修复它是基于此回复here
-
client_id的数据类型是什么?您也可以将Object用于未知类型