📄 blogdaoimpl.java
字号:
/*
* This source file was generated by FireStorm/DAO 3.0 (build 99)
* on 26-十月-2005 at 09:50:56
*
* If you purchase a full license for FireStorm/DAO you can customize this file header.
*
* For more information please visit http://www.codefutures.com/products/firestorm
*/
package cn.wanfeng.myblog.jdbc;
import cn.wanfeng.myblog.dao.*;
import cn.wanfeng.myblog.factory.*;
import cn.wanfeng.myblog.dto.*;
import cn.wanfeng.myblog.exceptions.*;
import java.sql.Connection;
import java.sql.Types;
import java.util.Collection;
import java.sql.PreparedStatement;
import java.sql.Statement;
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 BlogDaoImpl extends AbstractDataAccessObject implements BlogDao
{
/**
* 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;
/**
* All finder methods in this class use this SELECT constant to build their queries
*/
protected final String SQL_SELECT = "SELECT BlogID, Username, Password, Subject, Description, Email FROM " + getTableName() + "";
/**
* Finder methods will pass this value to the JDBC setMaxRows method
*/
private int maxRows;
/**
* SQL INSERT statement for this table
*/
protected final String SQL_INSERT = "INSERT INTO " + getTableName() + " ( Username, Password, Subject, Description, Email ) VALUES ( ?, ?, ?, ?, ? );SELECT @@IDENTITY";
/**
* SQL UPDATE statement for this table
*/
protected final String SQL_UPDATE = "UPDATE " + getTableName() + " SET Username = ?, Password = ?, Subject = ?, Description = ?, Email = ? WHERE BlogID = ?";
/**
* SQL DELETE statement for this table
*/
protected final String SQL_DELETE = "DELETE FROM " + getTableName() + " WHERE BlogID = ?";
/**
* Index of column BlogID
*/
protected static final int COLUMN_BLOG_I_D = 1;
/**
* Index of column Username
*/
protected static final int COLUMN_USERNAME = 2;
/**
* Index of column Password
*/
protected static final int COLUMN_PASSWORD = 3;
/**
* Index of column Subject
*/
protected static final int COLUMN_SUBJECT = 4;
/**
* Index of column Description
*/
protected static final int COLUMN_DESCRIPTION = 5;
/**
* Index of column Email
*/
protected static final int COLUMN_EMAIL = 6;
/**
* Number of columns
*/
protected static final int NUMBER_OF_COLUMNS = 6;
/**
* Index of primary-key column BlogID
*/
protected static final int PK_COLUMN_BLOG_I_D = 1;
/**
* Inserts a new row in the Blog table.
*/
public BlogPk insert(Blog dto) throws BlogDaoException
{
long t1 = System.currentTimeMillis();
// 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.getConnection();
stmt = conn.prepareCall( SQL_INSERT );
// no bind statement for auto increment column 'BlogID
stmt.setString( COLUMN_USERNAME - 1, dto.getUsername() );
stmt.setString( COLUMN_PASSWORD - 1, dto.getPassword() );
stmt.setString( COLUMN_SUBJECT - 1, dto.getSubject() );
stmt.setString( COLUMN_DESCRIPTION - 1, dto.getDescription() );
stmt.setString( COLUMN_EMAIL - 1, dto.getEmail() );
System.out.println( "Executing " + SQL_INSERT + " with DTO: " + dto );
stmt.execute();
int rows = stmt.getUpdateCount();
System.out.println( rows + " rows affected" );
// retrieve values from IDENTITY columns
boolean moreResults = true;
while (moreResults || rows != -1) {
try {
rs = stmt.getResultSet();
}
catch (IllegalArgumentException e) {
e.printStackTrace();
}
if (rs != null) {
rs.next();
dto.setBlogID( rs.getInt( 1 ) );
}
moreResults = stmt.getMoreResults();
rows = stmt.getUpdateCount();
}
return dto.createPk();
}
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);
}
}
}
/**
* Updates a single row in the Blog table.
*/
public void update(BlogPk pk, Blog dto) throws BlogDaoException
{
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.getConnection();
StringBuffer sql = new StringBuffer();
sql.append( "UPDATE " + getTableName() + " SET " );
boolean modified = false;
if (dto.isBlogIDModified()) {
if (modified) {
sql.append( ", " );
}
sql.append( "BlogID=?" );
modified=true;
}
if (dto.isUsernameModified()) {
if (modified) {
sql.append( ", " );
}
sql.append( "Username=?" );
modified=true;
}
if (dto.isPasswordModified()) {
if (modified) {
sql.append( ", " );
}
sql.append( "Password=?" );
modified=true;
}
if (dto.isSubjectModified()) {
if (modified) {
sql.append( ", " );
}
sql.append( "Subject=?" );
modified=true;
}
if (dto.isDescriptionModified()) {
if (modified) {
sql.append( ", " );
}
sql.append( "Description=?" );
modified=true;
}
if (dto.isEmailModified()) {
if (modified) {
sql.append( ", " );
}
sql.append( "Email=?" );
modified=true;
}
if (!modified) {
// nothing to update
return;
}
sql.append( " WHERE BlogID=?" );
System.out.println( "Executing " + sql.toString() + " with values: " + dto );
stmt = conn.prepareStatement( sql.toString() );
int index = 1;
if (dto.isBlogIDModified()) {
stmt.setInt( index++, dto.getBlogID() );
}
if (dto.isUsernameModified()) {
stmt.setString( index++, dto.getUsername() );
}
if (dto.isPasswordModified()) {
stmt.setString( index++, dto.getPassword() );
}
if (dto.isSubjectModified()) {
stmt.setString( index++, dto.getSubject() );
}
if (dto.isDescriptionModified()) {
stmt.setString( index++, dto.getDescription() );
}
if (dto.isEmailModified()) {
stmt.setString( index++, dto.getEmail() );
}
stmt.setInt( index++, 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);
}
}
}
/**
* Deletes a single row in the Blog table.
*/
public void delete(BlogPk pk) throws BlogDaoException
{
long t1 = System.currentTimeMillis();
// declare variables
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -