insertForm.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<title>MEMBER 테이블에 레코드 입력</title>
</head>
<body>
<form method="post" action="insert.jsp">
<table border="1">
<tr>
<th>아이디</th>
<td><input type="text" name="memberid" size="10" /></td>
<th>비밀번호</th>
<td><input type="password" name="password" size="10" /></td>
</tr>
<tr>
<th>이름</th>
<td><input type="text" name="name" size="10" /></td>
<th>이메일</th>
<td><input type="text" name="email" size="10" /></td>
</tr>
<tr>
<td colspan="4"><input type="submit" value="입력" /></td>
</tr>
</table>
</form>
</body>
</html>
insert.jsp
<%@page import="java.sql.SQLException"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.Statement"%>
<%@page import="java.sql.Connection"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
request.setCharacterEncoding("UTF-8");
String memberid = request.getParameter("memberid");
String password = request.getParameter("password");
String name = request.getParameter("name");
String email = request.getParameter("email");
out.print(memberid +","+ password
+","+ name +","+email);
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection conn = null;
//물음표 사용하기위해 써봄!!
PreparedStatement pstmt = null;
try{
conn = DriverManager.getConnection(
"jdbc:oracle:thin:@127.0.0.1:1521:xe"
,"jspexam"
,"java"
);
pstmt = conn.prepareStatement(
//pstmt는 괄호 안에 쿼리가 들어감
"INSERT INTO MEMBER(MEMBERID,PASSWORD,NAME,EMAIL)"
+" VALUES(?,?,?,?)"
);
pstmt.setString(1,memberid);
pstmt.setString(2,password);
pstmt.setString(3,name);
pstmt.setString(4,email);
pstmt.executeUpdate();
}catch(SQLException ex){
out.print(ex.getMessage());
}finally{
if(pstmt!=null)try{pstmt.close();}catch(SQLException ex){}
if(conn!=null)try{conn.close();}catch(SQLException ex){}
}
%>
<!DOCTYPE html>
<html>
<head>
<title>입력</title>
</head>
<body>
</body>
</html>