2014년 6월 19일 목요일

[Java]MVC란

  • MVC
모델,뷰,컨트롤 영역으로 코드 구현 시에 화면, 제어로직 및 데이터를 분리 하여 중앙 집중적인 제어 및 처리가 가능하도록 패턴화 하여 개발자와 디자이너의 분업이 더 잘되고 재사용성이 좋게 제공하는 것을 말함

[SPRING]DI(Dependency Injection) and Decoupling

  • DI(Dependency Injection)
Spring 프레임워크에서 지원하는 IoC의 형태이다. DI는 클래스 사이의 의존관계를 빈 설정 정보를 바탕으로 컨테이너가 자동적으로 연결해주는 것
  • Decoupling
클래스 간의 연결 고리를 없애고 클래스간의 연결은 인터패이스로 구성하여 클래스간의 의존성을 떨어트려 유지보수에 용이하게 하는 것

[Framework]전자정부프레임워크


  • 전자정부프레임워크
대한민국의 공공부문 정보화 사업 시 플랫폼별 표준화된 개발 프레임워크를 뜻하며
기업에서 워낙에 많은 자체 프레임워크기술로 개발을 하다보니 결과의 수준과 질이 차이가 많이 나게 되었습니다. 때문에자바 플렛폼 기반의 공공기관, 정보화 산업에 기업 자체 프레임워크를 사용하게 되면 유지 보수등의 문제가 있어 이를 스프링을 기반으로 한 프레임워크를 출시하게 되었으며 이를 전자 정부 표준 프레임워크라고 함

[JavaScript]JSON이란


  • JSON

자바스크립트 객체의 형식을 따르며 데이터를 쉽게 저장하고 사용하기 위한 객체
자바에 비유하자면 map과 유사 
Key와 value가 맵핑됨
선언 방법은 var json ={ key : "helloJamo"}; 처럼 선언 및 저장이되며 

json.key 같이 변수면에 key값을 입력하면 helloJamo 라는 데이터가 튀어나오게 되는 원리


본래는 서버에서 xml파일을 받아서 필요한 데이터를 분리하여 사용하엿으나 
분리작업이 복잡하기때문에 요즘은 json을 많이 이용하며 jquery 등 라이브러리에서 
셋팅값을 설정시에도 이 json객체를 많이 이용을 하고 있습니다

[Java]Library and Framework

  • 라이브러리
A클래스가 객체를 만들어 메서드를 사용할때에 해당 객체를 라이브러리의 것으로부터 사용
  • 프레임워크
A클래스의 라이프사이클이 프레임워크에 종속되는것을 의미(제어의 역전, IoC)


라이브러리 = 대여, 프레임워크 = 귀속 의 개념같다. 

[Framework]서버사이드 언어와 Framwork


  • PHP
    • 장점
      1. 빠르다
      2. 쉽다
      3. C와 비슷하다(C사용자들에겐 편하다)
      4. 계속 발전한다
    • 단점
      • 기술지원 불가능
    • 비용
      • 공짜
    • 대표 Framework
      • CodeIgniter
    • DBMS
      • MySQL(Oracle, MSSQL)

  • JSP
    • 장점
      1. 인력이 많다(고용되는 입장에선 단점)
      2. 자바로 되어있어 이식성이 좋다
    • 단점
      1. 느리다
      2. 서버속성을 많이 탄다
      3. 인건비가 싸다(고용하는 입장에는 장점)
    • 비용
      • 처음에는 공짜이나 나중에는 많이 비쌈
    • 대표 Framework
      • Spring
    • DBMS
      • MySQL, Oracle, MSSQL
  • ASP.NET
    • 장점
      1. MS의 지원
      2. 쉽다
      3. 안정성
    • 단점
      1. MS의 기술종속
      2. 사용자가 많아지면 느려짐(비싼데...)
    • 비용
      • 비쌈
    • 대표 Framework
      • ASP.NET MVC
    • DBMS
      • Oracle, MSSQL
PHP의 단점만 완화시킬수 있다면 좋을듯
실제로 Facebook은 PHP를 개조해서 사용하고 있다고하니....

2014년 6월 10일 화요일

[SPRING]스프링을 통해 게시판(DB설계)

http://java-school.net/jsp-pjt/01.php

SYS 계정으로 새로운 사용자를 만든다.
kim@kim-android:~$ sqlplus scott/tiger

다음에 접속됨:
Oracle Database 10g

SQL> connect sys as sysdba
암호 입력: 
연결되었습니다.
SQL> show user
USER은 "SYS"입니다
SQL> create user javaschool 
     identified by jspproject default tablespace users;

SQL> grant connect,resource to javaschool;

javaschool 계정에 접속한 후 아래 내용으로 테이블과 시퀀스를 만든다.
-- 회원테이블 
create table member (
 email varchar2(60) primary key,
 passwd varchar2(20) NOT NULL,
 name varchar2(20) NOT NULL,
 mobile varchar2(20)
);

-- 게시판 종류
create table board (
 boardcd varchar2(20),
 boardnm varchar2(40) not null,
 constraint board_boardcd_pk primary key(boardcd)
);

-- 게시글
create table article (
 articleno number,
 boardcd varchar2(20),
 title varchar2(200) not null,
 content1 varchar2(4000),
 content2 varchar2(4000),
 content3 varchar2(4000),
 content4 varchar2(4000),
 content5 varchar2(4000),
 email varchar2(60),
 hit number, 
 regdate date,
 constraint article_articleno_pk primary key(articleno),
 constraint article_boardcd_fk foreign key(boardcd) references board(boardcd)
);

-- 게시글 번호 생성기 
create sequence article_articleno_seq
increment by 1
start with 1;


-- 덧글
create table comments (
 commentno number,
 articleno number, 
 email varchar2(60), 
 memo varchar2(4000),
 regdate date, 
 constraint comment_commentno_pk primary key(commentno)
);

-- 덧글 번호 생성기
create sequence comments_commentno_seq
increment by 1
start with 1;


-- 첨부파일 
create table attachfile (
 attachfileno number,
 filename varchar2(50) not null,
 filetype varchar2(30),
 filesize number,
 articleno number,
 constraint attachfile_attachfileno_pk primary key(attachfileno)
);

-- 첨부파일 번호 생성기
create sequence attachfile_attachfileno_seq
increment by 1
start with 1;

insert into board values ('free','자유게시판');
insert into board values ('qna','QnA게시판');
insert into board values ('data','자료실');
commit;                                       

톰캣 DataSource 설정

JSPProject.xml 수정

CATALINA_HOME/conf/Catalina/localhost/JSPProject.xml 을 열고 아래와 같이 추가한다.
<?xml version="1.0" encoding="UTF-8"?>
<Context
 path="/JSPProject"
 docBase="C:/www/JSPProject/WebContent"
 reloadable="true">
 
 <Resource
  name="jdbc/javaschool"
  auth="Container"
  type="javax.sql.DataSource"
  username="javaschool"
  password="jspproject"
  driverClassName="oracle.jdbc.driver.OracleDriver"
  url="jdbc:oracle:thin:@127.0.0.1:1521:orcl" />
 
</Context>

WEB-INF/web.xml 편집

/WEB-INF/web.xml 파일에 아래를 추가한다.
<?xml version="1.0" encoding="ISO-8859-1"?>
<!--
 Licensed to the Apache Software Foundation (ASF) under one or more
  contributor license agreements.  See the NOTICE file distributed with
  this work for additional information regarding copyright ownership.
  The ASF licenses this file to You under the Apache License, Version 2.0
  (the "License"); you may not use this file except in compliance with
  the License.  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
-->

<web-app xmlns="http://java.sun.com/xml/ns/javaee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
                      http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
  version="3.0"
  metadata-complete="true">
 
 <session-config>
  <session-timeout>-1</session-timeout>
 </session-config>
 
 <welcome-file-list>
  <welcome-file>index.html</welcome-file>
  <welcome-file>index.htm</welcome-file>
  <welcome-file>index.jsp</welcome-file>
  <welcome-file>default.html</welcome-file>
  <welcome-file>default.htm</welcome-file>
  <welcome-file>default.jsp</welcome-file>
 </welcome-file-list>
  
 <resource-ref>
  <description>ConnectionPool for JSP Mini Project</description> 
  <res-ref-name>jdbc/javaschool</res-ref-name>
  <res-type>javax.sql.DataSource</res-type>
  <res-auth>Container</res-auth>
 </resource-ref>

</web-app>

CATALINA_HOME/common/lib/ 에 오라클 JDBC 드라이버 파일 ojdbc14.jar 복사

CATALINA_HOME/common/lib/ 에 오라클 JDBC 드라이버 파일인 ojdbc14.jar 을 복사한다.
톰캣 7인 경우 CATALINA_HOME/lib 에 복사한다.

테스트

Tomcat을 재시작한 후 아래 내용으로 test.jsp 파일을 WebContent 디렉토리에 생성한다.
test.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" 
 pageEncoding="UTF-8"%>
<%@ page import="java.sql.*,javax.sql.*,javax.naming.*" %>
<%
 Connection con = null;
 PreparedStatement pstmt = null;
 ResultSet rs = null;
 
 DataSource ds = null;
 
 int totalRecord = 0;
  
 try {
  Context ic = new InitialContext();
  Context envCtx = (Context) ic.lookup("java:comp/env");
  ds = (DataSource) envCtx.lookup("jdbc/javaschool");
 } catch (NamingException e) {
  System.out.println(e.getMessage());
 }
 
 try {
  con = ds.getConnection();
  
  String sql = "SELECT count(*) FROM board";
  
  pstmt = con.prepareStatement(sql);
  rs = pstmt.executeQuery();
  rs.next();
  totalRecord = rs.getInt(1);
 } catch (SQLException e) {
  System.out.println(e.getMessage());
 } finally {
  try {
   rs.close();
  } catch (SQLException e) {
   e.printStackTrace();
  }
  try {
   pstmt.close();
  } catch (SQLException e) {
   e.printStackTrace();
  }
  try {
   con.close();
  } catch (SQLException e) {
   e.printStackTrace();
  }
 }
%> 
<!DOCTYPE html 
     PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head>
    <title>DataSource 테스트</title>
 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 
</head>
<body>
<p>
<%=totalRecord %>
</p>
</body>
</html>
웹브라우저에서 http://localhost:8080/JSPProject/test.jsp 방문하여 3이 나오면 테스트가 성공한 것이다.