📄 group1daoimpl.java
字号:
package com.relationinfo.txl.jdbc;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import org.apache.log4j.Logger;
import com.relationinfo.txl.dao.Group1Dao;
import com.relationinfo.txl.dto.Group1;
import com.relationinfo.txl.dto.Group1Pk;
import com.relationinfo.txl.exceptions.Group1DaoException;
public class Group1DaoImpl extends AbstractDataAccessObject implements Group1Dao
{
/**
* 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;
protected static Logger logger = Logger.getLogger( Group1DaoImpl.class );
/**
* All finder methods in this class use this SELECT constant to build their queries
*/
protected String SQL_SELECT = "SELECT groupid, groupname FROM " + getTableName() + "";
/**
* Finder methods will pass this value to the JDBC setMaxRows method
*/
private int maxRows;
/**
* SQL INSERT 语句
*/
protected String SQL_INSERT = "INSERT INTO " + getTableName() + " ( groupid, groupname ) VALUES ( ?, ? )";
/**
* SQL UPDATE 语句
*/
protected String SQL_UPDATE = "UPDATE " + getTableName() + " SET groupid = ?, groupname = ? WHERE groupid = ?";
/**
* SQL DELETE 语句
*/
protected String SQL_DELETE = "DELETE FROM " + getTableName() + " WHERE groupid = ?";
/**
* 序号列 groupid
*/
protected static int COLUMN_GROUPID = 1;
/**
* 序号列 groupname
*/
protected static int COLUMN_GROUPNAME = 2;
/**
* Number of columns
*/
protected static int NUMBER_OF_COLUMNS = 2;
/**
* Index of primary-key column groupid
*/
protected static int PK_COLUMN_GROUPID = 1;
/**
* 增加新记录到 group1 table.
*/
public Group1Pk insert(Group1 dto) throws Group1DaoException
{
long t1 = System.currentTimeMillis();
//声明参数
boolean isConnSupplied = (userConn != null);
Connection conn = null;
CallableStatement stmt = null;
ResultSet rs = null;
try {
// 获得用户指定的connection 或者从ResourceManager获得连接
conn = isConnSupplied ? userConn : ResourceManager.getConnection();
stmt = conn.prepareCall( SQL_INSERT );
stmt.setString( COLUMN_GROUPID, dto.getGroupid() );
stmt.setString( COLUMN_GROUPNAME, dto.getGroupname() );
if (logger.isDebugEnabled()) {
logger.debug( "Executing " + SQL_INSERT + " with DTO: " + dto);
}
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 Group1DaoException( "SQLException: " + _e.getMessage(), _e );
}
catch (Exception _e) {
logger.error( "Exception: " + _e.getMessage(), _e );
throw new Group1DaoException( "Exception: " + _e.getMessage(), _e );
}
finally {
ResourceManager.close(stmt);
if (!isConnSupplied) {
ResourceManager.close(conn);
}
}
}
/**
* 更新单笔记录 group1 table.
*/
public void update(Group1Pk pk, Group1 dto) throws Group1DaoException
{
long t1 = System.currentTimeMillis();
//声明参数
boolean isConnSupplied = (userConn != null);
Connection conn = null;
PreparedStatement stmt = null;
try {
// 获得用户指定的connection 或者从ResourceManager获得连接
conn = isConnSupplied ? userConn : ResourceManager.getConnection();
if (logger.isDebugEnabled()) {
logger.debug( "Executing " + SQL_UPDATE + " with DTO: " + dto);
}
stmt = conn.prepareStatement( SQL_UPDATE );
int index=1;
stmt.setString( index++, dto.getGroupid() );
stmt.setString( index++, dto.getGroupname() );
stmt.setString( 3, pk.getGroupid() );
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 Group1DaoException( "SQLException: " + _e.getMessage(), _e );
}
catch (Exception _e) {
logger.error( "Exception: " + _e.getMessage(), _e );
throw new Group1DaoException( "Exception: " + _e.getMessage(), _e );
}
finally {
ResourceManager.close(stmt);
if (!isConnSupplied) {
ResourceManager.close(conn);
}
}
}
/**
* Deletes a single row in the group1 table.
*/
public void delete(Group1Pk pk) throws Group1DaoException
{
long t1 = System.currentTimeMillis();
//声明参数
boolean isConnSupplied = (userConn != null);
Connection conn = null;
PreparedStatement stmt = null;
try {
// 获得用户指定的connection 或者从ResourceManager获得连接
conn = isConnSupplied ? userConn : ResourceManager.getConnection();
if (logger.isDebugEnabled()) {
logger.debug( "Executing " + SQL_DELETE + " with PK: " + pk);
}
stmt = conn.prepareStatement( SQL_DELETE );
stmt.setString( 1, pk.getGroupid() );
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 Group1DaoException( "SQLException: " + _e.getMessage(), _e );
}
catch (Exception _e) {
logger.error( "Exception: " + _e.getMessage(), _e );
throw new Group1DaoException( "Exception: " + _e.getMessage(), _e );
}
finally {
ResourceManager.close(stmt);
if (!isConnSupplied) {
ResourceManager.close(conn);
}
}
}
/**
* Returns the rows from the group1 table that matches the specified primary-key value.
*/
public Group1 findByPrimaryKey(Group1Pk pk) throws Group1DaoException
{
return findByPrimaryKey( pk.getGroupid() );
}
/**
* Returns all rows from the group1 table that match the criteria 'groupid = :groupid'.
*/
public Group1 findByPrimaryKey(String groupid) throws Group1DaoException
{
Group1 ret[] = findByDynamicSelect( SQL_SELECT + " WHERE groupid = ?", new Object[] { groupid } );
return ret.length==0 ? null : ret[0];
}
/**
* Returns all rows from the group1 table that match the criteria ''.
*/
public Group1[] findAll() throws Group1DaoException
{
return findByDynamicSelect( SQL_SELECT + " ORDER BY groupid", null );
}
/**
* Returns all rows from the group1 table that match the criteria 'groupid = :groupid'.
*/
public Group1[] findWhereGroupidEquals(String groupid) throws Group1DaoException
{
return findByDynamicSelect( SQL_SELECT + " WHERE groupid = ? ORDER BY groupid", new Object[] { groupid } );
}
/**
* Returns all rows from the group1 table that match the criteria 'groupname = :groupname'.
*/
public Group1[] findWhereGroupnameEquals(String groupname) throws Group1DaoException
{
return findByDynamicSelect( SQL_SELECT + " WHERE groupname = ? ORDER BY groupname", new Object[] { groupname } );
}
/**
* Method 'Group1DaoImpl'
*
*/
public Group1DaoImpl()
{
}
/**
* Method 'Group1DaoImpl'
*
* @param userConn
*/
public Group1DaoImpl( java.sql.Connection userConn)
{
this.userConn = userConn;
}
/**
* 设置 maxRows
*/
public void setMaxRows(int maxRows)
{
this.maxRows = maxRows;
}
/**
* 获得 maxRows
*/
public int getMaxRows()
{
return maxRows;
}
/**
* Method 'getTableName'
*
* @return String
*/
public String getTableName()
{
return "group1";
}
/**
* 从结果集中获得单行
*/
protected Group1 fetchSingleResult(ResultSet rs) throws SQLException
{
if (rs.next()) {
Group1 dto = new Group1();
populateDto( dto, rs);
return dto;
} else {
return null;
}
}
/**
* 从结果集中获得多行
*/
protected Group1[] fetchMultiResults(ResultSet rs) throws SQLException
{
Collection resultList = new ArrayList();
while (rs.next()) {
Group1 dto = new Group1();
populateDto( dto, rs);
resultList.add( dto );
}
Group1 ret[] = new Group1[ resultList.size() ];
resultList.toArray( ret );
return ret;
}
/**
* 将结果集中的数据复制给DTO
*/
protected void populateDto(Group1 dto, ResultSet rs) throws SQLException
{
dto.setGroupid( rs.getString( COLUMN_GROUPID ) );
dto.setGroupname( rs.getString( COLUMN_GROUPNAME ) );
}
/**
* Returns all rows from the group1 table that match the specified arbitrary SQL statement
*/
public Group1[] findByDynamicSelect(String sql, Object[] sqlParams) throws Group1DaoException
{
//声明参数
boolean isConnSupplied = (userConn != null);
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
try {
// 获得用户指定的connection 或者从ResourceManager获得连接
conn = isConnSupplied ? userConn : ResourceManager.getConnection();
// construct the SQL statement
String SQL = sql;
if (logger.isDebugEnabled()) {
logger.debug( "Executing " + SQL);
}
//准备 statement
stmt = conn.prepareStatement( SQL );
stmt.setMaxRows( maxRows );
//绑定参数
for (int i=0; sqlParams!=null && i<sqlParams.length; i++ ) {
stmt.setObject( i+1, sqlParams[i] );
}
rs = stmt.executeQuery();
//获得结果
return fetchMultiResults(rs);
}
catch (SQLException _e) {
logger.error( "SQLException: " + _e.getMessage(), _e );
throw new Group1DaoException( "SQLException: " + _e.getMessage(), _e );
}
catch (Exception _e) {
logger.error( "Exception: " + _e.getMessage(), _e );
throw new Group1DaoException( "Exception: " + _e.getMessage(), _e );
}
finally {
ResourceManager.close(rs);
ResourceManager.close(stmt);
if (!isConnSupplied) {
ResourceManager.close(conn);
}
}
}
/**
* Returns all rows from the group1 table that match the specified arbitrary SQL statement
*/
public Group1[] findByDynamicWhere(String sql, Object[] sqlParams) throws Group1DaoException
{
//声明参数
boolean isConnSupplied = (userConn != null);
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
try {
// 获得用户指定的connection 或者从ResourceManager获得连接
conn = isConnSupplied ? userConn : ResourceManager.getConnection();
// construct the SQL statement
String SQL = SQL_SELECT + " WHERE " + sql;
if (logger.isDebugEnabled()) {
logger.debug( "Executing " + SQL);
}
//准备 statement
stmt = conn.prepareStatement( SQL );
stmt.setMaxRows( maxRows );
//绑定参数
for (int i=0; sqlParams!=null && i<sqlParams.length; i++ ) {
stmt.setObject( i+1, sqlParams[i] );
}
rs = stmt.executeQuery();
//获得结果
return fetchMultiResults(rs);
}
catch (SQLException _e) {
logger.error( "SQLException: " + _e.getMessage(), _e );
throw new Group1DaoException( "SQLException: " + _e.getMessage(), _e );
}
catch (Exception _e) {
logger.error( "Exception: " + _e.getMessage(), _e );
throw new Group1DaoException( "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 + -