< DBConnection.java >

 

package test.db;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class DBConnection {
 public static Connection getCon() throws SQLException{
  Connection con=null;
  try{
   Class.forName("oracle.jdbc.OracleDriver");
   String url="jdbc:oracle:thin:@localhost:1521:XE";
   con= DriverManager.getConnection(url,"SCOTT","TIGER");
   return con;
  }catch(ClassNotFoundException ce){
   System.out.println(ce.getMessage());
   return null;
  }
 }
}

 

1. 초기화면

 

2. 1을 등록하면 바로 옆에 검증 후 메시지 보여줌.

 3. 2을 등록하면 가능하다고 보여줌.

 

4. 관련 테이블 데이터

 

; 데이터로 ID 1이 들어가 있음.

 

 

< insert.html  >

 

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<title>Insert title here</title>
<style type="text/css">
 #idcheck{
  color:red;
  font-size:10pt;
 }
</style>
<script type="text/javascript">
 var xhr=null;
 
 function getXHR(){
  if(window.XMLHttpRequest){
   //IE 7.0이상,Chrome,Firefox,Safari,....
   return new XMLHttpRequest();
  }else{
   //IE 하위버젼(6.0이하)
   return new ActiveXObject("Microsoft.XMLHTTP");
  }
 }
 
 function idCheck(){
  xhr=getXHR();
  xhr.onreadystatechange=getResult;
  var id=document.getElementById("id").value;
  xhr.open("get","idcheck.jsp?id="+id,true);
  xhr.send(null);
 }
 
 function getResult(){
  if(xhr.readyState==4 && xhr.status==200){
   var xml=xhr.responseXML;
   var re=xml.getElementsByTagName("result")[0].firstChild.nodeValue;
   
   if(re=='true'){
    //span에 결과 출력하기
    document.getElementById("idcheck").innerHTML="이미 사용중인 아이디 입니다.";
   }else{
    document.getElementById("idcheck").innerHTML="사용가능한 아이디 입니다.";
   }
  }
 }
</script>
</head>
<body>
<h3>회원가입</h3>
<form method="post" action="insert.jsp">
 아이디 <input type="text" name="id" id="id" onkeyup="idCheck()"/>
 <!-- <input type="button" value="중복확인" onclick="idCheck()"/>  -->
 <span id="idcheck"></span>
 <br/>
 비밀번호 <input type="password" name="pwd"/><br/>
 이메일<input type="text" name="email"/><br/>
 <input type="submit" value="회원가입"/>
</form>
</body>
</html>

 

 

< idcheck.jsp >

 

<%@page import="java.io.PrintWriter"%>
<%@page import="java.sql.SQLException"%>
<%@page import="test.db.DBConnection"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.Connection"%>
<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<%
 //1.사용자가 입력한 아이디 얻어오기
 String id=request.getParameter("id");
 //2.해당아이디가 존재하는지 검사
 Connection con=null;
 PreparedStatement pstmt=null;
 ResultSet rs=null;
 boolean result=false;
 try{
  con=DBConnection.getCon();
  String sql="select * from members where id=?";
  pstmt=con.prepareStatement(sql);
  pstmt.setString(1, id);
  rs=pstmt.executeQuery();
  if(rs.next()){
   result=true;
  }
 }catch(SQLException se){
  System.out.println(se.getMessage());
  
 }finally{
  rs.close();
  pstmt.close();
  con.close();
 }
 
 ////////// 3.xml로 결과 응답하기 ///////////
 response.setContentType("text/xml;charset=euc-kr");
 PrintWriter pw=response.getWriter();
 pw.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
 pw.println("<data>");
 pw.println("<result>" + result + "</result>");
 pw.println("</data>");
 pw.close();
 
%>

 

 

 

 

 

 

 

 

 

 

 

'WEB > Ajax Json' 카테고리의 다른 글

AJAX #08 JSON  (0) 2013.05.04
AJAX #07 JSON  (0) 2013.05.04
AJAX #06 JSON 관련 jar, javascript파일  (0) 2013.05.04
AJAX #05 JSON  (0) 2013.05.04
AJAX #04 댓글 달기 예제  (1) 2013.05.04

+ Recent posts