⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 accountdaomysql.java

📁 一个功能较为完善的论坛
💻 JAVA
字号:
/*
 * XP Forum
 *
 * Copyright (c) 2002-2003 RedSoft Group.  All rights reserved.
 *
 */
package org.redsoft.forum.dao.mysql;

/**
 * AccountDAO's implmentation for Mysql
 *
 * @author Charles Huang
 * @version 1.0
 */
import java.sql.Connection;
import java.sql.Statement;
import java.sql.PreparedStatement;
import java.sql.DriverManager;
import java.sql.SQLException;
import org.redsoft.forum.web.Account;
import java.util.Collection;
import org.redsoft.forum.exception.AccountNotFoundException;
import org.redsoft.forum.exception.AccountAlreadyExistException;
import java.sql.ResultSet;
import org.redsoft.forum.util.Validation;
import org.redsoft.forum.fixture.MysqlFixture;
import org.redsoft.forum.dao.AccountDAO;


public class AccountDAOmySql implements AccountDAO{

    public AccountDAOmySql() {
	}


	/**
     *  Get a db connection
     *
     * @return Connection - DB connection
     */
    public Connection getConnection() throws SQLException {
		return MysqlDAOFactory.getConnection();
	}


    /**
     *  Add a user account
     *
     * @param Account - A account object that contains the user info,like userName,
     *                  password,email
     */
    public void addAccount( final Account account )
                                            throws SQLException,
                                            	   AccountAlreadyExistException{
        Validation.validateNotNull( account );
        try{
			findByUserName( account.getUserName() );
		}catch( final AccountNotFoundException accountNotFoundException ){
		        final Connection conn = getConnection();
        		final PreparedStatement stat
        		    = conn.prepareStatement("insert into "
                                    + Account.PROPERTY_ACCOUNT_TABLE
                                    + " ( "
                                    + Account.PROPERTY_USER_NAME + ","
                                    + Account.PROPERTY_PASSWORD + ","
                                    + Account.PROPERTY_EMAIL + ")"
                                    + " values( '"
                                    + account.getUserName() + "', '"
                                    + account.getPassword() + "', '"
                                    + account.getEmail() + "')");

        		stat.execute();
        		stat.close();
		        conn.close();
		        return;
		}

		throw new AccountAlreadyExistException();
    }

    /**
     *  update a user account
     *
     * @param Account - A account object that contains the user info,like userName,
     *                  password,email
     */
    public void updateAccount( final Account account )
                                            throws SQLException,
                                            	   AccountNotFoundException{
        Validation.validateNotNull( account );

		findByUserName( account.getUserName() );

        final Connection conn = getConnection();
        String statement = "update "
                            + Account.PROPERTY_ACCOUNT_TABLE
                            + " set "
                            + Account.PROPERTY_PASSWORD + "='"
                            + account.getPassword() + "', "
                            + Account.PROPERTY_EMAIL + "='"
                            + account.getEmail() + "' where "
                            + Account.PROPERTY_USER_NAME + "='"
                            + account.getUserName() + "'";
		final PreparedStatement stat = conn.prepareStatement(statement);
		stat.execute();
		stat.close();
    	conn.close();
    }

    /**
     *  Find a user given a user name
     *
     * @param String - The user name
     * @return Account - A account object that contains the user info
     */
    public Account findByUserName( final String userName )
                                            throws SQLException,
                                                   AccountNotFoundException{
        Account account = null;
        Validation.validateNotNull( userName );
        final Connection conn = getConnection();
        final PreparedStatement stat
            = conn.prepareStatement("select "
                                    + Account.PROPERTY_USER_NAME + ","
                                    + Account.PROPERTY_PASSWORD + ","
                                    + Account.PROPERTY_EMAIL + " from "
                                    + Account.PROPERTY_ACCOUNT_TABLE
                                    + " where "
                                    + Account.PROPERTY_USER_NAME
                                    + "='" + userName + "'");
        final ResultSet resultSet = stat.executeQuery();
        if( resultSet.next() ){
            account = new Account( resultSet.getString( Account.PROPERTY_USER_NAME ),
                                   resultSet.getString( Account.PROPERTY_PASSWORD ),
                                   resultSet.getString( Account.PROPERTY_EMAIL ) );
        }
        try{
            resultSet.close();
            stat.close();
            conn.close();
        }catch( final Exception e ){
            e.printStackTrace();
        }
        if( account == null ){
            throw new AccountNotFoundException();
        }
        return account;
    }

    /**
     *  Remove a account given a user name
     *
     *  @param String - User name
     */
    public void removeAccount( final String userName ) throws SQLException{
        Validation.validateNotNull( userName );
        final Connection conn = getConnection();
        final PreparedStatement stat
            = conn.prepareStatement("delete from " + Account.PROPERTY_ACCOUNT_TABLE
                                    + " where " + Account.PROPERTY_USER_NAME
                                    + "='" + userName + "'");
        stat.execute();
        try{
            stat.close();
            conn.close();
        }catch( final SQLException sqlException ){
            sqlException.printStackTrace();
        }

    }
}//EOC


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -