ConnectionPool

programing/JAVA 2009.06.02 17:24

//context.xml
<Context antiJARLocking="true" path="" privileged="true">
<Resource name="jdbc/Oracle" auth="Container"
   type="javax.sql.DataSource"  driverClassName="oracle.jdbc.driver.OracleDriver"
   url="jdbc:oracle:thin:@localhost:1521:XE"
   username="TEST" password="0105" maxActive="20" maxIdle="10"
   maxWait="-1"/>
</Context>


//util.ConnLocator
public class ConnLocator {
 public static Connection getConnection() throws SQLException{
  DataSource ds = null;
  try {
   Context context = new InitialContext();
   ds = (DataSource)context.lookup("java:comp/env/jdbc/Oracle");
  } catch (NamingException e) {
   // TODO: handle exception
   e.printStackTrace();
  }
  return ds.getConnection(); //커넥션 생성 X 이미 만들어져있는 커넥션을 가져오는것
 }
}

//DAO
private static MemberDao single;

 private MemberDao() {

 }

 public static MemberDao getInstance() {
  if (single == null)
   single = new MemberDao();
  return single;
 }
//후 insert, select, update 등의 메서드
public MemberVo select(String id){
  MemberVo mvo = null;
  Connection con = null;
  PreparedStatement pstmt = null;
  ResultSet rs = null;
  try {
   con = ConnLocator.getConnection();
   pstmt = con.prepareStatement("select * from member where id=?");
   pstmt.setString(1, id);
   rs = pstmt.executeQuery();
   if(rs.next()){
    String _id = rs.getString(1);
    String _pwd = rs.getString(2);
    String name = rs.getString(3);
    String cellPhone1 = rs.getString(4);
    String cellPhone2 = rs.getString(5);
    String cellPhone3 = rs.getString(6);
    String register = rs.getString(7);
    String regdate = rs.getString(8);
    mvo = new MemberVo(_id,_pwd,name,cellPhone1,cellPhone2,cellPhone3,register,regdate);
   }
  } catch (SQLException e) {
   e.printStackTrace();
  } finally{
   try {
    if(rs !=null) rs.close();
    if(pstmt != null)pstmt.close();
    if(con != null)con.close();
   } catch (Exception e2) {
    // TODO: handle exception
   }
  }
  return mvo;
 }


신고

'programing > JAVA' 카테고리의 다른 글

File Up & Down  (0) 2009.06.02
Paging  (0) 2009.06.02
FileUtil  (0) 2009.06.02
JSP ibatis+Struts2  (0) 2009.06.02
ConnectionPool  (0) 2009.06.02
Servlet & JSP 1'  (0) 2009.04.15
Posted by 대절님

댓글을 달아 주세요



티스토리 툴바