<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" isELIgnored="false" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<c:set var="contextPath" value="${pageContext.request.contextPath}" />
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>고객번호 조회 및 출력</title>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script>
function fn_process(){
var _id=$("#cust_id").val(); // 하단 input의 id의 cust_id를 var _id에 넣는다.
if(_id=='' || _id == null){ // var _id 널값 체크
alert("고객번호를 입력하세요");
return;
}
$.ajax({
type: "post",
async:false,
url:"${contextPath}/cus",
dataType : "text",
data: {cust_id: _id}, // 넘길 data를 set
success: function (data, textStatus){
var jsonInfo = JSON.parse(data);
$("#output_cust_id").html(jsonInfo.cust_id);
$("#output_cust_name").html(jsonInfo.cust_name);
$("#output_cust_address").html(jsonInfo.cust_address);
$("#output_cust_state").html(jsonInfo.cust_state);
$("#output_cust_zip").html(jsonInfo.cust_zip);
$("#output_cust_country").html(jsonInfo.cust_country);
$("#output_cust_contact").html(jsonInfo.cust_contact);
$("#output_cust_email").html(jsonInfo.cust_email);
},
error: function(data, textStatus){
alert("고객이 존재하지 않습니다.");
}
});
}
</script>
</head>
<body>
고객번호<input type="text" id="cust_id" >
<input type="button" id="checkJson" value="조회" onClick="fn_process()"><br><br>
<table border=1 align=left>
<tr>
<td>고객번호</td>
<td><div id="output_cust_id"></div></td>
</tr>
<tr>
<td>고객이름</td>
<td><div id="output_cust_name"></div></td>
</tr>
<tr>
<td>고객주소</td>
<td><div id="output_cust_address"></div></td>
</tr>
<tr>
<td>고객주</td>
<td><div id="output_cust_state"></div></td>
</tr>
<tr>
<td>고객우편번호</td>
<td><div id="output_cust_zip"></div></td>
</tr>
<tr>
<td>고객국가</td>
<td><div id="output_cust_country"></div></td>
</tr>
<tr>
<td>고객담당자</td>
<td><div id="output_cust_contact"></div></td>
</tr>
<tr>
<td>고객메일주소</td>
<td><div id="output_cust_email"></div></td>
</tr>
</table>
</body>
</html>
---------------------------------------
package day0828;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
/**
* Servlet implementation class JsonServlet3
*/
@WebServlet("/cus")
public class Customer extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doHandle(request, response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doHandle(request, response);
}
protected void doHandle(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setContentType("text/html; charset=utf-8");
PrintWriter writer = response.getWriter();
JSONObject customerInfo = new JSONObject();
CustomerDAO dao = new CustomerDAO();
String input_id = request.getParameter("cust_id");
List customerList = dao.listCustomers(input_id);
CustomerVO vo = (CustomerVO) customerList.get(0);
customerInfo.put("cust_id", vo.getCust_id());
customerInfo.put("cust_name", vo.getCust_name());
customerInfo.put("cust_address", vo.getCust_address());
customerInfo.put("cust_state", vo.getCust_state());
customerInfo.put("cust_zip", vo.getCust_zip());
customerInfo.put("cust_country", vo.getCust_country());
customerInfo.put("cust_contact", vo.getCust_contact());
customerInfo.put("cust_email", vo.getCust_email());
String jsonInfo = customerInfo.toJSONString();
System.out.println(jsonInfo);
writer.print(jsonInfo);
}
}
---------------------------------------
package day0828;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
public class CustomerDAO {
private static final String driver= "oracle.jdbc.driver.OracleDriver";
private static final String url = "jdbc:oracle:thin:@localhost:1521:XE";
private static final String user ="scott";
private static final String pwd = "tiger";
private Connection con;
private PreparedStatement pstmt; // 실무에선 PreparedStatement를 더 많이씀.
public List listCustomers(String input_id)
{
List list = new ArrayList();
try
{
connDB(); // 네가지 정보로 데이터베이스를 연결
String query = "SELECT cust_id, cust_name, cust_address, cust_state, cust_zip, cust_country, cust_contact, cust_email "
+ "FROM customers "
+ "where cust_id = '" + input_id + "'";
System.out.println("preparedStatement : " + query);
pstmt = con.prepareStatement(query); // 파생된 SQL 재사용. preparedStatement 메소드에 sql문을 전달해 prepareStatement객체를 생성.
ResultSet rs = pstmt.executeQuery(); // sql문으로 회원 정보를 조회
while(rs.next())
{
// 조회한 레코드의 각 컬럼 값을 받아옴.
String cust_id = rs.getString("cust_id"); // " " 안에는 컬럼명이 와야함.!!!!!!!!!!!!!!
String cust_name =rs.getString("cust_name");
String cust_address =rs.getString("cust_address");
String cust_state =rs.getString("cust_state");
String cust_zip =rs.getString("cust_zip");
String cust_country =rs.getString("cust_country");
String cust_contact =rs.getString("cust_contact");
String cust_email =rs.getString("cust_email");
// 각 컬럼 값을 다시 MemberVO 객체의 속성에 설정.
CustomerVO vo = new CustomerVO();
vo.setCust_id(cust_id);
vo.setCust_name(cust_name);
vo.setCust_address(cust_address);
vo.setCust_state(cust_state);
vo.setCust_zip(cust_zip);
vo.setCust_country(cust_country);
vo.setCust_contact(cust_contact);
vo.setCust_email(cust_email);
list.add(vo); // 설정된 MemberVO 객체를 다시 ArrayList에 저장.
}
rs.close();
pstmt.close();
con.close();
} catch (Exception e)
{
e.printStackTrace();
}
return list; // 조회한 레코드의 개수만큼 MemberVO객체를 저장한 ArrayList를 반환.
}
private void connDB()
{
try
{
Class.forName(driver);
System.out.println("Oracle 드라이버 로딩 성공");
con = DriverManager.getConnection(url, user, pwd);
System.out.println("Connection 생성 성공");
} catch (Exception e)
{
e.printStackTrace();
}
}
}
---------------------------------------
package day0828;
public class CustomerVO {
private String cust_id;
private String cust_name;
private String cust_address;
private String cust_state;
private String cust_zip;
private String cust_country;
private String cust_contact;
private String cust_email;
public String getCust_id() {
return cust_id;
}
public void setCust_id(String cust_id) {
this.cust_id = cust_id;
}
public String getCust_name() {
return cust_name;
}
public void setCust_name(String cust_name) {
this.cust_name = cust_name;
}
public String getCust_address() {
return cust_address;
}
public void setCust_address(String cust_address) {
this.cust_address = cust_address;
}
public String getCust_state() {
return cust_state;
}
public void setCust_state(String cust_state) {
this.cust_state = cust_state;
}
public String getCust_zip() {
return cust_zip;
}
public void setCust_zip(String cust_zip) {
this.cust_zip = cust_zip;
}
public String getCust_country() {
return cust_country;
}
public void setCust_country(String cust_country) {
this.cust_country = cust_country;
}
public String getCust_contact() {
return cust_contact;
}
public void setCust_contact(String cust_contact) {
this.cust_contact = cust_contact;
}
public String getCust_email() {
return cust_email;
}
public void setCust_email(String cust_email) {
this.cust_email = cust_email;
}
}
---------------------------------------