📄 usersdaoimpl.java
字号:
/*
*
* 开发时间:2004-08-20 at 05:40:07
*
* 作者:曹广鑫
*
* 网站:www.helpsoft.org 电子邮件:support@helpsoft.org
*/
package org.helpsoft.blog.jdbc;
import org.helpsoft.blog.dao.*;
import org.helpsoft.blog.dto.*;
import org.helpsoft.blog.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.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 UsersDaoImpl extends AbstractDataAccessObject implements UsersDao
{
/**
* 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( UsersDaoImpl.class );
/**
* All finder methods in this class use this SELECT constant to build their queries
*/
protected final String SQL_SELECT = "SELECT userid, password, name FROM " + getTableName() + " t0";
/**
* SQL INSERT statement for this table
*/
protected final String SQL_INSERT = "INSERT INTO " + getTableName() + " ( userid, password, name ) VALUES ( ?, ?, ? );SELECT @@IDENTITY";
/**
* SQL UPDATE statement for this table
*/
protected final String SQL_UPDATE = "UPDATE " + getTableName() + " SET userid = ?, password = ?, name = ? WHERE userid = ?";
/**
* SQL DELETE statement for this table
*/
protected final String SQL_DELETE = "DELETE FROM " + getTableName() + " WHERE userid = ?";
/**
* Index of column userid
*/
protected static final int COLUMN_USERID = 1;
/**
* Index of column password
*/
protected static final int COLUMN_PASSWORD = 2;
/**
* Index of column name
*/
protected static final int COLUMN_NAME = 3;
/**
* Number of columns
*/
protected static final int NUMBER_OF_COLUMNS = 3;
/**
* Index of primary-key column userid
*/
protected static final int PK_COLUMN_USERID = 1;
/**
* Inserts a new row in the users table.
*/
public UsersPk insert(Users dto) throws UsersDaoException
{
// 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.getPool().getConnection();
stmt = conn.prepareCall( SQL_INSERT );
stmt.setString( COLUMN_USERID, dto.getUserid() );
stmt.setString( COLUMN_PASSWORD, dto.getPassword() );
stmt.setString( COLUMN_NAME, dto.getName() );
if (logger.isDebugEnabled()) {
logger.debug( "Executing " + SQL_INSERT);
}
stmt.execute();
int rows = stmt.getUpdateCount();
if (logger.isDebugEnabled()) {
logger.debug( rows + " rows affected");
}
return dto.createPk();
}
catch (SQLException _e) {
logger.error( "SQLException: " + _e.getMessage(), _e );
throw new UsersDaoException( "SQLException: " + _e.getMessage(), _e );
}
catch (Exception _e) {
logger.error( "Exception: " + _e.getMessage(), _e );
throw new UsersDaoException( "Exception: " + _e.getMessage(), _e );
}
finally {
ResourceManager.close(stmt);
if (!isConnSupplied) {
ResourceManager.close(conn);
}
}
}
/**
* Updates a single row in the users table.
*/
public void update(UsersPk pk, Users dto) throws UsersDaoException
{
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.getPool().getConnection();
if (logger.isDebugEnabled()) {
logger.debug( "Executing " + SQL_UPDATE + " with DTO: " + dto);
}
stmt = conn.prepareStatement( SQL_UPDATE );
stmt.setString( COLUMN_USERID, dto.getUserid() );
stmt.setString( COLUMN_PASSWORD, dto.getPassword() );
stmt.setString( COLUMN_NAME, dto.getName() );
stmt.setString( 4, pk.getUserid() );
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 UsersDaoException( "SQLException: " + _e.getMessage(), _e );
}
catch (Exception _e) {
logger.error( "Exception: " + _e.getMessage(), _e );
throw new UsersDaoException( "Exception: " + _e.getMessage(), _e );
}
finally {
ResourceManager.close(stmt);
if (!isConnSupplied) {
ResourceManager.close(conn);
}
}
}
/**
* Deletes a single row in the users table.
*/
public void delete(UsersPk pk) throws UsersDaoException
{
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.getPool().getConnection();
if (logger.isDebugEnabled()) {
logger.debug( "Executing " + SQL_DELETE + " with PK: " + pk);
}
stmt = conn.prepareStatement( SQL_DELETE );
stmt.setString( 1, pk.getUserid() );
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 UsersDaoException( "SQLException: " + _e.getMessage(), _e );
}
catch (Exception _e) {
logger.error( "Exception: " + _e.getMessage(), _e );
throw new UsersDaoException( "Exception: " + _e.getMessage(), _e );
}
finally {
ResourceManager.close(stmt);
if (!isConnSupplied) {
ResourceManager.close(conn);
}
}
}
/**
* Returns the rows from the users table that matches the specified primary-key value.
*/
public Users findByPrimaryKey(UsersPk pk) throws UsersDaoException
{
return findByPrimaryKey( pk.getUserid() );
}
/**
* Returns all rows from the users table.
*/
public Users[] findAll() throws UsersDaoException
{
return findByDynamicSelect( SQL_SELECT + " ORDER BY userid", null );
}
/**
* Returns all rows from the users table that match the criteria 'userid = :userid'.
*/
public Users[] findWhereUseridEquals(String userid) throws UsersDaoException
{
return findByDynamicSelect( SQL_SELECT + " WHERE userid = ? ORDER BY userid", new Object[] { userid } );
}
/**
* Returns all rows from the users table that match the criteria 'name = :name'.
*/
public Users[] findWhereNameEquals(String name) throws UsersDaoException
{
return findByDynamicSelect( SQL_SELECT + " WHERE name = ? ORDER BY name", new Object[] { name } );
}
/**
* Returns all rows from the users table that match the criteria 'userid = :userid'.
*/
public Users findByPrimaryKey(String userid) throws UsersDaoException
{
Users ret[] = findByDynamicSelect( SQL_SELECT + " WHERE userid = ?", new Object[] { userid } );
return ret.length==0 ? null : ret[0];
}
/**
* Method 'UsersDaoImpl'
*
*/
public UsersDaoImpl()
{
}
/**
* Method 'UsersDaoImpl'
*
* @param userConn
*/
public UsersDaoImpl(final java.sql.Connection userConn)
{
this.userConn = userConn;
}
/**
* Method 'getTableName'
*
* @return String
*/
public String getTableName()
{
return "users";
}
/**
* Fetches a single row from the result set
*/
protected Users fetchSingleResult(ResultSet rs) throws SQLException
{
if (rs.next()) {
Users dto = new Users();
populateDto( dto, rs);
return dto;
} else {
return null;
}
}
/**
* Fetches multiple rows from the result set
*/
protected Users[] fetchMultiResults(ResultSet rs) throws SQLException
{
Collection resultList = new ArrayList();
while (rs.next()) {
Users dto = new Users();
populateDto( dto, rs);
resultList.add( dto );
}
Users ret[] = new Users[ resultList.size() ];
resultList.toArray( ret );
return ret;
}
/**
* Populates a DTO with data from a ResultSet
*/
protected void populateDto(Users dto, ResultSet rs) throws SQLException
{
dto.setUserid( rs.getString( COLUMN_USERID ) );
dto.setPassword( rs.getString( COLUMN_PASSWORD ) );
dto.setName( rs.getString( COLUMN_NAME ) );
}
/**
* Returns all rows from the users table that match the specified arbitrary SQL statement
*/
public Users[] findByDynamicSelect(String sql, Object[] sqlParams) throws UsersDaoException
{
// 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.getPool().getConnection();
// construct the SQL statement
final String SQL = sql;
if (logger.isDebugEnabled()) {
logger.debug( "Executing " + SQL);
}
// prepare statement
stmt = conn.prepareStatement( SQL );
// 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 UsersDaoException( "SQLException: " + _e.getMessage(), _e );
}
catch (Exception _e) {
logger.error( "Exception: " + _e.getMessage(), _e );
throw new UsersDaoException( "Exception: " + _e.getMessage(), _e );
}
finally {
ResourceManager.close(rs);
ResourceManager.close(stmt);
if (!isConnSupplied) {
ResourceManager.close(conn);
}
}
}
/**
* Returns all rows from the users table that match the specified arbitrary SQL statement
*/
public Users[] findByDynamicWhere(String sql, Object[] sqlParams) throws UsersDaoException
{
// 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.getPool().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 );
// 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 UsersDaoException( "SQLException: " + _e.getMessage(), _e );
}
catch (Exception _e) {
logger.error( "Exception: " + _e.getMessage(), _e );
throw new UsersDaoException( "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 + -