티스토리 뷰
준비하기
우선 JDBC를 사용하기 위해 드라이버를 다운받는다. (Oracle 사용)
https://www.oracle.com/database/technologies/jdbcdriver-ucp-downloads.html
ojdbc6.jar 파일 다운
이클립스에서 dynamic web project 생성
드라이버 복사 후 WebContent -> WEB-INF -> lib 폴더에 붙여넣는다.
DB table 생성
오라클에 접속하여 사용할 테이블을 생성한다.
create table dbtest (
id varchar2(50) primary key,
passwd varchar2(50) not null,
name varchar2(50) not null,
tel varchar2(30),
reg_date date not null
);
아이디, 비밀번호, 이름, 전화번호, 가입일자 사용
DBDto.java 생성
데이터들을 한번에 담을 DBDto 생성
<DBDto.java>
package db;
import java.sql.Timestamp;
public class DBDto {
private String id;
private String passwd;
private String name;
private String tel;
private Timestamp reg_date;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getPasswd() {
return passwd;
}
public void setPasswd(String passwd) {
this.passwd = passwd;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getTel() {
return tel;
}
public void setTel(String tel) {
this.tel = tel;
}
public Timestamp getReg_date() {
return reg_date;
}
public void setReg_date(Timestamp reg_date) {
this.reg_date = reg_date;
}
}
변수 이름은 데이터베이스와 같게 설정하고 getter, setter 메소드 작성
데이터를 처리할 DBDao 생성
여기서는 데이터 처리만 담당한다.
<DBDao.java>
package db;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class DBDao {
// 싱글톤 패턴을 사용하여 객체를 생성 하여 재사용
public static DBDao instance = new DBDao();
public static DBDao getInstance() {
return instance;
}
// 데이터베이스와 연결
public Connection getConnection() throws ClassNotFoundException, SQLException {
Class.forName("oracle.jdbc.driver.OracleDriver");
String url = "jdbc:oracle:thin:@localhost:1521:xe";
String dbId = "exam";
String dbPasswd = "exam";
return DriverManager.getConnection(url, dbId, dbPasswd);
}
// 데이터 추가하는 메소드
public int insertMember(DBDto dbDto) {
int result = 0;
Connection conn = null;
PreparedStatement pstmt = null;
try {
conn = getConnection();
String sql = "insert into dbtest values(?, ?, ?, ?, sysdate)";
pstmt = conn.prepareStatement(sql);
// sql의 ? 부분에 데이터를 넣는다.
pstmt.setString(1, dbDto.getId());
pstmt.setString(2, dbDto.getPasswd());
pstmt.setString(3, dbDto.getName());
pstmt.setString(4, dbDto.getTel());
result = pstmt.executeUpdate();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if(pstmt != null) pstmt.close();
if(conn != null) conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return result;
}
}
데이터 입력받을 폼 만들기
<insertTestForm.jsp>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert test</title>
</head>
<body>
<form method="post" action="insertTest.jsp">
<table border="1">
<tr>
<th> 아이디 </th>
<td> <input type="text" name="id"> </td>
</tr>
<tr>
<th> 비밀번호 </th>
<td> <input type="password" name="passwd"> </td>
</tr>
<tr>
<th> 이름 </th>
<td> <input type="text" name="name"> </td>
</tr>
<tr>
<th> 전화번호 </th>
<td> <input type="text" name="tel"> </td>
</tr>
<tr>
<th colspan="2">
<input type="submit" value="전송">
<input type="reset" value="취소">
</th>
</tr>
</table>
</form>
</body>
</html>
데이터 처리페이지 만들기
<insertTest.jsp>
<%@page import="db.DBDao"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert test</title>
</head>
<body>
<%
request.setCharacterEncoding("utf-8");
%>
<jsp:useBean id="dbDto" class="db.DBDto"/>
<jsp:setProperty property="*" name="dbDto"/>
<%
// DBDao 객체 생성하기
DBDao dbDao = DBDao.getInstance();
// insertMember() 메소드 실행
int result = dbDao.insertMember(dbDto);
if( result == 0) {
%>
데이터 추가 실패 <br>
<!-- 데이터 추가 실패 시 2초후 insertTestForm으로 이동한다. -->
<meta http-equiv="refresh" content="2; url=insertTestForm.jsp">
<%
} else {
%>
데이터 추가 성공 <br>
<%
}
%>
</body>
</html>
테스트
DB에서 확인하기
파일구조
'JSP' 카테고리의 다른 글
JSTL - <c:foreach> 사용법 (0) | 2020.09.27 |
---|---|
[JDBC] JDBC(Java DataBase Connectivity)란? (0) | 2020.06.14 |
Tomcat(톰캣) 설치 및 환경설정 (0) | 2020.05.15 |
Eclipse(이클립스) 설치 및 환경설정 (0) | 2020.05.15 |
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- SPARK
- Free space management
- Spring
- linux
- maven
- Variable allocation
- RAID Architecture
- I/O Services of OS
- Disk System
- 빅데이터
- vmware
- Disk Scheduling
- Flume
- gradle
- JSON
- hadoop
- 하둡
- File Protection
- I/O Mechanisms
- 빅데이터 플랫폼
- Java
- Allocation methods
- springboot
- Replacement Strategies
- oracle
- jdbc
- aop
- SQL
- mapreduce
- HDFS
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
글 보관함