context.xml 설정
<Resource name="jdbc/mysql" type="javax.sql.DataSource" auth="Container" maxActive="30" maxIdle="3" maxWait="3000" username="디비관리자아이디" password="비밀번호" testOnBorrow="true" driverClassName="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/bbs?characterEncoding=UTF-8&serverTimezone=UTC" /> |
public class DBCPConnection { public static Connection getConnection() throws NamingException, SQLException{ Context init = new InitialContext(); DataSource ds = (DataSource)init.lookup("java:comp/env/jdbc/mysql"); Connection conn = ds.getConnection(); return conn } } |
Libraries탭에서 Add JARs...버튼을 클릭하여 mysql-connectro 추가하기
예]
// ######## 서블릿파일 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try { process(request, response); } catch (SQLException e) { e.printStackTrace(); } catch (NamingException e) { e.printStackTrace(); } } public void process(HttpServletRequest request, HttpServletResponse response) throws SQLException, ServletException, IOException, NamingException{
request.setCharacterEncoding("UTF-8"); String id = request.getParameter("id"); MemberDao dao = new MemberDao(); String a = dao.idCheck(id); System.out.println(id); System.out.println(a); String viewpage = null try { if(a.equals("1")) { viewpage = "canuseid.jsp" System.out.println("id사용가능"); }else if(a.equals("2")) { viewpage = "cantuseid.jsp" System.out.println("id 사용불가"); } RequestDispatcher dispatcher = request.getRequestDispatcher(viewpage);
dispatcher.forward(request, response); } catch (Exception e) { e.printStackTrace(); } }
public static Connection getConnection() throws NamingException, SQLException{ Context init = new InitialContext(); DataSource ds = (DataSource)init.lookup("java:comp/env/jdbc/mysql"); Connection conn = ds.getConnection(); return conn } } Connection conn = DBCPConnection.getConnection(); PreparedStatement pstmt = null ResultSet rs = null
MemberDto dto = new MemberDto();
try { pstmt=conn.prepareStatement("select * from member where id = ?"); pstmt.setString(1, mid);
rs = pstmt.executeQuery(); if(rs.next()) { String name = rs.getString("name"); String id = rs.getString("id"); String password = rs.getString("password");
dto.setId(id); dto.setName(name); dto.setPassword(password); } } catch (Exception e) { e.printStackTrace(); }finally { JdbcUtil.close(pstmt); JdbcUtil.close(conn); } return dto } |
'프로그램언어 > 자바' 카테고리의 다른 글
AWT와 스윙(Swing) (0) | 2020.11.01 |
---|---|
jsp파일에서 다음 주소검색창 열기 (0) | 2020.10.18 |
2차원배열에 대문자를 입력하고 소문자로 출력하기 (0) | 2020.09.29 |
클래스를 배열로 선언하기 (0) | 2020.09.15 |
이클립스 jsp파일 생성시 utf-8 설정 (0) | 2020.06.29 |