이번엔 JSP InsertTable 을 실습해보겠다.
일단 index.jsp를 작성해보자
insert_form 을 작성해보자.
이제 mysql jdbc연결 방법을 두가지 알려주겠다.
첫번째 방법: 의존성을 추가해준다.
두번째 방법: lib 파일에 직접 jar파일을 넣어주기
아무거나 선택해서 하면된다.
이제 서블릿을 생성해주자
경로를 참고하고 URL pattern을 알맞게 지정해주자
그럼 서블릿을 작성해주자
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package ch06_class; import java.io.IOException; import java.io.PrintWriter; import static java.lang.System.out; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * * @author user1 */ @WebServlet(name = "NewServlet", urlPatterns = {"/ch06/InsertTable/Insert.do"}) public class InsertServlet extends HttpServlet { /** * Processes requests for both HTTP GET and POST * methods. * * @param request servlet request * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); try (PrintWriter out = response.getWriter()) { /* TODO output your page here. You may use following sample code. */ final String JdbcDriver = "com.mysql.cj.jdbc.Driver"; final String JdbcUrl = "jdbc:mysql:///webmail?serverTimezone=Asia/Seoul"; final String User = "earthking"; final String Password = "1234"; try{ //1. JDBC 드라이버 적재 Class.forName(JdbcDriver); //2. Connection 객체 생성 Connection conn = DriverManager.getConnection(JdbcUrl, User, Password); //3. PreparedStatement 객체 생성 String sql = "INSERT INTO address VALUES(?,?,?)"; PreparedStatement pstmt = conn.prepareStatement(sql); //4. SQL 문 완성 request.setCharacterEncoding("UTF-8"); String email = request.getParameter("email"); // 주키 if (!(email == null) && !email.equals("")){ String name = request.getParameter("name"); String phone = request.getParameter("phone"); pstmt.setString(1, email); pstmt.setString(2, name); pstmt.setString(3, phone); // 5. 실행 : Insert, update, delete 사용 가능함 pstmt.executeUpdate(); } //6. 자원 해제 pstmt.close(); conn.close(); response.sendRedirect("index.jsp"); } catch (Exception ex){ out.println("오류가 발생했습니다. (발생오류: " + ex.getMessage() + ")"); } } finally { out.close(); } } // /** * Handles the HTTP GET method. * * @param request servlet request * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); } /** * Handles the HTTP POST method. * * @param request servlet request * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); } /** * Returns a short description of the servlet. * * @return a String containing servlet description */ @Override public String getServletInfo() { return "Short description"; }// } |
그다음 오른쪽 마우스 클릭을 눌러 mysql을 imports 해주자
그럼이제 실행해보자
'JSP' 카테고리의 다른 글
#7.2 JSP Download 프로젝트 (0) | 2020.05.07 |
---|---|
#7.1 JSP FileUpload프로젝트 (0) | 2020.05.06 |
#6.2 JSP ShowTable2 프로젝트 (테그파일) (0) | 2020.04.25 |
#5.6 JSP URLTest 프로젝트 (0) | 2020.04.15 |
#5.5 JSP 태그 파일 사용 예제 (0) | 2020.04.14 |