📄 blogdaoimpl.java
字号:
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.getBlogID() );
int rows = stmt.executeUpdate();
long t2 = System.currentTimeMillis();
System.out.println( rows + " rows affected (" + (t2-t1) + " ms)" );
}
catch (SQLException _e) {
_e.printStackTrace();
throw new BlogDaoException( "SQLException: " + _e.getMessage(), _e );
}
catch (Exception _e) {
_e.printStackTrace();
throw new BlogDaoException( "Exception: " + _e.getMessage(), _e );
}
finally {
ResourceManager.close(stmt);
if (!isConnSupplied) {
ResourceManager.close(conn);
}
}
}
/**
* Returns the rows from the Blog table that matches the specified primary-key value.
*/
public Blog findByPrimaryKey(BlogPk pk) throws BlogDaoException
{
return findByPrimaryKey( pk.getBlogID() );
}
/**
* Returns all rows from the Blog table that match the criteria ''.
*/
public Blog[] findAll() throws BlogDaoException
{
return findByDynamicSelect( SQL_SELECT + " ORDER BY BlogID", null );
}
/**
* Returns all rows from the Blog table that match the criteria 'BlogID = :blogID'.
*/
public Blog findByPrimaryKey(int blogID) throws BlogDaoException
{
Blog ret[] = findByDynamicSelect( SQL_SELECT + " WHERE BlogID = ?", new Object[] { new Integer(blogID) } );
return ret.length==0 ? null : ret[0];
}
/**
* Returns all rows from the Blog table that match the criteria 'BlogID = :blogID'.
*/
public Blog[] findWhereBlogIDEquals(int blogID) throws BlogDaoException
{
return findByDynamicSelect( SQL_SELECT + " WHERE BlogID = ? ORDER BY BlogID", new Object[] { new Integer(blogID) } );
}
/**
* Returns all rows from the Blog table that match the criteria 'Username = :username'.
*/
public Blog[] findWhereUsernameEquals(String username) throws BlogDaoException
{
return findByDynamicSelect( SQL_SELECT + " WHERE Username = ? ORDER BY Username", new Object[] { username } );
}
/**
* Returns all rows from the Blog table that match the criteria 'Password = :password'.
*/
public Blog[] findWherePasswordEquals(String password) throws BlogDaoException
{
return findByDynamicSelect( SQL_SELECT + " WHERE Password = ? ORDER BY Password", new Object[] { password } );
}
/**
* Returns all rows from the Blog table that match the criteria 'Subject = :subject'.
*/
public Blog[] findWhereSubjectEquals(String subject) throws BlogDaoException
{
return findByDynamicSelect( SQL_SELECT + " WHERE Subject = ? ORDER BY Subject", new Object[] { subject } );
}
/**
* Returns all rows from the Blog table that match the criteria 'Description = :description'.
*/
public Blog[] findWhereDescriptionEquals(String description) throws BlogDaoException
{
return findByDynamicSelect( SQL_SELECT + " WHERE Description = ? ORDER BY Description", new Object[] { description } );
}
/**
* Returns all rows from the Blog table that match the criteria 'Email = :email'.
*/
public Blog[] findWhereEmailEquals(String email) throws BlogDaoException
{
return findByDynamicSelect( SQL_SELECT + " WHERE Email = ? ORDER BY Email", new Object[] { email } );
}
/**
* Method 'BlogDaoImpl'
*
*/
public BlogDaoImpl()
{
}
/**
* Method 'BlogDaoImpl'
*
* @param userConn
*/
public BlogDaoImpl(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..Blog";
}
/**
* Fetches a single row from the result set
*/
protected Blog fetchSingleResult(ResultSet rs) throws SQLException
{
if (rs.next()) {
Blog dto = new Blog();
populateDto( dto, rs);
return dto;
} else {
return null;
}
}
/**
* Fetches multiple rows from the result set
*/
protected Blog[] fetchMultiResults(ResultSet rs) throws SQLException
{
Collection resultList = new ArrayList();
while (rs.next()) {
Blog dto = new Blog();
populateDto( dto, rs);
resultList.add( dto );
}
Blog ret[] = new Blog[ resultList.size() ];
resultList.toArray( ret );
return ret;
}
/**
* Populates a DTO with data from a ResultSet
*/
protected void populateDto(Blog dto, ResultSet rs) throws SQLException
{
dto.setBlogID( rs.getInt( COLUMN_BLOG_I_D ) );
dto.setUsername( rs.getString( COLUMN_USERNAME ) );
dto.setPassword( rs.getString( COLUMN_PASSWORD ) );
dto.setSubject( rs.getString( COLUMN_SUBJECT ) );
dto.setDescription( rs.getString( COLUMN_DESCRIPTION ) );
dto.setEmail( rs.getString( COLUMN_EMAIL ) );
}
/**
* Returns all rows from the Blog table that match the specified arbitrary SQL statement
*/
public Blog[] findByDynamicSelect(String sql, Object[] sqlParams) throws BlogDaoException
{
// 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 BlogDaoException( "SQLException: " + _e.getMessage(), _e );
}
catch (Exception _e) {
_e.printStackTrace();
throw new BlogDaoException( "Exception: " + _e.getMessage(), _e );
}
finally {
ResourceManager.close(rs);
ResourceManager.close(stmt);
if (!isConnSupplied) {
ResourceManager.close(conn);
}
}
}
/**
* Returns all rows from the Blog table that match the specified arbitrary SQL statement
*/
public Blog[] findByDynamicWhere(String sql, Object[] sqlParams) throws BlogDaoException
{
// 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 BlogDaoException( "SQLException: " + _e.getMessage(), _e );
}
catch (Exception _e) {
_e.printStackTrace();
throw new BlogDaoException( "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 + -