Tuesday, January 26, 2016

memo


  1. Class with Static functions:
    If with high calling frequency, we need to refactor to non-static function, parse with different object of the Class.
  2. Path Separator VS separator
    File.pathSeparator: separate file path in PATH environment vairable, ";"
    File.separator: "/" or "\" to split up the path of a file
  3. Close the resource with the block
    Example: SQL Clean way to close ResultSet
    Use CachedRowSet
  4.  
     public static ResultSet executeQuery(String sql, String[] parameters){
      DataSource ds = DBConnector.setupDataSource();
      try (
       Connection conn = ds.getConnection();
       PreparedStatement ps = createPreparedStatement(conn,sql,parameters);
       ResultSet rs = ps.executeQuery();
      ) {
       CachedRowSet rowset = new CachedRowSetImpl();
       rowset.populate(rs);
       return rowset;
      } catch (SQLException e) {
       Logger lgr = Logger.getLogger(SqlHelper.class);
       lgr.log(Level.WARNING,e.getMessage(),e);
      }
      return null;
     }
sdf

No comments:

Post a Comment