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

📄 jfdaoimpl.java

📁 Struts+jdbc开发的合同管理系统,用的SQL Server 2000做为后台数据库!
💻 JAVA
字号:
/*
 * Author :Cao guangxin
 * on 26-三月-2005 at 09:54:55
 * 
 * Mail:relationinfo@hotmail.com
 * 
 * visit:www.relaioninfo.com or www.helpsoft.org
 */

package org.helpsoft.contract.jdbc;

import org.helpsoft.contract.dao.*;
import org.helpsoft.contract.factory.*;
import org.helpsoft.contract.dto.*;
import org.helpsoft.contract.exceptions.*;
import java.sql.Connection;
import java.sql.Types;
import java.util.Collection;
import org.apache.log4j.Logger;
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;

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

	protected static final Logger logger = Logger.getLogger( JfDaoImpl.class );

	/** 
	 * All finder methods in this class use this SELECT constant to build their queries
	 */
	protected final String SQL_SELECT = "SELECT jfbm, jfmc 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() + " ( jfbm, jfmc ) VALUES ( ?, ? )";

	/** 
	 * SQL UPDATE statement for this table
	 */
	protected final String SQL_UPDATE = "UPDATE " + getTableName() + " SET jfbm = ?, jfmc = ? WHERE jfbm = ?";

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

	/** 
	 * Index of column jfbm
	 */
	protected static final int COLUMN_JFBM = 1;

	/** 
	 * Index of column jfmc
	 */
	protected static final int COLUMN_JFMC = 2;

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

	/** 
	 * Index of primary-key column jfbm
	 */
	protected static final int PK_COLUMN_JFBM = 1;

	/** 
	 * Inserts a new row in the jf table.
	 */
	public JfPk insert(Jf dto) throws JfDaoException
	{
		long t1 = System.currentTimeMillis();
		// 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();
		
			stmt = conn.prepareStatement( SQL_INSERT );
			stmt.setString( COLUMN_JFBM, dto.getJfbm() );
			stmt.setString( COLUMN_JFMC, dto.getJfmc() );
			if (logger.isDebugEnabled()) {
				logger.debug( "Executing " + SQL_INSERT + " with DTO: " + dto);
			}
		
			int rows = stmt.executeUpdate();
			long t2 = System.currentTimeMillis();
			if (logger.isDebugEnabled()) {
				logger.debug( rows + " rows affected (" + (t2-t1) + " ms)");
			}
		
			return dto.createPk();
		}
		catch (SQLException _e) {
			logger.error( "SQLException: " + _e.getMessage(), _e );
			throw new JfDaoException( "SQLException: " + _e.getMessage(), _e );
		}
		catch (Exception _e) {
			logger.error( "Exception: " + _e.getMessage(), _e );
			throw new JfDaoException( "Exception: " + _e.getMessage(), _e );
		}
		finally {
			ResourceManager.close(stmt);
			if (!isConnSupplied) {
				ResourceManager.close(conn);
			}
		
		}
		
	}

	/** 
	 * Updates a single row in the jf table.
	 */
	public void update(JfPk pk, Jf dto) throws JfDaoException
	{
		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();
		
			if (logger.isDebugEnabled()) {
				logger.debug( "Executing " + SQL_UPDATE + " with DTO: " + dto);
			}
		
			stmt = conn.prepareStatement( SQL_UPDATE );
			stmt.setString( COLUMN_JFBM, dto.getJfbm() );
			stmt.setString( COLUMN_JFMC, dto.getJfmc() );
			stmt.setString( 3, pk.getJfbm() );
			int rows = stmt.executeUpdate();
			long t2 = System.currentTimeMillis();
			if (logger.isDebugEnabled()) {
				logger.debug( rows + " rows affected (" + (t2-t1) + " ms)");
			}
		
		}
		catch (SQLException _e) {
			logger.error( "SQLException: " + _e.getMessage(), _e );
			throw new JfDaoException( "SQLException: " + _e.getMessage(), _e );
		}
		catch (Exception _e) {
			logger.error( "Exception: " + _e.getMessage(), _e );
			throw new JfDaoException( "Exception: " + _e.getMessage(), _e );
		}
		finally {
			ResourceManager.close(stmt);
			if (!isConnSupplied) {
				ResourceManager.close(conn);
			}
		
		}
		
	}

	/** 
	 * Deletes a single row in the jf table.
	 */
	public void delete(JfPk pk) throws JfDaoException
	{
		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();
		
			if (logger.isDebugEnabled()) {
				logger.debug( "Executing " + SQL_DELETE + " with PK: " + pk);
			}
		
			stmt = conn.prepareStatement( SQL_DELETE );
			stmt.setString( 1, pk.getJfbm() );
			int rows = stmt.executeUpdate();
			long t2 = System.currentTimeMillis();
			if (logger.isDebugEnabled()) {
				logger.debug( rows + " rows affected (" + (t2-t1) + " ms)");
			}
		
		}
		catch (SQLException _e) {
			logger.error( "SQLException: " + _e.getMessage(), _e );
			throw new JfDaoException( "SQLException: " + _e.getMessage(), _e );
		}
		catch (Exception _e) {
			logger.error( "Exception: " + _e.getMessage(), _e );
			throw new JfDaoException( "Exception: " + _e.getMessage(), _e );
		}
		finally {
			ResourceManager.close(stmt);
			if (!isConnSupplied) {
				ResourceManager.close(conn);
			}
		
		}
		
	}

	/** 
	 * Returns the rows from the jf table that matches the specified primary-key value.
	 */
	public Jf findByPrimaryKey(JfPk pk) throws JfDaoException
	{
		return findByPrimaryKey( pk.getJfbm() );
	}

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

	/** 
	 * Returns all rows from the jf table that match the criteria 'jfbm = :jfbm'.
	 */
	public Jf findByPrimaryKey(String jfbm) throws JfDaoException
	{
		Jf ret[] = findByDynamicSelect( SQL_SELECT + " WHERE jfbm = ?", new Object[] { jfbm } );
		return ret.length==0 ? null : ret[0];
	}

	/** 
	 * Returns all rows from the jf table that match the criteria 'jfbm = :jfbm'.
	 */
	public Jf[] findWhereJfbmEquals(String jfbm) throws JfDaoException
	{
		return findByDynamicSelect( SQL_SELECT + " WHERE jfbm = ? ORDER BY jfbm", new Object[] { jfbm } );
	}

	/** 
	 * Returns all rows from the jf table that match the criteria 'jfmc = :jfmc'.
	 */
	public Jf[] findWhereJfmcEquals(String jfmc) throws JfDaoException
	{
		return findByDynamicSelect( SQL_SELECT + " WHERE jfmc = ? ORDER BY jfmc", new Object[] { jfmc } );
	}

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

	/**
	 * Method 'JfDaoImpl'
	 * 
	 * @param userConn
	 */
	public JfDaoImpl(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 "jf";
	}

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

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

	/** 
	 * Populates a DTO with data from a ResultSet
	 */
	protected void populateDto(Jf dto, ResultSet rs) throws SQLException
	{
		dto.setJfbm( rs.getString( COLUMN_JFBM ) );
		dto.setJfmc( rs.getString( COLUMN_JFMC ) );
	}

	/** 
	 * Returns all rows from the jf table that match the specified arbitrary SQL statement
	 */
	public Jf[] findByDynamicSelect(String sql, Object[] sqlParams) throws JfDaoException
	{
		// 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;
		
		
			if (logger.isDebugEnabled()) {
				logger.debug( "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) {
			logger.error( "SQLException: " + _e.getMessage(), _e );
			throw new JfDaoException( "SQLException: " + _e.getMessage(), _e );
		}
		catch (Exception _e) {
			logger.error( "Exception: " + _e.getMessage(), _e );
			throw new JfDaoException( "Exception: " + _e.getMessage(), _e );
		}
		finally {
			ResourceManager.close(rs);
			ResourceManager.close(stmt);
			if (!isConnSupplied) {
				ResourceManager.close(conn);
			}
		
		}
		
	}

	/** 
	 * Returns all rows from the jf table that match the specified arbitrary SQL statement
	 */
	public Jf[] findByDynamicWhere(String sql, Object[] sqlParams) throws JfDaoException
	{
		// 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;
		
		
			if (logger.isDebugEnabled()) {
				logger.debug( "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) {
			logger.error( "SQLException: " + _e.getMessage(), _e );
			throw new JfDaoException( "SQLException: " + _e.getMessage(), _e );
		}
		catch (Exception _e) {
			logger.error( "Exception: " + _e.getMessage(), _e );
			throw new JfDaoException( "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 + -