문자형 데이터 타입과 문자 상수 비교 방법

 

*비교 방법

 

-문자 상수는 문자 데이터 타입으로 내부 변환되어 문자 타입의 비교 방식에 의해 처리

-주의

。동일한 문자 상수를 비교하는 경우에도 비교 대상 칼럼의 데이터 타입에 따라 비교 결과가 달라질 수 있음

-문자 상수와 CHAR 데이터 타입 비교

。문자 상수를 CHAR 타입으로 변환한 후, CHAR와 CHAR 비교 방식으로 비교

-문자 상수와 VARCHAR2 데이터 타입 비교

。문자 상수를 VARCHAR2 타입으로 변환한 후 VARCHAR2와 VARCHAR2 비교 방식에 의해 비교

 

 

 

 

문자형 데이터 타입의 비교 방법

 

*CHAR와 CHAR 데이터 타입의 비교 방법

 

 

 *CHAR와 VARCHA2 데이터 타입의 비교 방법

 

VARCHAR2 데이터 타입

 

*개요

-가변 길이의 문자열을 저장하기 위해 사용하는 데이터 타입

-최대 4,000 바이트 저장 가능

-지정된 길이보다 짧은 문자열이 입력되면 뒷부분은 NULL로 처리되어 저장공간을 낭비하지 않음

 

 

-특징

。데이터 입력시 사용자가 값을 입력하지 않으면 NULL이 입력

。지정된 길이보단 긴 데이터가 입력되면 데이터가 입력되지 않고 오류 발생

。입력될 데이터의 편차가 심하거나 NULL이 많이 입력되는 경우에 사용하는 것이 효율적

。실무에서는 CHAR 데이터 타입보다 VARCHAR2를 많이 사용

 

 

 

 

CHAR 데이터 타입

 

*개요

-고정 길이의 문자열을 저장하며 최대 2,000바이트까지 저장 가능

-지정된 길이보다 짧은 데이터가 입력되는 경우, 나머지 공간은 공백으로 채워짐

 

 

-특징

。데이터 입력시 사용자가 데이터를 입력하지 않으면 NULL이 입력

。지정된 길이보다 긴 데이터가 입력되면 오류 발생

。주소 데이터와 같은 편차가 심한 데이터를 입력할 때 사용하면 저장공간이 낭비될 수 있음

。주민등록번호와 같이 길이가 일정하거나 비슷한 경우에 사용하는 것이 좋음

 

 

 

데이터 타입의 종류

 

[표4.1] 오라클에서 지원하는 데이터 타입의 종류

 

 

헤쉬테이블(HashTable) 사용 예(1)




import java.util.Hashtable;

public class HashtableTest {

public static void main(String[] args) {

Hashtable hashtable = new Hashtable();


//1. hashtable에 객체의 삽입 

hashtable.put("Name", new String("홍길동"));

hashtable.put("Age", new Integer(27));

hashtable.put("Tel", new String("02-1111-2222"));

hashtable.put("Handphone", new String("017-777-9999"));

hashtable.put("Etc", new String("I'm a boy"));


//키 값을 이용하여 객체 추출

String name = (String)hashtable.get("Name");

if (name != null) {

 System.out.println("Name = " + name);

}

 

Integer age = (Integer)hashtable.get("Age");

if (age != null) {

 System.out.println("Age = " + age.intValue());

}

}

}

헤쉬테이블(HashTable)





nHashTableMap 인터페이스를 구현

n해시테이블을 내부적으로 구현하여 데이터 저장의 역할을 함

n키와 값을 한 쌍으로 대입하므로 키를 이용해 값을 찾을 수 있다

n유일한 키 값을 가지고 있음

- 객체를 삽입할때 특별한 키 값 같이 입력

- 객체를 추출할 때 키값을 이용하여 추출

- put(Object key, Object value)메서드로 객체 삽입

- get(Object key)메서드로 키가 맵핑된 값 리턴


 

Hashtable hashtable = new Hashtable();

hashtable.put(Name, new String(홍길동));

hashtable.put(Age, new Integer(29));

 

String name = (String) hashtable.get(Name);

Integer age = (Integer)hashtable.get(Age);

 


벡터(Vector) 사용 예(2)




import java.util.*;

public class StudentVectorTest {

public static void main(String[] args) {

Vector v=new Vector();     //capacity 10 increasing *2

      //Vector v=new Vector(5);  //capacity 5 increasing *2

      //Vector v=new Vector(5,5);//capacity 5 increasing 5

      v.add(new Student("Jee",1,"Seoul"));//0

      v.clear();//모두 제거

      v.add(new Student("Gong",2,"Seoul"));//1

      v.add(new Student("Song",3,"Seoul"));//2

      v.add(new Student("Lee",4,"Koyang"));//3

      v.add(new Student("Lee",4,"Koyang"));//3과 중복허용

      System.out.println(v.size()+"  "+v.capacity());

      System.out.println(v.contains(new Student("Lee",25,"Koyang")));


     Student stu=(Student)v.elementAt(2);//index0부터

      System.out.println(stu.getId()+"  "+stu.getName()+"

 "+stu.getAddr());

      //모든 elements 출력하기

      Enumeration enums=v.elements();

      while(enums.hasMoreElements()){

      Student stus=(Student)enums.nextElement() ;

      System.out.println(stus.getId()+"  "+stus.getName()+"  "+

                              stus.getAddr());


     }

}


}

+ Recent posts