为什么我的结果集会显示为假?

时间:2019-04-24 06:09:27

标签: java prepared-statement resultset

我正在为我的编程课设计一个项目,老师鼓励使用stackoverflow寻求帮助。我的问题如下:在我的if sResult.next()中返回false。我正在尝试从我的derby数据库中获取一行。

我已验证我的productUuid正在获取UUID,preparedstatement处于活动状态。我检查了查询的findAll是否正常工作。

拦截器应该返回正在使用的类名。

我的查询:

private static final String QUERY_02 =
            "SELECT oid, product_name, product_desc, product_price, product_sku, product_inv FROM PRODUCT WHERE oid=?";

我的UUIDUtils:

public class UUIDUtils {

    public static UUID asUuid(byte[] bytes) {
        final ByteBuffer bb = ByteBuffer.wrap(bytes);
        final long firstLong = bb.getLong();
        final long secondLong = bb.getLong();

        return new UUID(firstLong, secondLong);
    }

    public static byte[] asBytes(UUID uuid) {
        final ByteBuffer bb = ByteBuffer.wrap(new byte[16]);
        bb.putLong(uuid.getMostSignificantBits());
        bb.putLong(uuid.getLeastSignificantBits());

        return bb.array();
    }

我的单件商品:

public ProductDTO singleProduct(UUID productUuid) throws MyStoreException {
        log().trace("Entered singleProduct()");
         ProductDTO sProduct = null;


          try (final Connection connection = datasource.getConnection();
                     final PreparedStatement sPre = connection.prepareStatement(QUERY_02);) {  


              sPre.setBytes(1, UUIDUtils.asBytes(productUuid));
                  log().trace("PreparedStatment connection is active: {}", !sPre.isClosed());
              log().trace("productUuid passed through ProductQueryImpl: {}" , productUuid);
              ResultSet sResult = sPre.executeQuery();

              if(sResult.next()) { 

             sProduct = new ProductDTO(productUuid,
                        sResult.getString("product_name"),
                        sResult.getString("product_desc"),
                        sResult.getDouble("product_price"),
                        sResult.getString("product_sku"),
                        sResult.getInt("product_inv"));


                }else {
                    log().error("Product not found");
                }
                return sProduct;


            }catch(final SQLException ex2) {
            log().error(ex2.getMessage(), ex2);
            throw new MyStoreException(ex2.getMessage());
            }




          }

我的控制台:

00:05:04,092 TRACE [ca.sait.mystore.mvc.model.ProductModel] (default task-1) Entered postConstruct()
00:05:04,094 TRACE [ca.sait.mystore.interceptors.LogInterceptors] (default task-1) Entered logInterceptor(context)
00:05:04,094 DEBUG [ca.sait.mystore.interceptors.LogInterceptors] (default task-1) Method Name: singleProduct
00:05:04,094 TRACE [ca.sait.mystore.dao.ProductQueryImpl] (default task-1) Entered singleProduct()
00:05:04,096 TRACE [ca.sait.mystore.dao.ProductQueryImpl] (default task-1) PreparedStatment connection is active: true
00:05:04,096 TRACE [ca.sait.mystore.dao.ProductQueryImpl] (default task-1) productUuid passed through ProductQueryImpl: 00b6d373-af18-54ed-f8c9-7f9d986a834a
00:05:04,097 ERROR [ca.sait.mystore.dao.ProductQueryImpl] (default task-1) Product not found
00:05:04,098 DEBUG [ca.sait.mystore.interceptors.LogInterceptors] (default task-1) Returning Null
00:05:04,098 TRACE [ca.sait.mystore.interceptors.LogInterceptors] (default task-1) Exited logInterceptor(context)
00:05:04,099 TRACE [ca.sait.mystore.mvc.model.ProductModel] (default task-1) Following UUID passed through Model:  00b6d373-af18-54ed-f8c9-7f9d986a834a
00:05:04,099 TRACE [ca.sait.mystore.mvc.model.ProductModel] (default task-1) Exited postConstruct()

0 个答案:

没有答案