📄 frienddaoimpl.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.FriendDao;
import com.relationinfo.txl.dto.Friend;
import com.relationinfo.txl.dto.FriendPk;
import com.relationinfo.txl.exceptions.FriendDaoException;
public class FriendDaoImpl extends AbstractDataAccessObject implements FriendDao
{
protected java.sql.Connection userConn;
protected static Logger logger = Logger.getLogger( FriendDaoImpl.class );
/**
* All finder methods in this class use this SELECT constant to build their queries
*/
protected String SQL_SELECT = "SELECT xmjp, groupid, name, age, sex, Professional, Scholarship, address, email, phone, note 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() + " ( xmjp, groupid, name, age, sex, Professional, Scholarship, address, email, phone, note ) VALUES ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? )";
/**
* SQL UPDATE 语句
*/
protected String SQL_UPDATE = "UPDATE " + getTableName() + " SET xmjp = ?, groupid = ?, name = ?, age = ?, sex = ?, Professional = ?, Scholarship = ?, address = ?, email = ?, phone = ?, note = ? WHERE xmjp = ?";
/**
* SQL DELETE 语句
*/
protected String SQL_DELETE = "DELETE FROM " + getTableName() + " WHERE xmjp = ?";
/**
* 序号列 xmjp
*/
protected static int COLUMN_XMJP = 1;
/**
* 序号列 groupid
*/
protected static int COLUMN_GROUPID = 2;
/**
* 序号列 name
*/
protected static int COLUMN_NAME = 3;
/**
* 序号列 age
*/
protected static int COLUMN_AGE = 4;
/**
* 序号列 sex
*/
protected static int COLUMN_SEX = 5;
/**
* 序号列 Professional
*/
protected static int COLUMN_PROFESSIONAL = 6;
/**
* 序号列 Scholarship
*/
protected static int COLUMN_SCHOLARSHIP = 7;
/**
* 序号列 address
*/
protected static int COLUMN_ADDRESS = 8;
/**
* 序号列 email
*/
protected static int COLUMN_EMAIL = 9;
/**
* 序号列 phone
*/
protected static int COLUMN_PHONE = 10;
/**
* 序号列 note
*/
protected static int COLUMN_NOTE = 11;
/**
* 列的数量
*/
protected static int NUMBER_OF_COLUMNS = 11;
/**
* 主键序号列 xmjp
*/
protected static int PK_COLUMN_XMJP = 1;
/**
* 增加新记录到 friend table.
*/
public FriendPk insert(Friend dto) throws FriendDaoException
{
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_XMJP, dto.getXmjp() );
stmt.setString( COLUMN_GROUPID, dto.getGroupid() );
stmt.setString( COLUMN_NAME, dto.getName() );
stmt.setString( COLUMN_AGE, dto.getAge() );
stmt.setString( COLUMN_SEX, dto.getSex() );
stmt.setString( COLUMN_PROFESSIONAL, dto.getProfessional() );
stmt.setString( COLUMN_SCHOLARSHIP, dto.getScholarship() );
stmt.setString( COLUMN_ADDRESS, dto.getAddress() );
stmt.setString( COLUMN_EMAIL, dto.getEmail() );
stmt.setString( COLUMN_PHONE, dto.getPhone() );
stmt.setString( COLUMN_NOTE, dto.getNote() );
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 FriendDaoException( "SQLException: " + _e.getMessage(), _e );
}
catch (Exception _e) {
logger.error( "Exception: " + _e.getMessage(), _e );
throw new FriendDaoException( "Exception: " + _e.getMessage(), _e );
}
finally {
ResourceManager.close(stmt);
if (!isConnSupplied) {
ResourceManager.close(conn);
}
}
}
/**
* 更新单笔记录.
*/
public void update(FriendPk pk, Friend dto) throws FriendDaoException
{
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.getXmjp() );
stmt.setString( index++, dto.getGroupid() );
stmt.setString( index++, dto.getName() );
stmt.setString( index++, dto.getAge() );
stmt.setString( index++, dto.getSex() );
stmt.setString( index++, dto.getProfessional() );
stmt.setString( index++, dto.getScholarship() );
stmt.setString( index++, dto.getAddress() );
stmt.setString( index++, dto.getEmail() );
stmt.setString( index++, dto.getPhone() );
stmt.setString( index++, dto.getNote() );
stmt.setString( 12, pk.getXmjp() );
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 FriendDaoException( "SQLException: " + _e.getMessage(), _e );
}
catch (Exception _e) {
logger.error( "Exception: " + _e.getMessage(), _e );
throw new FriendDaoException( "Exception: " + _e.getMessage(), _e );
}
finally {
ResourceManager.close(stmt);
if (!isConnSupplied) {
ResourceManager.close(conn);
}
}
}
/**
* 删除记录.
*/
public void delete(FriendPk pk) throws FriendDaoException
{
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.getXmjp() );
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 FriendDaoException( "SQLException: " + _e.getMessage(), _e );
}
catch (Exception _e) {
logger.error( "Exception: " + _e.getMessage(), _e );
throw new FriendDaoException( "Exception: " + _e.getMessage(), _e );
}
finally {
ResourceManager.close(stmt);
if (!isConnSupplied) {
ResourceManager.close(conn);
}
}
}
/**
* 返回符合指定主键值的记录.
*/
public Friend findByPrimaryKey(FriendPk pk) throws FriendDaoException
{
return findByPrimaryKey( pk.getXmjp() );
}
/**
* 返回所有的记录,满足条件 'xmjp = :xmjp'.
*/
public Friend findByPrimaryKey(String xmjp) throws FriendDaoException
{
Friend ret[] = findByDynamicSelect( SQL_SELECT + " WHERE xmjp = ?", new Object[] { xmjp } );
return ret.length==0 ? null : ret[0];
}
/**
* 返回所有的记录,满足条件 ''.
*/
public Friend[] findAll() throws FriendDaoException
{
return findByDynamicSelect( SQL_SELECT + " ORDER BY xmjp", null );
}
/**
* 返回所有的记录,满足条件 'groupid = :groupid'.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -