我正在编写简单的Java代码,我必须在Postgresql数据库上执行SQL查询。 这个数据库的结果(我手动检查)是一个由一列和两个整数组成的简单表,如下所示:
我的问题是我找不到任何方法来阅读这两行。 我的阅读代码是:
_conn.setReadOnly(假);
_stmt = _conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
_stmt.setFetchSize(100);
列表与LT;简短> lista = new List<简短>();
_rs = _stmt.executeQuery(query);
while(_rs.next()){
lista.add((短)_rs.getInt(1));
}
此代码仅读取第1行,但无法将其读取到第2行。 短名单“lista”总是包含1个元素。 就好像ResultSet没有继续读取第二行......
如何阅读这两行?
编辑:我尝试过使用ArrayList,但它不起作用。我已经为_conn连接和_stmt语句添加了代码部分。 这是查询:
SELECT COUNT(bffK.biological_function_feature_oid)
FROM biological_function_feature AS bff
INNER JOIN biological_function_feature_unfolded AS bffu
ON bff.biological_function_feature_oid = bffu.term_oid
INNER JOIN ( SELECT DISTINCT ON (g.gene_oid, bffu.ancestor_oid) g.gene_oid AS ance_gene, bffu.ancestor_oid AS ance_oid, mf.feature_name AS feature_name, mf.feature_id AS feature_id, ef.name AS evidence, qf.name AS qualifier
FROM gene2biological_function_feature AS g2bff
INNER JOIN biological_function_feature AS bff
ON g2bff.biological_function_feature_oid = bff.biological_function_feature_oid
INNER JOIN biological_function_feature_unfolded AS bffu
ON bffu.term_oid = bff.biological_function_feature_oid
INNER JOIN metadata.feature AS mf ON bff.feature_type = mf.feature_id
INNER JOIN gene AS g ON g2bff.gene_oid = g.gene_oid
LEFT JOIN flag.evidence_flags AS ef ON g2bff.evidence = ef.id
LEFT JOIN flag.qualifier_flags AS qf ON g2bff.qualifier = qf.id
LEFT JOIN flag.inferred_flags AS fif ON fif.id = g2bff.inferred
WHERE g.gene_oid=(4060969)
AND (bff.is_obsolete NOT IN ('t') OR bff.is_obsolete IS NULL)
AND (g.is_obsolete NOT IN ('t') OR g.is_obsolete IS NULL)
AND distance=1
UNION
SELECT DISTINCT ON (g.gene_oid, bff.biological_function_feature_oid)
g.gene_oid AS ance_gene, bff.biological_function_feature_oid AS ance_oid,
mf.feature_name AS feature_name, mf.feature_id AS feature_id,
ef.name AS evidence,
qf.name AS qualifier
FROM gene2biological_function_feature AS g2bff
INNER JOIN biological_function_feature AS bff
ON g2bff.biological_function_feature_oid = bff.biological_function_feature_oid
INNER JOIN metadata.feature AS mf ON bff.feature_type = mf.feature_id
INNER JOIN gene AS g ON g2bff.gene_oid = g.gene_oid
LEFT JOIN flag.evidence_flags AS ef ON g2bff.evidence = ef.id
LEFT JOIN flag.qualifier_flags AS qf ON g2bff.qualifier = qf.id
LEFT JOIN flag.inferred_flags AS fif ON fif.id = g2bff.inferred
WHERE g.gene_oid=(4060969)
AND (bff.is_obsolete NOT IN ('t') OR bff.is_obsolete IS NULL)
AND (g.is_obsolete NOT IN ('t') OR g.is_obsolete IS NULL)
AND ef.name NOT IN ('IEA', 'ND')
)
AS unf ON unf.ance_oid = bffu.ancestor_oid
INNER JOIN gene AS gK on gk.Gene_oid=ance_gene
INNER JOIN biological_function_feature AS bffK
ON bffK.biological_function_feature_oid = bffu.ancestor_oid
LEFT JOIN flag.evidence_flags AS efK
ON evidence = efK.name
LEFT JOIN flag.qualifier_flags AS qfK
ON qualifier = qfK.name
WHERE bffu.distance=1 AND (bff.biological_function_feature_oid) IN (217538)
AND evidence NOT IN ('IEA', 'ND')
UNION
SELECT COUNT(bffK.biological_function_feature_oid)
FROM biological_function_feature AS bff
INNER JOIN biological_function_feature_unfolded AS bffu
ON bff.biological_function_feature_oid = bffu.term_oid
INNER JOIN ( SELECT DISTINCT ON (g.gene_oid, bffu.ancestor_oid) g.gene_oid AS ance_gene, bffu.ancestor_oid AS ance_oid, mf.feature_name AS feature_name, mf.feature_id AS feature_id, ef.name AS evidence, qf.name AS qualifier
FROM gene2biological_function_feature AS g2bff
INNER JOIN biological_function_feature AS bff
ON g2bff.biological_function_feature_oid = bff.biological_function_feature_oid
INNER JOIN biological_function_feature_unfolded AS bffu
ON bffu.term_oid = bff.biological_function_feature_oid
INNER JOIN metadata.feature AS mf ON bff.feature_type = mf.feature_id
INNER JOIN gene AS g ON g2bff.gene_oid = g.gene_oid
LEFT JOIN flag.evidence_flags AS ef ON g2bff.evidence = ef.id
LEFT JOIN flag.qualifier_flags AS qf ON g2bff.qualifier = qf.id
LEFT JOIN flag.inferred_flags AS fif ON fif.id = g2bff.inferred
WHERE g.gene_oid=(4060969)
AND (bff.is_obsolete NOT IN ('t') OR bff.is_obsolete IS NULL)
AND (g.is_obsolete NOT IN ('t') OR g.is_obsolete IS NULL)
AND distance=1
UNION
SELECT DISTINCT ON (g.gene_oid, bff.biological_function_feature_oid)
g.gene_oid AS ance_gene, bff.biological_function_feature_oid AS ance_oid,
mf.feature_name AS feature_name, mf.feature_id AS feature_id,
ef.name AS evidence,
qf.name AS qualifier
FROM gene2biological_function_feature AS g2bff
INNER JOIN biological_function_feature AS bff
ON g2bff.biological_function_feature_oid = bff.biological_function_feature_oid
INNER JOIN metadata.feature AS mf ON bff.feature_type = mf.feature_id
INNER JOIN gene AS g ON g2bff.gene_oid = g.gene_oid
LEFT JOIN flag.evidence_flags AS ef ON g2bff.evidence = ef.id
LEFT JOIN flag.qualifier_flags AS qf ON g2bff.qualifier = qf.id
LEFT JOIN flag.inferred_flags AS fif ON fif.id = g2bff.inferred
WHERE g.gene_oid=(4060969)
AND (bff.is_obsolete NOT IN ('t') OR bff.is_obsolete IS NULL)
AND (g.is_obsolete NOT IN ('t') OR g.is_obsolete IS NULL)
AND ef.name IN ('IEA', 'ND')
)
AS unf ON unf.ance_oid = bffu.ancestor_oid
INNER JOIN gene AS gK on gk.Gene_oid=ance_gene
INNER JOIN biological_function_feature AS bffK
ON bffK.biological_function_feature_oid = bffu.ancestor_oid
LEFT JOIN flag.evidence_flags AS efK
ON evidence = efK.name
LEFT JOIN flag.qualifier_flags AS qfK
ON qualifier = qfK.name
WHERE bffu.distance=1 AND (bff.biological_function_feature_oid) IN (217538)
AND evidence IN ('IEA', 'ND')
答案 0 :(得分:1)
List是一个接口,尝试使用像ArrayList
这样的东西List< Short > lista = new ArrayList< Short >();
_rs = _stmt.executeQuery(query);
while (_rs.next()) {
lista.add((short)_rs.getShort(1));
}
答案 1 :(得分:1)
List是一个接口,因此您不能直接实例化而不是使用像ArrayList这样的实现。另请注意,count是PG中的关键字,因此请避免使用关键字作为标识符。工作解决方案:
pst = connection.prepareStatement("SELECT * FROM demo");
List<Short> lista = new ArrayList<Short>();
rs = pst.executeQuery();
while (rs.next()) {
lista.add((short)rs.getInt(1));
}
答案 2 :(得分:0)
它似乎不是语法问题。在针对数据库运行查询时,您是否也获得了一行?也许你也可以发布你的SQL查询。