未公开的声明让我发疯

时间:2014-05-13 14:16:08

标签: java prepared-statement jboss7.x

我有这段代码:

      Helper helper = getDBhelper();

      Connection conn = helper.getConnection();

      String sql = 
              " SELECT DISTINCT date as plannedDate" +
              " FROM plan ";

      String AssimilationTabDates = 
              " SELECT DISTINCT payment as plannedDate " +
              " FROM credit_mo_assimilation_tab ";

      String assimilationInfoDates = 
              " SELECT DISTINCT planned_pay as plannedDate " +
              " FROM assimilation ";


      String sqlUpdate = 
               " UPDATE plan " +
               " SET date = ? " +
               " WHERE planned_date = ? ";

      String updateAssimilationTab = 
              " UPDATE assimilation " +
              " SET payment = to_date(?, 'DD-MM-YYYY') " +
              " WHERE d_payment = to_date(?, 'DD-MM-YYYY') ";

      String updateAssimilationInfo = 
              " UPDATE assimilation " +
              " SET pay = to_date(?, 'DD-MM-YYYY') " +
              " WHERE planned_pay = ? ";



        PreparedStatement ps = null;
        ResultSet rs = null;
        Date plannedDate = null;
        List<String> plannedDates = new ArrayList<String>();
        List<String> plannedTabDates = new ArrayList<String>();
        List<String> plannedInfoDates = new ArrayList<String>();

      try {
            ps = conn.prepareStatement(sql);
            ps.execute();
            rs = ps.getResultSet();

            while(rs.next()) {
                  plannedDate = rs.getDate("plannedDate");  
                  String plannedD = new SimpleDateFormat("dd-MMM-yy").format(plannedDate);
                  plannedDates.add(plannedD);
            }

            ps = conn.prepareStatement(AssimilationTabDates);
            ps.execute();
            rs = ps.getResultSet();

            while(rs.next()) {
                  plannedDate = rs.getDate("plannedDate");  
                  String plannedD = null;
                  if(plannedDate != null){
                      plannedD = new SimpleDateFormat("dd-MMM-yy").format(plannedDate);
                  }
                  if(plannedD != null){
                      plannedTabDates.add(plannedD);
                  }

            }

            ps = conn.prepareStatement(assimilationInfoDates);
            ps.execute();
            rs = ps.getResultSet();

            while(rs.next()) {
                  plannedDate = rs.getDate("plannedDate");  
                  String plannedD = new SimpleDateFormat("dd-MMM-yy").format(plannedDate);
                  plannedInfoDates.add(plannedD);
            }

      } catch (Exception ex) {
          ex.printStackTrace();
      } 


      try{
          for( String oneItem : plannedDates ) {
              SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yy");
              Date datePlanned = formatter.parse(oneItem);
              Date paymentDate = datePlanned;

              Date paymentDateReal = paymentDate;

              if (paymentDate != null) {

                    paymentDateReal = DefaultProdCalendar.findNearestWorkingDay(paymentDate); 
            }

            String realDate = new SimpleDateFormat("dd-MMM-yy").format(paymentDateReal);



                ps = conn.prepareStatement(sqlUpdate);
                ps.setString(1, realDate);
                ps.setString(2, oneItem);
                ps.execute();
                rs = ps.getResultSet();
            }

          for( String oneItem : plannedTabDates ) {
              SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yy");
              Date datePlanned = formatter.parse(oneItem);
              Date paymentDate = datePlanned;

              Date paymentDateReal = paymentDate;

              if (paymentDate != null) {
                  paymentDateReal = DefaultProdCalendar.findNearestWorkingDay(paymentDate);
            }

            String realDate = new SimpleDateFormat("dd-MMM-yy").format(paymentDateReal);



                ps = conn.prepareStatement(updateAssimilationTab);
                ps.setString(1, realDate);
                ps.setString(2, oneItem);
                ps.execute();
                rs = ps.getResultSet();
            }

          for( String oneItem : plannedInfoDates ) {
              SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yy");
              Date datePlanned = formatter.parse(oneItem);
              Date paymentDate = datePlanned;

              Date paymentDateReal = paymentDate;

              if (paymentDate != null) {
                  paymentDateReal = DefaultProdCalendar.findNearestWorkingDay(paymentDate);
            }

            String realDate = new SimpleDateFormat("dd-MMM-yy").format(paymentDateReal);



                ps = conn.prepareStatement(updateAssimilationInfo);
                ps.setString(1, realDate);
                ps.setString(2, oneItem);
                ps.execute();
                rs = ps.getResultSet();
            }


      }catch(Exception e){
          e.printStackTrace();
      }



      try {
            ps = conn.prepareStatement(sql);
            ps.execute();
            rs = ps.getResultSet();

            while(rs.next()) {
                  plannedDate = rs.getDate("plannedDate");  
                  String plannedD = new SimpleDateFormat("dd-MMM-yy").format(plannedDate);
                  plannedDates.add(plannedD);
            }

            ps = conn.prepareStatement(AssimilationTabDates);
            ps.execute();
            rs = ps.getResultSet();

            while(rs.next()) {
                  plannedDate = rs.getDate("plannedDate");  
                  String plannedD = null;
                  if(plannedDate != null){
                      plannedD  = new SimpleDateFormat("dd-MMM-yy").format(plannedDate);
                  }

                  if(plannedD != null){
                      plannedTabDates.add(plannedD);
                  }
            }

      } catch (Exception ex) {
          ex.printStackTrace();
      } 


      try{
          for( String oneItem : plannedDates ) {
              SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yy");
              Date datePlanned = formatter.parse(oneItem);
              Date paymentDate = datePlanned;

              Date paymentDateReal = paymentDate;

              if (paymentDate != null) {
                  paymentDateReal = DefaultProdCalendar.findNearestWorkingDay(paymentDate);
            }

            String realDate = new SimpleDateFormat("dd-MMM-yy").format(paymentDateReal);



                ps = conn.prepareStatement(sqlUpdate);
                ps.setString(1, realDate);
                ps.setString(2, oneItem);
                ps.execute();
                rs = ps.getResultSet();
            }

          for( String oneItem : plannedTabDates ) {
              SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yy");
              Date datePlanned = formatter.parse(oneItem);
              Date paymentDate = datePlanned;

              Date paymentDateReal = paymentDate;

              if (paymentDate != null) {
                  paymentDateReal = DefaultProdCalendar.findNearestWorkingDay(paymentDate);
            }

            String realDate = new SimpleDateFormat("dd-MMM-yy").format(paymentDateReal);

                ps = conn.prepareStatement(updateAssimilationTab);
                ps.setString(1, realDate);
                ps.setString(2, oneItem);
                ps.execute();
                rs = ps.getResultSet();
            }


      }catch(Exception e){
          e.printStackTrace();
      }   
      finally {
          try {
            ps.close();
            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
      }

上面的代码运行得很好,但是有一个小问题。该语句没有被关闭,因此这推动我的jboss服务器执行此操作,并且几乎所有查询执行都会引发以下错误:

Closing a statement you left open, please do your own housekeeping: java.lang.Throwable: STACKTRACE

AREN&#39;吨

ps.close();
conn.close();

finally应该关闭所有内容吗?

我知道我错过了一些非常小而简单的东西,但我现在无法发现它。

3 个答案:

答案 0 :(得分:8)

您不断创建新的预准备语句并将其分配给ps,但您只能关闭最后一个。如果你需要执行一系列语句,可以随意关闭它们,也可以将它们放在单独的变量中,这样你就可以在最后关闭它们。

答案 1 :(得分:1)

如果我们同意一个方法应该做好一件事,我会说这是一个糟糕的实现。

每个SQL操作都应该有一个单独的方法。每个try/catch/finally应该在尽可能最窄的范围内创建和关闭资源(方法范围)。

如果您获得Connection,则无法拥有提交/回滚逻辑。我建议将它传递给你的SQL方法。让知道工作单元的服务或业务层获取Connection并提交或回滚。

我还说你不必要地重复了很多代码。我会重构制作这个DRY-er。

答案 2 :(得分:0)

除了chrylis所说的,你的异常处理是有缺陷的。当您捕获异常时,它不会传播到封闭的try/catch。所以你应该用这个替换你的catch子句:

  } catch (Exception ex) {
      ex.printStackTrace(); // I'd also replace this by something useful (ie. logging)
  } finally {
      ps.close();
  }