MySQL ๊ธฐ๋ณธ ์ฌ์ฉ๋ฒ
JSP์ MySQL ์ฐ๋
1) user ํจํค์ง ์์ฑ
Java Resourses > New > Package
(ํจํค์ง ๋ช : USER ๋ฐ์ดํฐ๋ฒ ์ด์ค ๋ช ๊ณผ ๋์ผํ๊ฒ ์ง์ด์ค)
2) user ํจํค์ง ๋ด์ UserDTO, UserDAO ํด๋์ค ์์ฑ
- UserDTO(data transfer object) : jspํ๋ก๊ทธ๋จ ์์์ ์ผ์์ ์ผ๋ก ํ๋์ ๋ฐ์ดํฐ ๋จ์๋ฅผ ๋ด๊ธฐ ์ํ ์ฉ๋๋ก ์ ์๋ ๊ฐ์ฒด
- UserDAO(data access object) : ์ค์ง์ ์ผ๋ก ๋ฐ์ดํฐ๋ฒ ์ด์ค์ ์ฐ๋๋์ด ์ด๋ ํ ๋ด์ฉ์ ๊ธฐ๋กํ๊ณ ๊ฐ์ ธ์ค๋ ์ญํ ์ ์ํํ๋ ๊ฐ์ฒด
3) UserDTO.java ํ์ผ ์ฝ๋ ์์ฑ
package user;
public class UserDTO {
String userID;
String userPassword;
}
์ ์ฝ๋์ ๊ฐ์ด ์์ฑํ ๋ค [์ค๋ฅธ์ชฝ ๋ง์ฐ์ค ํด๋ฆญ > Source > "Generate Getters and Setters"] ์ ํ
๋ชจ๋ ๋ณ์์ ๋ํด์ getter์ setter๋ฅผ ๋ง๋ค์ด์ค๋ค.
package user;
public class UserDTO {
String userID;
String userPassword;
public String getUserID() {
return userID;
}
public void setUserID(String userID) {
this.userID = userID;
}
public String getUserPassword() {
return userPassword;
}
public void setUserPassword(String userPassword) {
this.userPassword = userPassword;
}
}
4) ๋ฐ์ดํฐ๋ฒ ์ด์ค ์ฐ๋ ๋ถ๋ถ ์ค์
util ํจํค์ง ์์ฑ -> DatabaseUtil ํด๋์ค ์์ฑ
#DatabaseUtil.java ํ์ผ
package util;
import java.sql.Connection;
import java.sql.DriverManager;
public class DatabaseUtil {
public static Connection getConnection() {
try {
String dbURL = "jdbc:mysql://localhost:3306/TUTORIAL";
String dbID = "root";
String dbPassword = "0000";
Class.forName("com.mysql.cj.jdbc.Driver");
return DriverManager.getConnection(dbURL, dbID, dbPassword);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
5) jdbc Driver ํ์ผ ์ค์
์๋ ๋งํฌ์์ ๊ฐ์ ํ๋ซํผ์ ๋ง๋ JDBC๋ฅผ ๋ค์ด๋ก๋ ํด์ค๋ค.
https://dev.mysql.com/downloads/connector/j/
jar ํ์ผ์ ๋ค์ด๋ก๋ ๋ฐ์ [WEB-INF > libํด๋] ์์ ์ ์ฅํด๋๋ค.
6) UserDTO.java ํ์ผ ์ฝ๋ ์์ฑ
package user;
import java.sql.Connection;
import java.sql.PreparedStatement;
import util.DatabaseUtil;
public class UserDAO {
public int join(String userID, String userPassword) {
String SQL = "INSERT INTO USER VALUES (?, ?)";
try {
Connection conn = DatabaseUtil.getConnection();
PreparedStatement pstmt = conn.prepareStatement(SQL);
pstmt.setString(1, userID);
pstmt.setString(2, userPassword);
return pstmt.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
}
return -1;
}
}
7) index.jsp ํ์ผ ์ค์
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
//userID์ userPassword๋ฅผ ์
๋ ฅ๋ฐ์ ํ์๊ฐ์
์์ผ์ฃผ๋ form ์์ฑ
<form action="./userJoinAction.jsp" method="post">
<input type="text" name="userID">
<input type="password" name="userPassword">
<input type="submit" value="ํ์๊ฐ์
">
</form>
</body>
</html>
๊ฒฐ๊ณผ ํ๋ฉด
ํ์๊ฐ์ ๋ฒํผ์ ํด๋ฆญํ๊ฒ ๋๋ฉด userJoinAction์ผ๋ก ์ฌ์ฉ์๊ฐ ์ ๋ ฅํ ์ ๋ณด๊ฐ ๋ ๋ผ๊ฐ๋ค.
๊ทธ๋ ๋ค๋ฉด userJoinAction.jsp๋ฅผ ์์ฑํด๋ณด์.
8) userJoinAction.jsp ํ์ผ ์์ฑ
[webapp > New > JSP File]
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="user.UserDTO"%>
<%@ page import="user.UserDAO"%>
<%@ page import="java.io.PrintWriter"%>
<%
request.setCharacterEncoding("UTF-8");
String userID = null;
String userPassword = null;
if (request.getParameter("userID")!=null){
userID = (String) request.getParameter("userID");
}
if (request.getParameter("userPassword")!=null){
userPassword = (String) request.getParameter("userPassword");
}
if (userID == null || userPassword == null){
PrintWriter script = response.getWriter();
script.println("<script>");
script.println("alert('์
๋ ฅ์ด ์ ๋ ์ฌํญ์ด ์์ต๋๋ค.');");
script.println("history.back();");
script.println("</script>");
script.close();
return;
}
UserDAO userDAO = new UserDAO();
int result = userDAO.join(userID, userPassword);
if (result == 1){
PrintWriter script = response.getWriter();
script.println("<script>");
script.println("alert('ํ์๊ฐ์
์ ์ฑ๊ณตํ์ต๋๋ค.');");
script.println("location.href = 'index.jsp';");
script.println("</script>");
script.close();
return;
}
%>
๊ฒฐ๊ณผํ๋ฉด
ํ๋ฉด ์ ๋๋ก ๋ธ!
mysql ์ฑ๊ณต!!!
'๐ปWEB BackEnd > JSP' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
DB ์ค๊ณ & ๋ฐ์ดํฐ ๋ชจ๋ธ๋ง (0) | 2022.11.07 |
---|---|
๋ก๊ทธ์ธ, ํ์๊ฐ์ , ๋ก๊ทธ์์ ๊ตฌํ (0) | 2022.11.07 |
index.jsp ํ์ด์ง (0) | 2022.11.07 |
์น๋์์ธ ํ๋ ์์ํฌ - Bootstrap & jQuery (0) | 2022.11.07 |
๊ฐ๋ฐ ํ๊ฒฝ ๊ตฌ์ถ + ํ ์คํธ (0) | 2022.11.06 |