接口中定义的方法在抽象类中实现



今天在做一个dao工厂时遇到一个问题,就是对于数据库的连接到底在哪里关闭,在创建连接时是在dao父类中创建的,而关闭则不能在一个dao的方法中去完成,那样在使用这个dao实例的方法会报异常,看来还得为每个dao定义一个关闭连接的方法,而这个方法还得在接口中声明,,那样就得在每个实现中去实现一遍重复的代码了,试着把这个实现的代码提到一个抽象类中,在实现接口的子类中没有去覆写,也没有报错,嘿嘿,目的达到了,,原来实现一个接口又实现一个父类,接口中方法原来可以在继承的父类中实现。好吧贴一下代码。



dao接口:
package com.justsy.mdm.dao;

import java.sql.SQLException;

import com.justsy.mdm.permission.User;

public interface IUserDAO {

	public User login(String name, String password) throws SQLException;
	
	
	public void closeConnQuiet() ;
}


抽象父类:


package com.justsy.mdm.dao;

import java.sql.Connection;
import java.sql.SQLException;

import org.apache.log4j.Logger;

import com.justsy.mdm.util.DBManager;

public abstract class AbstractBaseDAO {

	protected final Logger logger = Logger.getLogger(getClass());

	protected final DBManager db = DBManager.getDbManager();

	protected Connection conn;

	public AbstractBaseDAO() throws SQLException, ClassNotFoundException {
		conn = db.createConn();
	}

	/**
	 * 关闭连接,不捕获异常
	 */
	public void closeConnQuiet() {
		if (conn != null) {
			try {
				conn.close() ;
				logger.debug("close connection success!!") ;
			} catch (SQLException e) {
				logger.warn("close connection fail"+e) ;
				//e.printStackTrace();
			}
		}
	}
}


userDao实现类,这个类中就没有覆写closeConnQuiet方法

package com.justsy.mdm.dao.impl;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.justsy.mdm.dao.AbstractBaseDAO;
import com.justsy.mdm.dao.IUserDAO;
import com.justsy.mdm.permission.Resource;
import com.justsy.mdm.permission.Role;
import com.justsy.mdm.permission.User;
import com.justsy.mdm.permission.comparator.Function;
import com.justsy.mdm.permission.comparator.FunctionFactory;

public class UserDAO extends AbstractBaseDAO implements IUserDAO {

	public UserDAO() throws SQLException, ClassNotFoundException {
		super();
	}

	@Override
	public User login(String name, String password) throws SQLException {
		logger.debug("user login username:" + name + ",password:" + password);

		String sql = "select * from user where name=? and password=?";

		PreparedStatement pstmt = db.prepare(conn, sql);
		ResultSet rs = db.getResult(pstmt, new Object[] { name, password });
		User user = null;
		while (rs.next()) {
			user = new User();
			int userId = rs.getInt(1);
			String userName = rs.getString("name");

			user.setId(userId);
			user.setName(userName);

			sql = "select * from role where id in (select roleid from user_role where userid=?)";

			PreparedStatement rolePstmt = db.prepare(conn, sql);
			ResultSet roleRs = db.getResult(rolePstmt, new Object[] { userId });
			Role role = null;
			while (roleRs.next()) {
				role = new Role();
				long roleId = roleRs.getLong("id");
				String roleName = roleRs.getString("name");
				role.setId(roleId);
				role.setName(roleName);
				// user中添加角色
				user.getRoles().add(role);

				// function
				sql = "select f.id funId,f.name funName,r.id resId,r.name resName from function f join resource r on f.resid = r.id and f.id in (select funid from role_function where roleid=?)";
				PreparedStatement funPstmt = db.prepare(conn, sql);
				ResultSet funRs = db.getResult(funPstmt,
						new Object[] { roleId });
				Function function = null;
				Resource resource = null;
				while (funRs.next()) {
					resource = new Resource();
					long resId = funRs.getLong("resId");
					String resName = funRs.getString("resName");
					resource.setId(resId);
					resource.setObject(resName);

					String funName = funRs.getString("funName");

					// 得到默认的操作
					function = FunctionFactory.getDefaultFunction(funName,
							resource);
					role.getFunctions().add(function);
				}
				db.close(funPstmt, funRs);
			}
			db.close(rolePstmt, roleRs);
		}
		// db.close(conn, pstmt, rs);
		db.close(pstmt, rs);

		return user;

	}

	// public static void main(String[] args) throws SQLException,
	// ClassNotFoundException {
	// UserDAO dao = new UserDAO();
	// dao.logger.debug("12") ;
	// System.out.println(dao.conn);
	// }

}




你可能感兴趣的:(Java,string,dao,function,user,login,object)