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

📄 picturedaoimpl.java

📁 jsp做的blog jsp做的blog
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 * This source file was generated by FireStorm/DAO 3.0 (build 99)
 * on 26-十月-2005 at 09:50:56
 * 
 * If you purchase a full license for FireStorm/DAO you can customize this file header.
 * 
 * For more information please visit http://www.codefutures.com/products/firestorm
 */

package cn.wanfeng.myblog.jdbc;

import cn.wanfeng.myblog.dao.*;
import cn.wanfeng.myblog.factory.*;
import cn.wanfeng.myblog.dto.*;
import cn.wanfeng.myblog.exceptions.*;
import java.sql.Connection;
import java.sql.Types;
import java.util.Collection;
import java.sql.PreparedStatement;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Time;
import java.util.List;
import java.util.Iterator;
import java.util.ArrayList;
import java.sql.CallableStatement;

public class PictureDaoImpl extends AbstractDataAccessObject implements PictureDao
{
	/** 
	 * The factory class for this DAO has two versions of the create() method - one thattakes no arguments and one that takes a Connection argument. If the Connection versionis chosen then the connection will be stored in this attribute and will be used by allcalls to this DAO, otherwise a new Connection will be allocated for each operation.
	 */
	protected java.sql.Connection userConn;

	/** 
	 * All finder methods in this class use this SELECT constant to build their queries
	 */
	protected final String SQL_SELECT = "SELECT PictureID, CategoryID, Subject, FileName FROM " + getTableName() + "";

	/** 
	 * Finder methods will pass this value to the JDBC setMaxRows method
	 */
	private int maxRows;

	/** 
	 * SQL INSERT statement for this table
	 */
	protected final String SQL_INSERT = "INSERT INTO " + getTableName() + " ( CategoryID, Subject, FileName ) VALUES ( ?, ?, ? );SELECT @@IDENTITY";

	/** 
	 * SQL UPDATE statement for this table
	 */
	protected final String SQL_UPDATE = "UPDATE " + getTableName() + " SET CategoryID = ?, Subject = ?, FileName = ? WHERE PictureID = ?";

	/** 
	 * SQL DELETE statement for this table
	 */
	protected final String SQL_DELETE = "DELETE FROM " + getTableName() + " WHERE PictureID = ?";

	/** 
	 * Index of column PictureID
	 */
	protected static final int COLUMN_PICTURE_I_D = 1;

	/** 
	 * Index of column CategoryID
	 */
	protected static final int COLUMN_CATEGORY_I_D = 2;

	/** 
	 * Index of column Subject
	 */
	protected static final int COLUMN_SUBJECT = 3;

	/** 
	 * Index of column FileName
	 */
	protected static final int COLUMN_FILE_NAME = 4;

	/** 
	 * Number of columns
	 */
	protected static final int NUMBER_OF_COLUMNS = 4;

	/** 
	 * Index of primary-key column PictureID
	 */
	protected static final int PK_COLUMN_PICTURE_I_D = 1;

	/** 
	 * Inserts a new row in the Picture table.
	 */
	public PicturePk insert(Picture dto) throws PictureDaoException
	{
		long t1 = System.currentTimeMillis();
		// declare variables
		final boolean isConnSupplied = (userConn != null);
		Connection conn = null;
		CallableStatement stmt = null;
		ResultSet rs = null;
		
		try {
			// get the user-specified connection or get a connection from the ResourceManager
			conn = isConnSupplied ? userConn : ResourceManager.getConnection();
		
			stmt = conn.prepareCall( SQL_INSERT );
			// no bind statement for auto increment column 'PictureID
			if (dto.isCategoryIDNull()) {
				stmt.setNull( COLUMN_CATEGORY_I_D - 1, Types.INTEGER );
			} else {
				stmt.setInt( COLUMN_CATEGORY_I_D - 1, dto.getCategoryID() );
			}
		
			stmt.setString( COLUMN_SUBJECT - 1, dto.getSubject() );
			stmt.setString( COLUMN_FILE_NAME - 1, dto.getFileName() );
			System.out.println( "Executing " + SQL_INSERT + " with DTO: " + dto );
			stmt.execute();
			int rows = stmt.getUpdateCount();
			System.out.println( rows + " rows affected" );
		
			// retrieve values from IDENTITY columns
			boolean moreResults = true;
			while (moreResults || rows != -1) {
				try {
					rs = stmt.getResultSet();
				}
				catch (IllegalArgumentException e) {
					e.printStackTrace();
				}
		
				if (rs != null) {
					rs.next();
					dto.setPictureID( rs.getInt( 1 ) );
				}
		
				moreResults = stmt.getMoreResults();
				rows = stmt.getUpdateCount();
			}
		
			return dto.createPk();
		}
		catch (SQLException _e) {
			_e.printStackTrace();
			throw new PictureDaoException( "SQLException: " + _e.getMessage(), _e );
		}
		catch (Exception _e) {
			_e.printStackTrace();
			throw new PictureDaoException( "Exception: " + _e.getMessage(), _e );
		}
		finally {
			ResourceManager.close(stmt);
			if (!isConnSupplied) {
				ResourceManager.close(conn);
			}
		
		}
		
	}

	/** 
	 * Updates a single row in the Picture table.
	 */
	public void update(PicturePk pk, Picture dto) throws PictureDaoException
	{
		long t1 = System.currentTimeMillis();
		// declare variables
		final boolean isConnSupplied = (userConn != null);
		Connection conn = null;
		PreparedStatement stmt = null;
		
		try {
			// get the user-specified connection or get a connection from the ResourceManager
			conn = isConnSupplied ? userConn : ResourceManager.getConnection();
		
			StringBuffer sql = new StringBuffer();
			sql.append( "UPDATE " + getTableName() + " SET " );
			boolean modified = false;
			if (dto.isPictureIDModified()) {
				if (modified) {
					sql.append( ", " );
				}
		
				sql.append( "PictureID=?" );
				modified=true;
			}
		
			if (dto.isCategoryIDModified()) {
				if (modified) {
					sql.append( ", " );
				}
		
				sql.append( "CategoryID=?" );
				modified=true;
			}
		
			if (dto.isSubjectModified()) {
				if (modified) {
					sql.append( ", " );
				}
		
				sql.append( "Subject=?" );
				modified=true;
			}
		
			if (dto.isFileNameModified()) {
				if (modified) {
					sql.append( ", " );
				}
		
				sql.append( "FileName=?" );
				modified=true;
			}
		
			if (!modified) {
				// nothing to update
				return;
			}
		
			sql.append( " WHERE PictureID=?" );
			System.out.println( "Executing " + sql.toString() + " with values: " + dto );
			stmt = conn.prepareStatement( sql.toString() );
			int index = 1;
			if (dto.isPictureIDModified()) {
				stmt.setInt( index++, dto.getPictureID() );
			}
		
			if (dto.isCategoryIDModified()) {
				if (dto.isCategoryIDNull()) {
					stmt.setNull( index++, Types.INTEGER );
				} else {
					stmt.setInt( index++, dto.getCategoryID() );
				}
		
			}
		
			if (dto.isSubjectModified()) {
				stmt.setString( index++, dto.getSubject() );
			}
		
			if (dto.isFileNameModified()) {
				stmt.setString( index++, dto.getFileName() );
			}
		
			stmt.setInt( index++, pk.getPictureID() );
			int rows = stmt.executeUpdate();
			long t2 = System.currentTimeMillis();
			System.out.println( rows + " rows affected (" + (t2-t1) + " ms)" );
		}
		catch (SQLException _e) {
			_e.printStackTrace();
			throw new PictureDaoException( "SQLException: " + _e.getMessage(), _e );
		}
		catch (Exception _e) {
			_e.printStackTrace();
			throw new PictureDaoException( "Exception: " + _e.getMessage(), _e );
		}
		finally {
			ResourceManager.close(stmt);
			if (!isConnSupplied) {
				ResourceManager.close(conn);
			}
		
		}
		
	}

	/** 
	 * Deletes a single row in the Picture table.
	 */
	public void delete(PicturePk pk) throws PictureDaoException
	{
		long t1 = System.currentTimeMillis();
		// declare variables
		final boolean isConnSupplied = (userConn != null);
		Connection conn = null;
		PreparedStatement stmt = null;
		
		try {
			// get the user-specified connection or get a connection from the ResourceManager
			conn = isConnSupplied ? userConn : ResourceManager.getConnection();
		
			System.out.println( "Executing " + SQL_DELETE + " with PK: " + pk );
			stmt = conn.prepareStatement( SQL_DELETE );
			stmt.setInt( 1, pk.getPictureID() );

⌨️ 快捷键说明

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