📄 essaydaoimpl.java
字号:
conn = isConnSupplied ? userConn : ResourceManager.getConnection();
System.out.println( "Executing " + SQL_DELETE + " with PK: " + pk );
stmt = conn.prepareStatement( SQL_DELETE );
stmt.setInt( 1, pk.getEssayID() );
int rows = stmt.executeUpdate();
long t2 = System.currentTimeMillis();
System.out.println( rows + " rows affected (" + (t2-t1) + " ms)" );
}
catch (SQLException _e) {
_e.printStackTrace();
throw new EssayDaoException( "SQLException: " + _e.getMessage(), _e );
}
catch (Exception _e) {
_e.printStackTrace();
throw new EssayDaoException( "Exception: " + _e.getMessage(), _e );
}
finally {
ResourceManager.close(stmt);
if (!isConnSupplied) {
ResourceManager.close(conn);
}
}
}
/**
* Returns the rows from the Essay table that matches the specified primary-key value.
*/
public Essay findByPrimaryKey(EssayPk pk) throws EssayDaoException
{
return findByPrimaryKey( pk.getEssayID() );
}
/**
* Returns all rows from the Essay table that match the criteria ''.
*/
public Essay[] findAll() throws EssayDaoException
{
return findByDynamicSelect( SQL_SELECT + " ORDER BY EssayID", null );
}
/**
* Returns all rows from the Essay table that match the criteria 'EssayID = :essayID'.
*/
public Essay findByPrimaryKey(int essayID) throws EssayDaoException
{
Essay ret[] = findByDynamicSelect( SQL_SELECT + " WHERE EssayID = ?", new Object[] { new Integer(essayID) } );
return ret.length==0 ? null : ret[0];
}
/**
* Returns all rows from the Essay table that match the criteria 'CategoryID = :categoryID'.
*/
public Essay[] findByCategory(int categoryID) throws EssayDaoException
{
return findByDynamicSelect( SQL_SELECT + " WHERE CategoryID = ?", new Object[] { new Integer(categoryID) } );
}
/**
* Returns all rows from the Essay table that match the criteria 'CategoryID = :categoryID'.
*/
public Essay[] findWhereCategoryIDEquals(int categoryID) throws EssayDaoException
{
return findByDynamicSelect( SQL_SELECT + " WHERE CategoryID = ? ORDER BY CategoryID", new Object[] { new Integer(categoryID) } );
}
/**
* Returns all rows from the Essay table that match the criteria 'EssayID = :essayID'.
*/
public Essay[] findWhereEssayIDEquals(int essayID) throws EssayDaoException
{
return findByDynamicSelect( SQL_SELECT + " WHERE EssayID = ? ORDER BY EssayID", new Object[] { new Integer(essayID) } );
}
/**
* Returns all rows from the Essay table that match the criteria 'Subject = :subject'.
*/
public Essay[] findWhereSubjectEquals(String subject) throws EssayDaoException
{
return findByDynamicSelect( SQL_SELECT + " WHERE Subject = ? ORDER BY Subject", new Object[] { subject } );
}
/**
* Returns all rows from the Essay table that match the criteria 'Content = :content'.
*/
public Essay[] findWhereContentEquals(String content) throws EssayDaoException
{
return findByDynamicSelect( SQL_SELECT + " WHERE Content = ? ORDER BY Content", new Object[] { content } );
}
/**
* Returns all rows from the Essay table that match the criteria 'PublishTime = :publishTime'.
*/
public Essay[] findWherePublishTimeEquals(Date publishTime) throws EssayDaoException
{
return findByDynamicSelect( SQL_SELECT + " WHERE PublishTime = ? ORDER BY PublishTime", new Object[] { publishTime==null ? null : new java.sql.Timestamp( publishTime.getTime() ) } );
}
/**
* Method 'EssayDaoImpl'
*
*/
public EssayDaoImpl()
{
}
/**
* Method 'EssayDaoImpl'
*
* @param userConn
*/
public EssayDaoImpl(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..Essay";
}
/**
* Fetches a single row from the result set
*/
protected Essay fetchSingleResult(ResultSet rs) throws SQLException
{
if (rs.next()) {
Essay dto = new Essay();
populateDto( dto, rs);
return dto;
} else {
return null;
}
}
/**
* Fetches multiple rows from the result set
*/
protected Essay[] fetchMultiResults(ResultSet rs) throws SQLException
{
Collection resultList = new ArrayList();
while (rs.next()) {
Essay dto = new Essay();
populateDto( dto, rs);
resultList.add( dto );
}
Essay ret[] = new Essay[ resultList.size() ];
resultList.toArray( ret );
return ret;
}
/**
* Populates a DTO with data from a ResultSet
*/
protected void populateDto(Essay dto, ResultSet rs) throws SQLException
{
dto.setCategoryID( rs.getInt( COLUMN_CATEGORY_I_D ) );
if (rs.wasNull()) {
dto.setCategoryIDNull( true );
}
dto.setEssayID( rs.getInt( COLUMN_ESSAY_I_D ) );
dto.setSubject( rs.getString( COLUMN_SUBJECT ) );
dto.setContent( rs.getString( COLUMN_CONTENT ) );
dto.setPublishTime( rs.getTimestamp(COLUMN_PUBLISH_TIME ) );
}
/**
* Returns all rows from the Essay table that match the specified arbitrary SQL statement
*/
public Essay[] findByDynamicSelect(String sql, Object[] sqlParams) throws EssayDaoException
{
// 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 EssayDaoException( "SQLException: " + _e.getMessage(), _e );
}
catch (Exception _e) {
_e.printStackTrace();
throw new EssayDaoException( "Exception: " + _e.getMessage(), _e );
}
finally {
ResourceManager.close(rs);
ResourceManager.close(stmt);
if (!isConnSupplied) {
ResourceManager.close(conn);
}
}
}
/**
* Returns all rows from the Essay table that match the specified arbitrary SQL statement
*/
public Essay[] findByDynamicWhere(String sql, Object[] sqlParams) throws EssayDaoException
{
// 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 EssayDaoException( "SQLException: " + _e.getMessage(), _e );
}
catch (Exception _e) {
_e.printStackTrace();
throw new EssayDaoException( "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 + -