๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

๐Ÿ’ปWEB BackEnd/JSP

MySQL + JSP ์—ฐ๋™

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/

 

MySQL :: Download Connector/J

MySQL Connector/J 8.0 is highly recommended for use with MySQL Server 8.0, 5.7 and 5.6. Please upgrade to MySQL Connector/J 8.0.

dev.mysql.com

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 ์„ฑ๊ณต!!!