简体   繁体   English

SQL Server-查询返回数据,但使用相同查询的ResultSet返回空

[英]SQL Server - a query returns data but ResultSet using same query returns empty

Can anyone think of a reason why this piece of code, looped (exactly 12 times) never returns any data, while the same statement entered in SQL Server SMS, gives me exactly what I'm looking for? 谁能想到一个原因,为什么这段循环代码(恰好12次)从不返回任何数据,而在SQL Server SMS中输入的同一条语句却恰恰给了我所需的信息? When debugging, the rset.next() returns false and never enters the loop. 调试时,rset.next()返回false,并且永远不会进入循环。

Connection connection = null;
PreparedStatement statement = null;
ResultSet rset = null;
ArrayList<PriceList> priceLists = new ArrayList<PriceList>();
String sqlStatement = "";
int i = 1;
try {
    connection = pool.getConnection();
    sqlStatement = "SELECT DISTINCT PriceList.ExRateAbbr FROM PriceList WHERE PLName Like ?;";
    statement = connection.prepareStatement(sqlStatement);
    statement.setString(i++, pricelistName);
    rset = statement.executeQuery();
    while (rset.next()) {
            PriceList pList = new PriceList();
            pList.setCurrency(rset.getString("ExRateAbbr"));
            priceLists.add(pList);
    }
    statement.close();
} catch (SQLException sqle) {
    sqle.printStackTrace();
} finally {
if (pool != null) {
    try {
        pool.releaseConnection(connection);
    } catch (SQLException sqle) {
        sqle.printStackTrace();
    }
}

This worked when I was using Access as a database, but after moving whole db to SQL Server there's nothing. 当我将Access用作数据库时,此方法有效,但是将整个数据库移至SQL Server之后,什么也没有。 And no exceptions thrown. 并且没有抛出异常。 I'm at a loss and all I can think of is that there is some SQL Server limitations for opening and closing connections very quickly. 我茫然不知所措,我能想到的是,SQL Server的某些限制非常快地打开和关闭连接。

Changing my query to: 将我的查询更改为:

SELECT DISTINCT PriceList.ExRateAbbr FROM PriceList WHERE PLName=?;

solved it. 解决了。 But why would the query using LIKE not work ? 但是,为什么使用LIKE进行查询不起作用? Strange enough it worked when it was executed in SQL Server SMS, so what's the deal here? 当它在SQL Server SMS中执行时,它的工作效果就足够奇怪了,那么这有什么用呢?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM