5

我在 Eclipse 上使用 PostgreSQL 9.1 和 Tomcat 7。连接是通过连接池建立的。当我使用 的setInt()方法时PreparedStatement,没有返回任何内容。打印准备好的语句时,'?' 被一些奇怪的字符串取代,我想是对对象的引用。当我使用setObject()适当的方法时Type.INTEGER,打印的语句包含正确的值,但作为字符串,并且强制需要 Int。

这是代码:

public User_DTO[] select(User_DTO params, int order, int limit, int offset)
{
    User_DTO result = null;
    ArrayList<User_DTO> result_arr = new ArrayList<User_DTO>(0);

    String query = "SELECT id, name, role FROM Usuario WHERE name LIKE ? AND role LIKE ? AND id != ? ORDER BY ? LIMIT ? OFFSET ?";

    String name = params.getName();
    String role = params.getRole();
    String id = params.getId();

    try
    {
        conn = Pool.getConnection();
        stmt = conn.prepareStatement(query);
    
        stmt.setString(1, "%" + name + "%");
        stmt.setString(2, "%" + role + "%");
        stmt.setString(3, id);

                // Using setInt()
                // stmt.setInt(4, order);

                // Using setObject
        stmt.setObject(4, order, Types.INTEGER);
        System.out.println(order);
    
        if(limit == -1)
        {
            stmt.setNull(5, Types.NULL);
        }
        else
        {
            stmt.setInt(5, limit);
        }
    
        stmt.setInt(6, offset);

        //
        System.out.println(stmt);
    
        rs = stmt.executeQuery();
    
        while(rs.next())
        {
            result = new User_DTO();
        
            result.setId(rs.getString("id"));
            result.setName(rs.getString("name"));
            result.setRole(rs.getString("role"));
        
            result_arr.add(result);
        }
    }
    catch(SQLException sql_e)
    {
        sql_e.printStackTrace();
    }
    finally
    {
        Pool.close(conn, stmt, rs);
    }

    return (result != null) ? result_arr.toArray(new User_DTO[result_arr.size()]) : null;
}

使用的打印语句setInt()是这样的:

SELECT id, name, role FROM Usuario WHERE name LIKE '%%' AND role LIKE '%%' AND id != '13253cc9' ORDER BY '[B@65adcbab' LIMIT '[B@75167bb3' OFFSET '[B@171360d3'

并且使用打印的那个setObject()

SELECT id, name, role FROM Usuario WHERE name LIKE '%%' AND role LIKE '%%' AND id != '13253cc9' ORDER BY '1' LIMIT '[B@75167bb3' OFFSET '[B@171360d3'

知道为什么会发生这种情况吗?

4

2 回答 2

1

对此有两个快速的想法。

  1. 您看到的字符串是转换为字符串的 Integer 对象。
  2. 您为什么要按整数对结果进行排序?

我认为您的参数数量错误。您必须按查询中包含的表的字段进行排序,因此您不应该将第 4 个参数作为整数给出。事实上,您可能应该有一个固定的顺序。

于 2012-11-09T23:32:04.113 回答
-1
public User_DTO[] select(User_DTO params, int order, int limit, int offset)
{
    User_DTO result = null;
    ArrayList<User_DTO> result_arr = new ArrayList<User_DTO>(0);
    
    StringBuffer queryBuffer = new StringBuffer("SELECT id, name, role FROM Usuario WHERE name LIKE ");
    queryBuffer.append("%");
    queryBuffer.append(params.getName());
    queryBuffer.append("%");
    queryBuffer.append(" AND role LIKE ");
    queryBuffer.append("%");
    queryBuffer.append(params.getRole());
    queryBuffer.append("%");
    queryBuffer.append(" AND id != ");
    queryBuffer.append(params.getId());
    queryBuffer.append(" ORDER BY ");
    queryBuffer.append(order);

    if (limit != -1)
    {
        queryBuffer.append(" LIMIT ");
        queryBuffer.append(limit);
    }

    queryBuffer.append(" OFFSET ");
    queryBuffer.append(offset);
    
    try
    {
        conn = Pool.getConnection();
        stmt = conn.prepareStatement(queryBuffer.toString());

        rs = stmt.executeQuery();

        while (rs.next())
        {
            result = new User_DTO();

            result.setId(rs.getString("id"));
            result.setName(rs.getString("name"));
            result.setRole(rs.getString("role"));

            result_arr.add(result);
        }
    }
    catch(SQLException sql_e)
    {
        sql_e.printStackTrace();
    }
    finally
    {
        Pool.close(conn, stmt, rs);
    }

    return (result != null) ? result_arr.toArray(new User_DTO[result_arr.size()]) : null;
}

使用StringBuffer代替setIntorsetString

于 2012-12-11T13:37:52.323 回答