JSP_basic. SQL DBに関連付けられたログインページの変更(PStmt、セッションのINSERTを使用)


前の記事で作成したログインページにJSPファイルを追加し、UPDATE、DELETE、INSERTクエリー文を実行できます.
まず、INSERTクエリ文を実行するデータを入力します
userJoinForm.jspを作成します.
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<row>
<div class="col-sm-6">

		<p>회원가입 페이지입니다. 아래 정보를 입력해주세요.</p>
		<form action="userJoinCheck.jsp" method="post">
			<input type="text" name="userId" placeholder="아이디를 입력해주세요." class="form-control form-control-lg">
			<input type="password" name="userPw" placeholder="비밀번호를 입력해주세요." class="form-control form-control-lg">
			<input type="text" name="userName" placeholder="이름을 입력해주세요." class="form-control form-control-lg">
			<input type="text" name="userMail" placeholder="이메일을 입력해주세요." class="form-control form-control-lg">
			<input type="submit" value="확인" class="btn btn-primary mb-3">
			<a href="userLoginForm.jsp">뒤로가기</a>
		</form>
</div>
</row>

</body>
</html>
Action="は、入力された情報をuserJoinCheckで送信します.
userJoinCheck.jspではrequestです.getParameterで
転送したデータをDBにバインドします.
IDはname="userId"
パスワードはname=「userpw」
名前はname="userName"
メールはname="userMail"
使います.
placeholderを使用して、ユーザーが知っていると宣言しました.

次はUserJoinCheckjspを作成しましょう.
userJoinCheck.jspではrequestです.getParameterを使用してデータを受信し、Query文に書き込みます.
次はUserJoinCheckです.jspの専門家.
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.Connection"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
<%
	request.setCharacterEncoding("UTF-8"); 
	String uId = request.getParameter("userId");
	String uPw = request.getParameter("userPw");
	String uName = request.getParameter("userName");
	String uMail = request.getParameter("userMail");
	
	String dbType = "com.mysql.cj.jdbc.Driver";
	String connectUrl = "jdbc:mysql://localhost:3306/jdbcprac2?serverTimezone=UTC";
	String connectId = "root";
	String connectPw = "????";

	try {
		Class.forName(dbType);
		
		Connection con = DriverManager.getConnection(connectUrl, connectId, connectPw);
		
		
		String sql = "INSERT INTO userinfo VALUES(?, ?, ?, ?)";
		PreparedStatement pstmt = con.prepareStatement(sql);
		
		pstmt.setString(1, uId);
		pstmt.setString(2, uPw);
		pstmt.setString(3, uName);
		pstmt.setString(4, uMail);
		
		pstmt.executeUpdate();
		
	} catch(Exception e) {
		e.printStackTrace();
	}
	
	
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<p><strong><%= uId %>님 회원가입 감사합니다!</strong></p>
	<p><strong><a href="userLoginForm.jsp">로그인하기</a></strong></p>
	
	

</body>
</html>
まず
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.Connection"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
必要な方法を集めたと発表しました.
まだあります.
	request.setCharacterEncoding("UTF-8"); 
	String uId = request.getParameter("userId");
	String uPw = request.getParameter("userPw");
	String uName = request.getParameter("userName");
	String uMail = request.getParameter("userMail");
	
request.setCharacterEncoding("UTF-8");道.
発生する可能性のあるデータエラーを防止します.
次に、uId、uPw、uName、uMail変数です.
userJoinForm.jspのデータが保存されています.
	String dbType = "com.mysql.cj.jdbc.Driver";
	String connectUrl = "jdbc:mysql://localhost:3306/jdbcprac2?serverTimezone=UTC";
	String connectId = "root";
	String connectPw = "????";
PreparedStatementを使用するには、ConnectionにアクセスするSQL接続変数を指定しておきます.
try~catch文に必要なコマンドを入力できるようになりました.
	try {
		Class.forName(dbType);
		
		Connection con = DriverManager.getConnection(connectUrl, connectId, connectPw);
		
		
		String sql = "INSERT INTO userinfo VALUES(?, ?, ?, ?)";
		PreparedStatement pstmt = con.prepareStatement(sql);
		
		pstmt.setString(1, uId);
		pstmt.setString(2, uPw);
		pstmt.setString(3, uName);
		pstmt.setString(4, uMail);
		
		pstmt.executeUpdate();
		
	} catch(Exception e) {
		e.printStackTrace();
	}
	
	
接続接続でSQL DBに連絡します.
String sqlを宣言する場合のみ、sql文の変数を使用する必要がある場合?4つメモしました.
次に、PreparedStatementを使用して、1、2、3、および4回目に指定した変数を入力します.
次にexecuteUpdate()を宣言します.
SQL DBが変更されます.
<body>
	<p><strong><%= uId %>님 회원가입 감사합니다!</strong></p>
	<p><strong><a href="userLoginForm.jsp">로그인하기</a></strong></p>
	
	

</body>
bodyラベルにはハイパーリンクと挨拶文が簡単に書かれています.

まずはここまで.