⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 articledaoimpl.java

📁 blog 系 统 (基 于 java )
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
			conn = isConnSupplied ? userConn : ResourceManager.getConnection();
		
			System.out.println( "Executing " + SQL_DELETE + " with PK: " + pk );
			stmt = conn.prepareStatement( SQL_DELETE );
			stmt.setInt( 1, pk.getArticleID() );
			int rows = stmt.executeUpdate();
			long t2 = System.currentTimeMillis();
			System.out.println( rows + " rows affected (" + (t2-t1) + " ms)" );
		}
		catch (SQLException _e) {
			_e.printStackTrace();
			throw new ArticleDaoException( "SQLException: " + _e.getMessage(), _e );
		}
		catch (Exception _e) {
			_e.printStackTrace();
			throw new ArticleDaoException( "Exception: " + _e.getMessage(), _e );
		}
		finally {
			ResourceManager.close(stmt);
			if (!isConnSupplied) {
				ResourceManager.close(conn);
			}
		
		}
		
	}

	/** 
	 * Returns the rows from the Article table that matches the specified primary-key value.
	 */
	public Article findByPrimaryKey(ArticlePk pk) throws ArticleDaoException
	{
		return findByPrimaryKey( pk.getArticleID() );
	}

	/** 
	 * Returns all rows from the Article table that match the criteria ''.
	 */
	public Article[] findAll() throws ArticleDaoException
	{
		return findByDynamicSelect( SQL_SELECT + " ORDER BY ArticleID", null );
	}

	/** 
	 * Returns all rows from the Article table that match the criteria 'ArticleID = :articleID'.
	 */
	public Article findByPrimaryKey(int articleID) throws ArticleDaoException
	{
		Article ret[] = findByDynamicSelect( SQL_SELECT + " WHERE ArticleID = ?", new Object[] {  new Integer(articleID) } );
		return ret.length==0 ? null : ret[0];
	}

	/** 
	 * Returns all rows from the Article table that match the criteria 'CategoryID = :categoryID'.
	 */
	public Article[] findByCategory(int categoryID) throws ArticleDaoException
	{
		return findByDynamicSelect( SQL_SELECT + " WHERE CategoryID = ?", new Object[] {  new Integer(categoryID) } );
	}

	/** 
	 * Returns all rows from the Article table that match the criteria 'ArticleID = :articleID'.
	 */
	public Article[] findWhereArticleIDEquals(int articleID) throws ArticleDaoException
	{
		return findByDynamicSelect( SQL_SELECT + " WHERE ArticleID = ? ORDER BY ArticleID", new Object[] {  new Integer(articleID) } );
	}

	/** 
	 * Returns all rows from the Article table that match the criteria 'CategoryID = :categoryID'.
	 */
	public Article[] findWhereCategoryIDEquals(int categoryID) throws ArticleDaoException
	{
		return findByDynamicSelect( SQL_SELECT + " WHERE CategoryID = ? ORDER BY CategoryID", new Object[] {  new Integer(categoryID) } );
	}

	/** 
	 * Returns all rows from the Article table that match the criteria 'Subject = :subject'.
	 */
	public Article[] findWhereSubjectEquals(String subject) throws ArticleDaoException
	{
		return findByDynamicSelect( SQL_SELECT + " WHERE Subject = ? ORDER BY Subject", new Object[] { subject } );
	}

	/** 
	 * Returns all rows from the Article table that match the criteria 'Content = :content'.
	 */
	public Article[] findWhereContentEquals(String content) throws ArticleDaoException
	{
		return findByDynamicSelect( SQL_SELECT + " WHERE Content = ? ORDER BY Content", new Object[] { content } );
	}

	/** 
	 * Returns all rows from the Article table that match the criteria 'PublishTime = :publishTime'.
	 */
	public Article[] findWherePublishTimeEquals(Date publishTime) throws ArticleDaoException
	{
		return findByDynamicSelect( SQL_SELECT + " WHERE PublishTime = ? ORDER BY PublishTime", new Object[] { publishTime==null ? null : new java.sql.Timestamp( publishTime.getTime() ) } );
	}

	/**
	 * Method 'ArticleDaoImpl'
	 * 
	 */
	public ArticleDaoImpl()
	{
	}

	/**
	 * Method 'ArticleDaoImpl'
	 * 
	 * @param userConn
	 */
	public ArticleDaoImpl(final java.sql.Connection userConn)
	{
		this.userConn = userConn;
	}

	/** 
	 * Sets the value of maxRows
	 */
	public void setMaxRows(int maxRows)
	{
		this.maxRows = maxRows;
	}

	/** 
	 * Gets the value of maxRows
	 */
	public int getMaxRows()
	{
		return maxRows;
	}

	/**
	 * Method 'getTableName'
	 * 
	 * @return String
	 */
	public String getTableName()
	{
		return "MyBlog..Article";
	}

	/** 
	 * Fetches a single row from the result set
	 */
	protected Article fetchSingleResult(ResultSet rs) throws SQLException
	{
		if (rs.next()) {
			Article dto = new Article();
			populateDto( dto, rs);
			return dto;
		} else {
			return null;
		}
		
	}

	/** 
	 * Fetches multiple rows from the result set
	 */
	protected Article[] fetchMultiResults(ResultSet rs) throws SQLException
	{
		Collection resultList = new ArrayList();
		while (rs.next()) {
			Article dto = new Article();
			populateDto( dto, rs);
			resultList.add( dto );
		}
		
		Article ret[] = new Article[ resultList.size() ];
		resultList.toArray( ret );
		return ret;
	}

	/** 
	 * Populates a DTO with data from a ResultSet
	 */
	protected void populateDto(Article dto, ResultSet rs) throws SQLException
	{
		dto.setArticleID( rs.getInt( COLUMN_ARTICLE_I_D ) );
		dto.setCategoryID( rs.getInt( COLUMN_CATEGORY_I_D ) );
		if (rs.wasNull()) {
			dto.setCategoryIDNull( true );
		}
		
		dto.setSubject( rs.getString( COLUMN_SUBJECT ) );
		dto.setContent( rs.getString( COLUMN_CONTENT ) );
		dto.setPublishTime( rs.getTimestamp(COLUMN_PUBLISH_TIME ) );
	}

	/** 
	 * Returns all rows from the Article table that match the specified arbitrary SQL statement
	 */
	public Article[] findByDynamicSelect(String sql, Object[] sqlParams) throws ArticleDaoException
	{
		// declare variables
		final boolean isConnSupplied = (userConn != null);
		Connection conn = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		
		try {
			// get the user-specified connection or get a connection from the ResourceManager
			conn = isConnSupplied ? userConn : ResourceManager.getConnection();
		
			// construct the SQL statement
			final String SQL = sql;
		
		
			System.out.println( "Executing " + SQL );
			// prepare statement
			stmt = conn.prepareStatement( SQL );
			stmt.setMaxRows( maxRows );
		
			// bind parameters
			for (int i=0; sqlParams!=null && i<sqlParams.length; i++ ) {
				stmt.setObject( i+1, sqlParams[i] );
			}
		
		
			rs = stmt.executeQuery();
		
			// fetch the results
			return fetchMultiResults(rs);
		}
		catch (SQLException _e) {
			_e.printStackTrace();
			throw new ArticleDaoException( "SQLException: " + _e.getMessage(), _e );
		}
		catch (Exception _e) {
			_e.printStackTrace();
			throw new ArticleDaoException( "Exception: " + _e.getMessage(), _e );
		}
		finally {
			ResourceManager.close(rs);
			ResourceManager.close(stmt);
			if (!isConnSupplied) {
				ResourceManager.close(conn);
			}
		
		}
		
	}

	/** 
	 * Returns all rows from the Article table that match the specified arbitrary SQL statement
	 */
	public Article[] findByDynamicWhere(String sql, Object[] sqlParams) throws ArticleDaoException
	{
		// declare variables
		final boolean isConnSupplied = (userConn != null);
		Connection conn = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		
		try {
			// get the user-specified connection or get a connection from the ResourceManager
			conn = isConnSupplied ? userConn : ResourceManager.getConnection();
		
			// construct the SQL statement
			final String SQL = SQL_SELECT + " WHERE " + sql;
		
		
			System.out.println( "Executing " + SQL );
			// prepare statement
			stmt = conn.prepareStatement( SQL );
			stmt.setMaxRows( maxRows );
		
			// bind parameters
			for (int i=0; sqlParams!=null && i<sqlParams.length; i++ ) {
				stmt.setObject( i+1, sqlParams[i] );
			}
		
		
			rs = stmt.executeQuery();
		
			// fetch the results
			return fetchMultiResults(rs);
		}
		catch (SQLException _e) {
			_e.printStackTrace();
			throw new ArticleDaoException( "SQLException: " + _e.getMessage(), _e );
		}
		catch (Exception _e) {
			_e.printStackTrace();
			throw new ArticleDaoException( "Exception: " + _e.getMessage(), _e );
		}
		finally {
			ResourceManager.close(rs);
			ResourceManager.close(stmt);
			if (!isConnSupplied) {
				ResourceManager.close(conn);
			}
		
		}
		
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -