22.5.18
- MVC 1형 -> 2형 연결과정 배우는 중
- 1형으로 만든 웹마겟 페이지를 2형으로 다시 만들 수 있어야 함!
- CRUD 방식도 업로드 할 것!
1) DBConnecton 연결
- DBConnection 이라는 Class를 만들고 Connection 타입으로 파라메터 지정!
- Database와 연결시에는 url, username(id), password 3가지의 파라메터가 필요
- 이를 Class.forname으로 설정하고, DriveManager로 연결해주면 이클립스와 데이터베이스가 연동됨
public static Connection getConnection() {
Connection conn = null;
String url = "jdbc:mysql://localhost:3306/member_db";
String username= "root";
String password = "1234";
try {
Class.forName("com.mysql.cj.jdbc.Driver");
conn = DriverManager.getConnection(url, username, password);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}catch(SQLException e) {
e.printStackTrace();
}
return conn;
}
- 이런식으로 정리가 됨. 이를 다른 클래스에서 getConnection()으로 불러와 사용하는 것.
2) Web-Servlet 설정
package controller;
import java.io.IOException;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/cont123")
public class cont123 extends HttpServlet {
public cont123() {
}
public void init(ServletConfig config) throws ServletException {
}
public void destroy() {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { }
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { }
}
- Servlet으로 클래스를 만들면 자동으로 HttpServlet이 확장되고, 만들때 init과 destroy메서드도 같이 만들 수 있음
- 자동 생성된 주석을 다 삭제해주고 나면 위와 같은 형태가 됨.
- @WebServlet("/cont123") 부분이 WebServlet설정이고, " " 안의 부분을 원하는 대로 설정하면 됨
- deGet 은 method의 get dePost는 method의 post부분을 의미함. 이 둘이 실행되는 것(주로)
- init()은 맨 처음한번만 실행되고, 반복 실행시에는 실행되지 않음.
-----------------
여기까지!
38) Eclipse <-> Git hub 연결하기 (0) | 2022.05.25 |
---|---|
37) Spring MVC 연결(JAVA) (0) | 2022.05.24 |
35) EL(표현언어) 메소드 호출(JSTL 인코딩 디코딩) (0) | 2022.05.17 |
34) 세션 (0) | 2022.05.17 |
33) 오류 처리 (0) | 2022.05.17 |
댓글 영역