📄 picturedaoimpl.java
字号:
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);
}
}
}
/**
* Returns the rows from the Picture table that matches the specified primary-key value.
*/
public Picture findByPrimaryKey(PicturePk pk) throws PictureDaoException
{
return findByPrimaryKey( pk.getPictureID() );
}
/**
* Returns all rows from the Picture table that match the criteria ''.
*/
public Picture[] findAll() throws PictureDaoException
{
return findByDynamicSelect( SQL_SELECT + " ORDER BY PictureID", null );
}
/**
* Returns all rows from the Picture table that match the criteria 'PictureID = :pictureID'.
*/
public Picture findByPrimaryKey(int pictureID) throws PictureDaoException
{
Picture ret[] = findByDynamicSelect( SQL_SELECT + " WHERE PictureID = ?", new Object[] { new Integer(pictureID) } );
return ret.length==0 ? null : ret[0];
}
/**
* Returns all rows from the Picture table that match the criteria 'CategoryID = :categoryID'.
*/
public Picture[] findByCategory(int categoryID) throws PictureDaoException
{
return findByDynamicSelect( SQL_SELECT + " WHERE CategoryID = ?", new Object[] { new Integer(categoryID) } );
}
/**
* Returns all rows from the Picture table that match the criteria 'PictureID = :pictureID'.
*/
public Picture[] findWherePictureIDEquals(int pictureID) throws PictureDaoException
{
return findByDynamicSelect( SQL_SELECT + " WHERE PictureID = ? ORDER BY PictureID", new Object[] { new Integer(pictureID) } );
}
/**
* Returns all rows from the Picture table that match the criteria 'CategoryID = :categoryID'.
*/
public Picture[] findWhereCategoryIDEquals(int categoryID) throws PictureDaoException
{
return findByDynamicSelect( SQL_SELECT + " WHERE CategoryID = ? ORDER BY CategoryID", new Object[] { new Integer(categoryID) } );
}
/**
* Returns all rows from the Picture table that match the criteria 'Subject = :subject'.
*/
public Picture[] findWhereSubjectEquals(String subject) throws PictureDaoException
{
return findByDynamicSelect( SQL_SELECT + " WHERE Subject = ? ORDER BY Subject", new Object[] { subject } );
}
/**
* Returns all rows from the Picture table that match the criteria 'FileName = :fileName'.
*/
public Picture[] findWhereFileNameEquals(String fileName) throws PictureDaoException
{
return findByDynamicSelect( SQL_SELECT + " WHERE FileName = ? ORDER BY FileName", new Object[] { fileName } );
}
/**
* Method 'PictureDaoImpl'
*
*/
public PictureDaoImpl()
{
}
/**
* Method 'PictureDaoImpl'
*
* @param userConn
*/
public PictureDaoImpl(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..Picture";
}
/**
* Fetches a single row from the result set
*/
protected Picture fetchSingleResult(ResultSet rs) throws SQLException
{
if (rs.next()) {
Picture dto = new Picture();
populateDto( dto, rs);
return dto;
} else {
return null;
}
}
/**
* Fetches multiple rows from the result set
*/
protected Picture[] fetchMultiResults(ResultSet rs) throws SQLException
{
Collection resultList = new ArrayList();
while (rs.next()) {
Picture dto = new Picture();
populateDto( dto, rs);
resultList.add( dto );
}
Picture ret[] = new Picture[ resultList.size() ];
resultList.toArray( ret );
return ret;
}
/**
* Populates a DTO with data from a ResultSet
*/
protected void populateDto(Picture dto, ResultSet rs) throws SQLException
{
dto.setPictureID( rs.getInt( COLUMN_PICTURE_I_D ) );
dto.setCategoryID( rs.getInt( COLUMN_CATEGORY_I_D ) );
if (rs.wasNull()) {
dto.setCategoryIDNull( true );
}
dto.setSubject( rs.getString( COLUMN_SUBJECT ) );
dto.setFileName( rs.getString( COLUMN_FILE_NAME ) );
}
/**
* Returns all rows from the Picture table that match the specified arbitrary SQL statement
*/
public Picture[] findByDynamicSelect(String sql, Object[] sqlParams) throws PictureDaoException
{
// 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 PictureDaoException( "SQLException: " + _e.getMessage(), _e );
}
catch (Exception _e) {
_e.printStackTrace();
throw new PictureDaoException( "Exception: " + _e.getMessage(), _e );
}
finally {
ResourceManager.close(rs);
ResourceManager.close(stmt);
if (!isConnSupplied) {
ResourceManager.close(conn);
}
}
}
/**
* Returns all rows from the Picture table that match the specified arbitrary SQL statement
*/
public Picture[] findByDynamicWhere(String sql, Object[] sqlParams) throws PictureDaoException
{
// 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 PictureDaoException( "SQLException: " + _e.getMessage(), _e );
}
catch (Exception _e) {
_e.printStackTrace();
throw new PictureDaoException( "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 + -