본문 바로가기

프로그램언어/자바

톰켓8.5에서 mysql연동하기

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&amp;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 추가하기

 

 

예]

// ######## 서블릿파일
@WebServlet
("/idCheck.do")

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
 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

    }

}

// ######## 디비 접속하고 테이블 검색
public
MemberDto memberInfo(String mid) throws SQLException, NamingException{

    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

}