我有一个用 Java 编写的数据库桌面客户端。我需要从 postgres 检索数据,无论连接如何(我的意思是数据库用户角色),无论主机如何(我尝试在不同计算机上启动代码并在不同容器上运行数据库)始终在一个特定表上选择查询在 ResultSet 中返回 null。
这是表格:
Table "public.client"
Column | Type | Collation | Nullable | Default
--------------------------+-------------------+-----------+----------+---------
contact_phone | character(11) | | not null |
status | character(11) | | not null |
money_spent | real | | not null |
bonus | real | | not null |
client_name | character varying | | not null |
assigned_employee_number | character(11) | | |
email | character varying | | |
以下是 Java 查询此表的示例:
List<Client> resultClientList = new ArrayList<>();
try(PreparedStatement stmt = connection.prepareStatement("SELECT " +
"contact_phone," +
"status, " +
"money_spent, " +
"bonus, " +
"client_name, " +
"assigned_employee_number, " +
"email " +
"FROM client " +
"WHERE assigned_employee_number IS NULL;")){
resultClientList = executeClientQuery(stmt.getResultSet());
}
catch (SQLException e){
log.error("Error querying clients: " + e.getMessage());
}
以及从结果集中提取数据的方法:
private List<Client> executeClientQuery(ResultSet resultSet){
List<Client> resultClientList = new ArrayList<>();
if(resultSet == null) return resultClientList;
try{
while(resultSet.next()){
String contactPhone = resultSet.getString("contact_phone");
String status = resultSet.getString("status");
Double moneySpent = resultSet.getDouble("money_spent");
Double bonus = resultSet.getDouble("bonus");
String clientName = resultSet.getString("client_name");
String assignedEmployeeNumber = resultSet.getString("assigned_employee_number");
resultClientList.add(new Client(contactPhone, status, moneySpent, bonus, clientName, assignedEmployeeNumber));
}
}
catch (SQLException e){
log.error("Error querying clients: " + e.getMessage());
}
return resultClientList;
}
此时,方法
executeClientQuery
在 ResultSet 中接收到 null。我尝试查询SELECT * FROM client
,但这没有帮助。我尝试调试 JDBC 代码,但没有发现任何结果。没有抛出异常,只是 ResultSet 为 null。但是,在数据库中运行此查询会返回正确的数据。
连接处于活动状态。任何其他表上的任何其他查询都会返回正确的结果集。