JSP

47 강을 리팩토링 

 

interface RowMapper<T>

package net.slipp.user;

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

public interface RowMapper<T> {
	T mapRow(ResultSet rs) throws SQLException ;
}

 

interface PreparedStatementSetter

package net.slipp.user;

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

public interface PreparedStatementSetter {
	void setParameters(PreparedStatement pstmt) throws SQLException;
}

 

class UserDAO

package net.slipp.user;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import net.slipp.support.JdbcTemplate;

public class UserDAO {

	public void addUser(User user) throws SQLException {
		String sql = "insert into USERS values(?,?,?,?)";
		JdbcTemplate.getInstance().executeUpdate(sql, user.getUserId(), user.getPassword(), user.getName(), user.getEmail());			
	}
	
	public User findByUserId(String userId) throws SQLException {
		RowMapper<User> row = rowMap();		
		String sql = "select * from USERS where userId = ?";
		return JdbcTemplate.getInstance().executeQuery(sql, row , userId );		
	}
	

	public void removeUser(String userId) throws SQLException {
		String sql = "delete from USERS where userId = ?";
		JdbcTemplate.getInstance().executeUpdate(sql,userId);
	}

	
	public void updateUser(User user) throws SQLException {
		String sql = "update USERS set password = ?, name = ?, email = ? where userId = ?";
		JdbcTemplate.getInstance().executeUpdate(sql,user.getPassword(), user.getName(), user.getEmail(), user.getUserId());
	}


	public List<User> findUsers() throws SQLException {
		RowMapper<User> row = rowMap();		
		String sql = "select * from USERS ";
		return JdbcTemplate.getInstance().list(sql, row, null );	
	}


	private RowMapper<User> rowMap() {
		RowMapper<User> row =new RowMapper<User>() {	
			@Override
			public User mapRow(ResultSet rs) throws SQLException {
				return new User(
						rs.getString("userId"), 
						rs.getString("password"),
						rs.getString("name"),
						rs.getString("email"));
			}
		};
		return row;
	}
	
	
	
	
	
}


 

class JdbcTemplate

package net.slipp.support;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import net.slipp.user.PreparedStatementSetter;
import net.slipp.user.RowMapper;

public class JdbcTemplate {

	private static JdbcTemplate instance;
	private JdbcTemplate() {}
	public static JdbcTemplate getInstance(){
		if(instance==null) instance =new JdbcTemplate();
		return instance;
	}
	
	public void executeUpdate(String sql, PreparedStatementSetter set)throws SQLException {		
		Connection conn = null;
		PreparedStatement pstmt = null;
		try {
			conn = ConnectionManager.getConnection();
			pstmt = conn.prepareStatement(sql); 
			set.setParameters(pstmt);
			pstmt.executeUpdate();
		} finally {
			if (pstmt != null)pstmt.close(); 
			if (conn != null) conn.close();
		}	
	}
	
	
	public void executeUpdate(String sql, Object... parameters )throws SQLException {
		executeUpdate(sql, createPreparedStatementSetter(parameters));
	}

	public <T> T executeQuery(String sql, RowMapper<T> rm , PreparedStatementSetter pss  ) throws SQLException {				
			List<T> list =list(sql, rm, pss);
			if(list.isEmpty()) return null;
			return list.get(0);
	}
	

	public <T> List<T> list(String sql, RowMapper<T> rm , PreparedStatementSetter pss ) throws SQLException {		
			Connection conn = null;
			PreparedStatement pstmt = null;
			ResultSet rs = null;
			try {
				conn = ConnectionManager.getConnection();
				pstmt = conn.prepareStatement(sql); 
		
				if(pss!=null)pss.setParameters(pstmt);
				rs = pstmt.executeQuery();
				
				List<T> list =new ArrayList<>();
				while(rs.next()){
					list.add(rm.mapRow(rs));
				}
				return list;
			} finally {
				if (rs != null) rs.close();
				if (pstmt != null) pstmt.close();
				if (conn != null) conn.close();
			}
	}
	
	
	
	
	public <T> T executeQuery(String sql, RowMapper<T> row,  Object... parameters) throws SQLException {
		return executeQuery(sql, row, createPreparedStatementSetter(parameters));
	}

	
	private PreparedStatementSetter createPreparedStatementSetter(Object... parameters) {
		PreparedStatementSetter pss =new PreparedStatementSetter() {			
			@Override
			public void setParameters(PreparedStatement pstmt) throws SQLException {		
				for(int i=0; i<parameters.length; i++){
					pstmt.setObject(i+1, parameters[i]);
				}
			}
		};
		return pss;
	}
	
	
	
}

 

 

 

 

 

 

 

 

 

about author

PHRASE

Level 60  머나먼나라

조국은 어머니보다도 아버지보다도 또 그 밖의 모든 조상들보다도 더욱 귀하고 더욱 숭고하고 더욱 신성한 것이다. 우리는 조국을 소중히 여기고 조국에 순종해야 한다. -소크라테스

댓글 ( 1)

댓글 남기기

작성