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

📄 jfdlrdaoimpl.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 JfdlrDaoImpl extends AbstractDataAccessObject implements JfdlrDao
{
	/** 
	 * 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( JfdlrDaoImpl.class );

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

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

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

	/** 
	 * Index of column jfdlrbm
	 */
	protected static final int COLUMN_JFDLRBM = 1;

	/** 
	 * Index of column jfdlrxm
	 */
	protected static final int COLUMN_JFDLRXM = 2;

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

	/** 
	 * Index of primary-key column jfdlrbm
	 */
	protected static final int PK_COLUMN_JFDLRBM = 1;

	/** 
	 * Inserts a new row in the jfdlr table.
	 */
	public JfdlrPk insert(Jfdlr dto) throws JfdlrDaoException
	{
		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_JFDLRBM, dto.getJfdlrbm() );
			stmt.setString( COLUMN_JFDLRXM, dto.getJfdlrxm() );
			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 JfdlrDaoException( "SQLException: " + _e.getMessage(), _e );
		}
		catch (Exception _e) {
			logger.error( "Exception: " + _e.getMessage(), _e );
			throw new JfdlrDaoException( "Exception: " + _e.getMessage(), _e );
		}
		finally {
			ResourceManager.close(stmt);
			if (!isConnSupplied) {
				ResourceManager.close(conn);
			}
		
		}
		
	}

	/** 
	 * Updates a single row in the jfdlr table.
	 */
	public void update(JfdlrPk pk, Jfdlr dto) throws JfdlrDaoException
	{
		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_JFDLRBM, dto.getJfdlrbm() );
			stmt.setString( COLUMN_JFDLRXM, dto.getJfdlrxm() );
			stmt.setString( 3, pk.getJfdlrbm() );
			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 JfdlrDaoException( "SQLException: " + _e.getMessage(), _e );
		}
		catch (Exception _e) {
			logger.error( "Exception: " + _e.getMessage(), _e );
			throw new JfdlrDaoException( "Exception: " + _e.getMessage(), _e );
		}
		finally {
			ResourceManager.close(stmt);
			if (!isConnSupplied) {
				ResourceManager.close(conn);
			}
		
		}
		
	}

	/** 
	 * Deletes a single row in the jfdlr table.
	 */
	public void delete(JfdlrPk pk) throws JfdlrDaoException
	{
		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.getJfdlrbm() );
			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 JfdlrDaoException( "SQLException: " + _e.getMessage(), _e );
		}
		catch (Exception _e) {
			logger.error( "Exception: " + _e.getMessage(), _e );
			throw new JfdlrDaoException( "Exception: " + _e.getMessage(), _e );
		}
		finally {
			ResourceManager.close(stmt);
			if (!isConnSupplied) {
				ResourceManager.close(conn);
			}
		
		}
		
	}

	/** 
	 * Returns the rows from the jfdlr table that matches the specified primary-key value.
	 */
	public Jfdlr findByPrimaryKey(JfdlrPk pk) throws JfdlrDaoException
	{
		return findByPrimaryKey( pk.getJfdlrbm() );
	}

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

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

	/** 
	 * Returns all rows from the jfdlr table that match the criteria 'jfdlrbm = :jfdlrbm'.
	 */
	public Jfdlr[] findWhereJfdlrbmEquals(String jfdlrbm) throws JfdlrDaoException
	{
		return findByDynamicSelect( SQL_SELECT + " WHERE jfdlrbm = ? ORDER BY jfdlrbm", new Object[] { jfdlrbm } );
	}

	/** 
	 * Returns all rows from the jfdlr table that match the criteria 'jfdlrxm = :jfdlrxm'.
	 */
	public Jfdlr[] findWhereJfdlrxmEquals(String jfdlrxm) throws JfdlrDaoException
	{
		return findByDynamicSelect( SQL_SELECT + " WHERE jfdlrxm = ? ORDER BY jfdlrxm", new Object[] { jfdlrxm } );
	}

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

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

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

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

	/** 
	 * Populates a DTO with data from a ResultSet
	 */
	protected void populateDto(Jfdlr dto, ResultSet rs) throws SQLException
	{
		dto.setJfdlrbm( rs.getString( COLUMN_JFDLRBM ) );
		dto.setJfdlrxm( rs.getString( COLUMN_JFDLRXM ) );
	}

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

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