- JDBC 드라이버 로드 : DriverManager
- DB 연결 : Connection
- SQL 실행 : Statement
- 연결끊음 : ResultSet
import java.sql.Connection;
import java.sql.DriverManager;
public class JdbcUtil {
// 데이터 베이스 연결
public static Connection con;
public static Connection getConnection() {
Connection conn = null;
//연결
try {
String id = "아이디";
String pw= "비밀번호";
String url="jdbc:oracle:thin:@localhost:1521/xepdb1";
//jdbc 드라이버 로딩
Class.forName("oracle.jdbc.driver.OracleDriver");
conn = DriverManager.getConnection(url, id, pw);
System.out.println("DB 연결 성공");
} catch (Exception e) {
System.out.println("DB 연결 실패");
}
return conn;
}
}
- 회원가입 폼
더보기
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>MemberJoin</title>
</head>
<body>
<form action="MemberJoinProc.jsp" method="post">
<table width="500" border="1">
<tr height="50">
<td width="150" align="center"> 아이디</td>
<td width="350" align="center"><input type="text" name="id" size="40"></td>
</tr>
<tr height="50">
<td width="150" align="center"> 패스워드</td>
<td width="350" align="center"><input type="password" name="pass1" size="40"></td>
</tr>
<tr height="50">
<td width="150" align="center"> 패스워드확인</td>
<td width="350" align="center"><input type="password" name="pass2" size="40"></td>
</tr>
<tr height="50">
<td width="150" align="center"> 이메일</td>
<td width="350" align="center"><input type="email" name="email" size="40"></td>
</tr>
<tr height="50">
<td width="150" align="center"> 전화번호</td>
<td width="350" align="center"><input type="tel" name="tel" size="40"></td>
</tr>
<tr height="50">
<td width="150" align="center"> 당신의관심분야</td>
<td width="350" align="center">
<input type="checkbox" name="hobby" value="캠핑">캠핑
<input type="checkbox" name="hobby" value="등산">등산
<input type="checkbox" name="hobby" value="영화">영화
<input type="checkbox" name="hobby" value="독서">독서
</td>
</tr>
<tr height="50">
<td width="150" align="center"> 당신의직업은</td>
<td width="350" align="center">
<select name="job">
<option value="교사">교사</option>
<option value="변호사">변호사</option>
<option value="의사">의사</option>
<option value="기술사">기술사</option>
</select>
</td>
</tr>
<tr height="50">
<td width="150" align="center"> 당신의연령은</td>
<td width="350" align="center">
<input type="radio" name="age" value="10">10대
<input type="radio" name="age" value="20">20대
<input type="radio" name="age" value="30">30대
<input type="radio" name="age" value="40">40대
</td>
</tr>
<tr height="50">
<td width="150" align="center"> 하고싶은말</td>
<td width="350" align="center">
<textarea rows="5" cols="40" name="info"></textarea>
</td>
</tr>
<tr height="50">
<td align="center" colspan="2">
<input type="submit" value="회원가입">
<input type="reset" value="취소">
</td>
</tr>
</table>
</form>
</body>
</html>
- JavaBean
더보기
package bean;
public class MemberBean {
private String id;
private String pass1;
private String pass2;
private String email;
private String tel;
private String hobby;
private String job;
private String age;
private String info;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getPass1() {
return pass1;
}
public void setPass1(String pass1) {
this.pass1 = pass1;
}
public String getPass2() {
return pass2;
}
public void setPass2(String pass2) {
this.pass2 = pass2;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getTel() {
return tel;
}
public void setTel(String tel) {
this.tel = tel;
}
public String getHobby() {
return hobby;
}
public void setHobby(String hobby) {
this.hobby = hobby;
}
public String getJob() {
return job;
}
public void setJob(String job) {
this.job = job;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getInfo() {
return info;
}
public void setInfo(String info) {
this.info = info;
}
}
- db연결 jsp
더보기
<%@page import="java.sql.Connection"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.DriverManager"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>MemberJoinProc</title>
</head>
<body>
<%
request.setCharacterEncoding("UTF-8");
//복수 선택 값을 배열에 저장
String []hobby = request.getParameterValues("hobby");
String str = "";
//배열에 있는 내용을 하나의 문자열로 저장
for(int i=0; i<hobby.length; i++){
str += hobby[i]+" ";
}
%>
<jsp:useBean id="memBean" class="bean.MemberBean">
<jsp:setProperty property="*" name="memBean"/>
</jsp:useBean>
<%
memBean.setHobby(str);
String id = "아이디";
String pw= "비밀번호";
String url="jdbc:oracle:thin:@localhost:1521/xepdb1";
try {
//1. 해당 데이터 배이스를 사용한다고 선언
Class.forName("oracle.jdbc.driver.OracleDriver");
//2. 데이터 베이스에 접속
Connection con = DriverManager.getConnection(url, id, pw);
String sql = "insert into Mem values(?,?,?,?,?,?,?,?)";
//3. 접속 후 쿼리를 준비한다.
PreparedStatement pstmt = con.prepareStatement(sql);
//4. ?에 맞게 데이터를 맵핑
pstmt.setString(1, memBean.getId());
pstmt.setString(2, memBean.getPass1());
pstmt.setString(3, memBean.getEmail());
pstmt.setString(4, memBean.getTel());
pstmt.setString(5, memBean.getHobby());
pstmt.setString(6, memBean.getJob());
pstmt.setString(7, memBean.getAge());
pstmt.setString(8, memBean.getInfo());
//5. 쿼리 실행
//insert, update, delete 시 사용하는 메소드
pstmt.executeQuery();
con.close();
} catch(Exception e) {
e.printStackTrace();
}
%>
입력 완료
</body>
</html>
728x90
728x90
'JSP' 카테고리의 다른 글
DAO 이용한 회원관리 (0) | 2022.07.15 |
---|---|
DB 전까지의 문제 (0) | 2022.07.14 |
서블릿 (0) | 2022.07.12 |
JAVABEANS (0) | 2022.07.12 |
jsp review (0) | 2022.07.12 |