尽管我一次只有一个

时间:2016-07-08 23:21:49

标签: java jdbc resultset

我知道此问题之前有很多问题,但在这种情况下,我无法找到答案。

这是我的代码:

private Collection<Coupon> getCouponsMain(Company company, String filters) throws DAOException
{
    String sql = null;
    if (filters != null)
    {
        sql = "SELECT couponsystem.coupon.* FROM couponsystem.company_coupon LEFT JOIN couponsystem.coupon ON "
            + "couponsystem.company_coupon.COUPON_ID = couponsystem.coupon.ID WHERE couponsystem.company_coupon.COMP_ID = ? AND ?";
    }
    else
    {       
         sql = "SELECT couponsystem.coupon.* FROM couponsystem.company_coupon LEFT JOIN couponsystem.coupon ON "
            + "couponsystem.company_coupon.COUPON_ID = couponsystem.coupon.ID WHERE couponsystem.company_coupon.COMP_ID = ?";           
    }

    try (Connection con = pool.OpenConnection(); PreparedStatement preparedStatement = con.prepareStatement(sql);)
    {   
        // query command

        preparedStatement.setLong(1, company.getId());
        if (filters != null)
        {
            preparedStatement.setString(2, filters);
        }

        ResultSet rs = preparedStatement.executeQuery();

        if (rs.next())
        {
            CouponDBDAO couponDao = new CouponDBDAO();  
            rs.previous();
            return couponDao.BuildCoupons(rs);
        }
        else
        {
            return null;
        }
    }
    catch (SQLException | NullPointerException e)
    {
        throw new DAOException("Failed to retrieve data for all coupons" + e.getMessage());
    }
}

我认为查询本身不是重要的问题,但是,一旦我使用next()作为ResultSet,我就会收到错误:

  

java.sql.SQLException:ResultSet关闭&#34;

后不允许操作

这通常发生在同一个语句中使用两个rs时,这次不是这种情况。

由于以前的方法和BuildCoupons(rs)问题存在许多问题,但由于同样的原因,此部分也无法正常工作:

@Override
public Company getCompany(long id) throws DAOException
{
    String sql = "SELECT * FROM couponsystem.company WHERE ID = ?";
    try (Connection con = pool.OpenConnection(); PreparedStatement preparedStatement = con.prepareStatement(sql);)
    {   
        // query command

        preparedStatement.setLong(1, id);

        // query execution
        ResultSet rs = preparedStatement.executeQuery();

        Company comp = new Company();
        if (rs.next())
        {
            //Fill customer object from Customer table              
            comp.setId(rs.getLong("ID"));
            comp.setCompName(rs.getString("COMP_NAME"));
            comp.setPassword(rs.getString("PASSWORD"));
            comp.setEmail(rs.getString("EMAIL"));
            comp.setCoupons(comp.getCoupons());
        }   
        else
        {
            comp = null;
        }

        return comp;
    }
    catch (SQLException e)
    {
        throw new DAOException("Failed to retrieve data for customer id: " + id);
    }
}
BTW - 使用MySQL并且插入,更新和删除查询都正常工作,因此没有与db连接的问题

另一个更新 - 一旦我将其替换为常规声明,它就会起作用,但当然我会失去预备声明的所有优点

就像我说我创建新代码以隔离大程序 这是代码:

public class testState 
{
    public static void main(String[] args) throws SQLException
    {
            DBDAO pool = DBDAO.getInstance();

            String sql = "SELECT ID FROM couponsystem.company WHERE COMP_NAME = ? AND PASSWORD = ?";
            String compName = "t";
            String password = "t";

            pool.CreatePool();
            Connection con = pool.OpenConnection(); 
            PreparedStatement preparedStatement = con.prepareStatement(sql);

                preparedStatement.setString(1, compName);
                preparedStatement.setString(2, password);
preparedStatement.executeQuery();
                ResultSet rs = preparedStatement.executeQuery();

                System.out.println("rs status: " + rs.isClosed());
                if (rs.next())
                {
                    System.out.println("log-in was successfuly performed");
                    System.out.println(rs.getLong(1));
                    System.out.println("hjhjh");
                }
                else
                {
                    System.out.println("-1");
                }

            rs.close();
            preparedStatement.close();
            con.close();
            pool.CloseConnection();
    }
}

2 个答案:

答案 0 :(得分:0)

问题解决了, 这就是问题:

sql =&#34; SELECT couponsystem.coupon。* FROM couponsystem.company_coupon LEFT JOIN couponsystem.coupon ON&#34;             +&#34; couponsystem.company_coupon.COUPON_ID = couponsystem.coupon.ID WHERE couponsystem.company_coupon.COMP_ID =?和?&#34 ;;

第二个?是非法的,但例外是ResultSet关闭而不是查询问题

答案 1 :(得分:-2)

问题是你试图回到结果集中的前一个记录,这是不可能的。

了解可滚动的结果集并使其不敏感,一旦使用它,您可以使用rs.previous()返回到上一条记录

ResultSet.TYPE_SCROLL_INSENSITIVE