查询以提取行,即使DB具有匹配的记录,其中列为null的列始终返回空

时间:2019-06-18 11:53:02

标签: spring postgresql jdbctemplate

尽管DB中有匹配的记录,但以下代码始终返回空行:

String sql = "select * from test5 where id2 = ? ";
    return opsbldJdbcTemplate.query(sql, new PreparedStatementSetter() {
        @Override
        public void setValues(PreparedStatement ps) throws SQLException {
            ps.setNull(1, java.sql.Types.BIGINT);
        }
    }, new ResultSetExtractor<List<Map<String,Object>>>() {
        public List<Map<String,Object>> extractData(ResultSet rs) throws SQLException, DataAccessException {
            List<Map<String,Object>> lms = new ArrayList<>();
            while(rs.next()) {
                Map<String,Object> mss = new HashMap<>();
                lms.add(mss);
                mss.put("id1", "" + rs.getInt(1));
                mss.put("id2", "" + rs.getLong(2));
                mss.put("id4", "" + rs.getDouble(4));
            }

            return lms;
        }
     });
}       
ppql=> \d test5
                      Table "public.test5"
 Column |         Type          | Collation | Nullable | Default
--------+-----------------------+-----------+----------+---------
 id1    | integer               |           |          |
 id2    | bigint                |           |          |
 id3    | smallint              |           |          |
 id4    | double precision      |           |          |
 id5    | numeric(10,2)         |           |          |
 id6    | real                  |           |          |
 id7    | character varying(30) |           |          |
 id8    | text                  |           |          |
 id9    | character(30)         |           |          |


ppql=> select * from test5 where id2 is NULL;
 id1 | id2 | id3 | id4 | id5 | id6 | id7 | id8 | id9
-----+-----+-----+-----+-----+-----+-----+-----+-----
   4 |     |     |     |     |     |     |     |
(1 row)

不确定为什么它不返回任何行。.已经进行了各种尝试来实现它,但是无济于事

我尝试了查询函数和queryforList等的许多变体。但是没有帮助

1 个答案:

答案 0 :(得分:0)

不能使用return new Promise( function (resolve) { // self.viewer.model.getBulkProperties goes here // resolve(data) goes *inside the callback* }) 进行null检查,而必须这样进行:

= null

如果尝试通过设置空值来做到这一点,则通过

select * from test5 where id2 is null

它提供了JDBC驱动程序的异常:

ps.setNull(1, java.sql.Types.BIGINT);

在这种情况下,一种可能的解决方案是进行特定查询:

org.postgresql.util.PSQLException: ERROR: syntax error at or near "$1"
  Position: 34
    at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2440)
    at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:2183)
    at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:308)
    at org.postgresql.jdbc.PgStatement.executeInternal(PgStatement.java:441)
    at org.postgresql.jdbc.PgStatement.execute(PgStatement.java:365)
    at org.postgresql.jdbc.PgPreparedStatement.executeWithFlags(PgPreparedStatement.java:143)
    at org.postgresql.jdbc.PgPreparedStatement.executeQuery(PgPreparedStatement.java:106)

测试

这里有一个包含JDBC的自包含Java程序:

select * from test5 where id2 is null

如果test5表的内容如下所示:

import java.util.Properties;
import java.sql.*;

public class Main {

    public static void main(String[] args) {
        try {
            Connection connection = createConnection();
            try (connection) {
                /*
                PreparedStatement ps = connection.prepareStatement(
                        "select * from test5 where id2 is ?");
                ps.setNull(1, java.sql.Types.BIGINT);
                 */
                PreparedStatement ps = connection.prepareStatement(
                        "select * from test5 where id2 is null");

                try (ResultSet rs = ps.executeQuery()) {
                    while(rs.next()) {
                        int id1 = rs.getInt(1);
                        System.out.println("found entry with id1='" + id1 + "'");
                    }
                }
                ps.close();
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    private static Connection createConnection()
            throws ClassNotFoundException, SQLException {
        Class.forName("org.postgresql.Driver");
        String url = "jdbc:postgresql://localhost/stephan";
        Properties props = new Properties();
        props.setProperty("user", "stephan");
        props.setProperty("password", "secret");
        return DriverManager.getConnection(url, props);
    }
}

然后调试控制台上的输出如下:

stephan=# select * from test5;
 id1 | id2 | id3 | id4 | id5 | id6 | id7 | id8 | id9 
-----+-----+-----+-----+-----+-----+-----+-----+-----
   4 |     |     |     |     |     |     |     | 
(1 row)