postgresql 特定表上的ResultSet为null

epggiuax  于 2023-10-18  发布在  PostgreSQL
关注(0)|答案(1)|浏览(102)

我有一个用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。但是,在数据库中运行此查询将返回正确的数据。
连接还活着。对任何其他表的任何其他查询都将返回正确的ResultSet。

k3bvogb1

k3bvogb11#

请参阅PreparedStatement::getResultSet的JavaDoc.
你想调用的是PreparedStatement::executeQuery

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.executeQuery() );
}
catch( final SQLException e )
{
  log.error( "Error querying clients", e );
}

为什么你要使用一个准备好的语句,当你没有变量的时候?好吧,坦白说,有些RDBMS甚至优化了这些语句,但对于固定值查询

final var statement = connection.createStatement();
final var resultSet = statement.executeQuery( theQueryString );

是更常见的选择,它也适合这里:

final var query =
  """
  SELECT contact_phone,
         status, 
         money_spent,
         bonus,
         client_name,
         assigned_employee_number,
         email 
    FROM client 
    WHERE assigned_employee_number IS NULL;""";
List<Client> resultClientList = new ArrayList<>();
try( final var stmt = connection.createStatement() ) 
{
  resultClientList = executeClientQuery( stmt.executeQuery( query ) );
}
catch( final SQLException e )
{
  log.error( "Error querying clients", e );
}

相关问题