JSP

리팩토리 전

 

class JdbcTemplate

package net.slipp.support;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import net.slipp.user.User;

public abstract class JdbcTemplate {

	public void prepareStatementExecute(String sql)throws SQLException {
		
		Connection conn = null;
		PreparedStatement pstmt = null;
		try {
			conn = ConnectionManager.getConnection();
			pstmt = conn.prepareStatement(sql); 
			
			prepareSet(pstmt);
			
			pstmt.executeUpdate();
		} finally {
			if (pstmt != null) {
				pstmt.close();
			}
			
			if (conn != null) {
				conn.close();
			}
		}	
	}
		
	public abstract void prepareSet(PreparedStatement pstmt) throws SQLException;
	
	
}

 

 

 

class SelectJdbcTemplate

package net.slipp.support;

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

import net.slipp.user.User;

public abstract class SelectJdbcTemplate {

	public Object executeQuery(String sql) throws SQLException {
		
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try {
			conn = ConnectionManager.getConnection();
			pstmt = conn.prepareStatement(sql); 
			setParameters( pstmt);
			
			rs = pstmt.executeQuery();
			
			return mapRow(rs);
		} finally {
			if (rs != null) {
				rs.close();
			}
			
			if (pstmt != null) {
				pstmt.close();
			}
			
			if (conn != null) {
				conn.close();
			}
		}
		
	}
	
	public abstract void setParameters(PreparedStatement pstmt) throws SQLException ;	
	
	public abstract Object mapRow(ResultSet rs) throws SQLException ;
	
	
	
	
}


 

 

 

 

 

리팩토링 후

 

interface SetParameters 

package net.slipp.user;

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

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

 

 

interface MapRow

package net.slipp.user;

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

public interface MapRow {
	Object mapRow(ResultSet rs) throws SQLException ;
}

 

class JdbcTemplate 

package net.slipp.support;

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

import net.slipp.user.MapRow;
import net.slipp.user.SetParameters;

public class JdbcTemplate {

	public void prepareStatementExecute(String sql, SetParameters 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 Object executeQuery(String sql, SetParameters set,  MapRow row) throws SQLException {
			
			Connection conn = null;
			PreparedStatement pstmt = null;
			ResultSet rs = null;
			try {
				conn = ConnectionManager.getConnection();
				pstmt = conn.prepareStatement(sql); 
				set.setParameters(pstmt);
				
				rs = pstmt.executeQuery();
				
				if (!rs.next()) {
					return null;
				}
				
				return row.mapRow(rs);
				
			} finally {
				if (rs != null) {
					rs.close();
				}
				
				if (pstmt != null) {
					pstmt.close();
				}
				
				if (conn != null) {
					conn.close();
				}
			}
			
		}
	
	
}

 

class UserDAO 

package net.slipp.user;

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

import net.slipp.support.JdbcTemplate;

public class UserDAO {


	public void addUser(User user) throws SQLException {
		SetParameters set=new SetParameters() {
			@Override
			public void setParameters(PreparedStatement pstmt) throws SQLException {
				pstmt.setString(1, user.getUserId());			
				pstmt.setString(2, user.getPassword());
				pstmt.setString(3, user.getName());
				pstmt.setString(4, user.getEmail());
				
			}
		};
		String sql = "insert into USERS values(?,?,?,?)";
		JdbcTemplate jdbcTemplate=new JdbcTemplate();
		jdbcTemplate.prepareStatementExecute(sql,set);	
		
	}

	
	
	public User findByUserId(String userId) throws SQLException {
		SetParameters set=new SetParameters() {
			@Override
			public void setParameters(PreparedStatement pstmt) throws SQLException {
				pstmt.setString(1, userId);		
			}
		};
		
		MapRow row =new MapRow() {	
			@Override
			public Object mapRow(ResultSet rs) throws SQLException {

				return new User(
						rs.getString("userId"), 
						rs.getString("password"),
						rs.getString("name"),
						rs.getString("email"));
			}
		};
		
		String sql = "select * from USERS where userId = ?";
		JdbcTemplate jdbcTemplate=new JdbcTemplate();
		return (User)jdbcTemplate.executeQuery(sql, set, row);
		
	}

	

	public void removeUser(String userId) throws SQLException {
		SetParameters set=new SetParameters() {
			@Override
			public void setParameters(PreparedStatement pstmt) throws SQLException {
				pstmt.setString(1, userId);		
				
			}
		};
		
		String sql = "delete from USERS where userId = ?";
		JdbcTemplate jdbcTemplate=new JdbcTemplate();
		jdbcTemplate.prepareStatementExecute(sql,set);
	}

	
	public void updateUser(User user) throws SQLException {
		SetParameters set=new SetParameters() {
			@Override
			public void setParameters(PreparedStatement pstmt) throws SQLException {
				pstmt.setString(1, user.getPassword());
				pstmt.setString(2, user.getName());
				pstmt.setString(3, user.getEmail());
				pstmt.setString(4, user.getUserId());	
			}
		};

		String sql = "update USERS set password = ?, name = ?, email = ? where userId = ?";
		JdbcTemplate jdbcTemplate=new JdbcTemplate();
		jdbcTemplate.prepareStatementExecute(sql,set);
	}
	
	
	


}


 

 

 

 

 

 

 

jsp

 

about author

PHRASE

Level 60  머나먼나라

여자가 임신하면 피의 양이 25 % 정도 증가한다. - New Life 편집부

댓글 ( 4)

댓글 남기기

작성