📄 categorydaoimpl.java
字号:
/*
* 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 CategoryDaoImpl extends AbstractDataAccessObject implements CategoryDao
{
/**
* 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 CategoryID, Name, Description 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() + " ( Name, Description ) VALUES ( ?, ? );SELECT @@IDENTITY";
/**
* SQL UPDATE statement for this table
*/
protected final String SQL_UPDATE = "UPDATE " + getTableName() + " SET Name = ?, Description = ? WHERE CategoryID = ?";
/**
* SQL DELETE statement for this table
*/
protected final String SQL_DELETE = "DELETE FROM " + getTableName() + " WHERE CategoryID = ?";
/**
* Index of column CategoryID
*/
protected static final int COLUMN_CATEGORY_I_D = 1;
/**
* Index of column Name
*/
protected static final int COLUMN_NAME = 2;
/**
* Index of column Description
*/
protected static final int COLUMN_DESCRIPTION = 3;
/**
* Number of columns
*/
protected static final int NUMBER_OF_COLUMNS = 3;
/**
* Index of primary-key column CategoryID
*/
protected static final int PK_COLUMN_CATEGORY_I_D = 1;
/**
* Inserts a new row in the Category table.
*/
public CategoryPk insert(Category dto) throws CategoryDaoException
{
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 'CategoryID
stmt.setString( COLUMN_NAME - 1, dto.getName() );
stmt.setString( COLUMN_DESCRIPTION - 1, dto.getDescription() );
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.setCategoryID( rs.getInt( 1 ) );
}
moreResults = stmt.getMoreResults();
rows = stmt.getUpdateCount();
}
return dto.createPk();
}
catch (SQLException _e) {
_e.printStackTrace();
throw new CategoryDaoException( "SQLException: " + _e.getMessage(), _e );
}
catch (Exception _e) {
_e.printStackTrace();
throw new CategoryDaoException( "Exception: " + _e.getMessage(), _e );
}
finally {
ResourceManager.close(stmt);
if (!isConnSupplied) {
ResourceManager.close(conn);
}
}
}
/**
* Updates a single row in the Category table.
*/
public void update(CategoryPk pk, Category dto) throws CategoryDaoException
{
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.isCategoryIDModified()) {
if (modified) {
sql.append( ", " );
}
sql.append( "CategoryID=?" );
modified=true;
}
if (dto.isNameModified()) {
if (modified) {
sql.append( ", " );
}
sql.append( "Name=?" );
modified=true;
}
if (dto.isDescriptionModified()) {
if (modified) {
sql.append( ", " );
}
sql.append( "Description=?" );
modified=true;
}
if (!modified) {
// nothing to update
return;
}
sql.append( " WHERE CategoryID=?" );
System.out.println( "Executing " + sql.toString() + " with values: " + dto );
stmt = conn.prepareStatement( sql.toString() );
int index = 1;
if (dto.isCategoryIDModified()) {
stmt.setInt( index++, dto.getCategoryID() );
}
if (dto.isNameModified()) {
stmt.setString( index++, dto.getName() );
}
if (dto.isDescriptionModified()) {
stmt.setString( index++, dto.getDescription() );
}
stmt.setInt( index++, pk.getCategoryID() );
int rows = stmt.executeUpdate();
long t2 = System.currentTimeMillis();
System.out.println( rows + " rows affected (" + (t2-t1) + " ms)" );
}
catch (SQLException _e) {
_e.printStackTrace();
throw new CategoryDaoException( "SQLException: " + _e.getMessage(), _e );
}
catch (Exception _e) {
_e.printStackTrace();
throw new CategoryDaoException( "Exception: " + _e.getMessage(), _e );
}
finally {
ResourceManager.close(stmt);
if (!isConnSupplied) {
ResourceManager.close(conn);
}
}
}
/**
* Deletes a single row in the Category table.
*/
public void delete(CategoryPk pk) throws CategoryDaoException
{
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.getCategoryID() );
int rows = stmt.executeUpdate();
long t2 = System.currentTimeMillis();
System.out.println( rows + " rows affected (" + (t2-t1) + " ms)" );
}
catch (SQLException _e) {
_e.printStackTrace();
throw new CategoryDaoException( "SQLException: " + _e.getMessage(), _e );
}
catch (Exception _e) {
_e.printStackTrace();
throw new CategoryDaoException( "Exception: " + _e.getMessage(), _e );
}
finally {
ResourceManager.close(stmt);
if (!isConnSupplied) {
ResourceManager.close(conn);
}
}
}
/**
* Returns the rows from the Category table that matches the specified primary-key value.
*/
public Category findByPrimaryKey(CategoryPk pk) throws CategoryDaoException
{
return findByPrimaryKey( pk.getCategoryID() );
}
/**
* Returns all rows from the Category table that match the criteria ''.
*/
public Category[] findAll() throws CategoryDaoException
{
return findByDynamicSelect( SQL_SELECT + " ORDER BY CategoryID", null );
}
/**
* Returns all rows from the Category table that match the criteria 'CategoryID = :categoryID'.
*/
public Category findByPrimaryKey(int categoryID) throws CategoryDaoException
{
Category ret[] = findByDynamicSelect( SQL_SELECT + " WHERE CategoryID = ?", new Object[] { new Integer(categoryID) } );
return ret.length==0 ? null : ret[0];
}
/**
* Returns all rows from the Category table that match the criteria 'CategoryID = :categoryID'.
*/
public Category[] findWhereCategoryIDEquals(int categoryID) throws CategoryDaoException
{
return findByDynamicSelect( SQL_SELECT + " WHERE CategoryID = ? ORDER BY CategoryID", new Object[] { new Integer(categoryID) } );
}
/**
* Returns all rows from the Category table that match the criteria 'Name = :name'.
*/
public Category[] findWhereNameEquals(String name) throws CategoryDaoException
{
return findByDynamicSelect( SQL_SELECT + " WHERE Name = ? ORDER BY Name", new Object[] { name } );
}
/**
* Returns all rows from the Category table that match the criteria 'Description = :description'.
*/
public Category[] findWhereDescriptionEquals(String description) throws CategoryDaoException
{
return findByDynamicSelect( SQL_SELECT + " WHERE Description = ? ORDER BY Description", new Object[] { description } );
}
/**
* Method 'CategoryDaoImpl'
*
*/
public CategoryDaoImpl()
{
}
/**
* Method 'CategoryDaoImpl'
*
* @param userConn
*/
public CategoryDaoImpl(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..Category";
}
/**
* Fetches a single row from the result set
*/
protected Category fetchSingleResult(ResultSet rs) throws SQLException
{
if (rs.next()) {
Category dto = new Category();
populateDto( dto, rs);
return dto;
} else {
return null;
}
}
/**
* Fetches multiple rows from the result set
*/
protected Category[] fetchMultiResults(ResultSet rs) throws SQLException
{
Collection resultList = new ArrayList();
while (rs.next()) {
Category dto = new Category();
populateDto( dto, rs);
resultList.add( dto );
}
Category ret[] = new Category[ resultList.size() ];
resultList.toArray( ret );
return ret;
}
/**
* Populates a DTO with data from a ResultSet
*/
protected void populateDto(Category dto, ResultSet rs) throws SQLException
{
dto.setCategoryID( rs.getInt( COLUMN_CATEGORY_I_D ) );
dto.setName( rs.getString( COLUMN_NAME ) );
dto.setDescription( rs.getString( COLUMN_DESCRIPTION ) );
}
/**
* Returns all rows from the Category table that match the specified arbitrary SQL statement
*/
public Category[] findByDynamicSelect(String sql, Object[] sqlParams) throws CategoryDaoException
{
// 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 CategoryDaoException( "SQLException: " + _e.getMessage(), _e );
}
catch (Exception _e) {
_e.printStackTrace();
throw new CategoryDaoException( "Exception: " + _e.getMessage(), _e );
}
finally {
ResourceManager.close(rs);
ResourceManager.close(stmt);
if (!isConnSupplied) {
ResourceManager.close(conn);
}
}
}
/**
* Returns all rows from the Category table that match the specified arbitrary SQL statement
*/
public Category[] findByDynamicWhere(String sql, Object[] sqlParams) throws CategoryDaoException
{
// 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 CategoryDaoException( "SQLException: " + _e.getMessage(), _e );
}
catch (Exception _e) {
_e.printStackTrace();
throw new CategoryDaoException( "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 + -