PreparedStatement的setInt()不适用于PostgreSQL

时间:2012-11-09 20:53:15

标签: java postgresql prepared-statement

我在Eclipse上使用PostgreSQL 9.1和Tomcat 7。连接是通过连接池进行的。当我为PreparedStatement使用setInt()方法时,不返回任何内容。打印PreparedStatement时,'?'被一些奇怪的String取代我想是对象的引用。当我使用带有相应Type.INTEGER的方法setObject()时,打印的语句包含正确的值,但是作为String,并强制要求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'

知道为什么会这样吗?

提前致谢!

2 个答案:

答案 0 :(得分:1)

对此有两个快速的想法。

  1. 您看到的字符串是转换为字符串的Integer对象。
  2. 您为什么要尝试按整数排序?
  3. 我认为您的参数数量有误。您必须按查询中包含的表的字段进行排序,因此您不应将第4个参数作为整数给出。实际上,您应该有一个固定的顺序。

答案 1 :(得分:0)

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 querryBuffer = new StringBuffer("SELECT id, name, role FROM Usuario WHERE name LIKE ");
    querryBuffer.append("%");
    querryBuffer.append(params.getName());
    querryBuffer.append("%");
    querryBuffer.append(" AND role LIKE ");
    querryBuffer.append("%");
    querryBuffer.append(params.getRole());
    querryBuffer.append("%");
    querryBuffer.append(" AND id != ");
    querryBuffer.append(params.getId());
    querryBuffer.append(" ORDER BY ");
    querryBuffer.append(order);

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

    querryBuffer.append(" OFFSET ");
    querryBuffer.append(offset);

    try
    {
        conn = Pool.getConnection();
        stmt = conn.prepareStatement(querryBuffer.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而不是setINt或setString