[새싹] 현대IT&E 231025 기록 - JAVA 18~19장 / Oracle

최정윤·2023년 10월 25일
0

새싹

목록 보기
7/67
post-custom-banner

SQLdeveloper 설치하기

oracle.com 설치

oracle > product > hardware and software > oracle database > applicationdevelopment > oracle sql developer > download sql developer > download
https://www.oracle.com/database/sqldeveloper/technologies/download/

SQLmodeler 설치하기

https://www.oracle.com/database/sqldeveloper/technologies/sql-data-modeler/download/

oracle21c.xe 설치

https://www.oracle.com/kr/database/technologies/xe-downloads.html
https://velog.io/@devsaza/M1-M2-Mac-OS%EC%97%90%EC%84%9C-Oracle-DB-%EC%82%AC%EC%9A%A9%ED%95%98%EA%B8%B0

colima stop
colima start --memory 4 --arch x86_64
colima list
net start OracleOraDB21Home1TNSListener
net start OracleServiceXE

강사님 파일

=============================
 Oracle 21c XE 설치 후
=============================

  C:\Users\COM> notepad xe_start.bat

    net start OracleOraDB21Home1TNSListener
    net start OracleServiceXE


  C:\Users\COM> notepad xe_stop.bat

    net stop OracleServiceXE
    net stop OracleOraDB21Home1TNSListener

  C:\Users\COM> xe_stop
  C:\Users\COM> xe_start


# Connecting to a Pluggable Database (PDB)

  참고.https://oracle-base.com/articles/12c/multitenant-connecting-to-cdb-and-pdb-12cr1#pdb
  
  [1] 설치 뒤 SQL*Plus로 접속

  C:\Users\COM> sqlplus / as sysdba         <- CDB에 접속하는 것임

  SQL> col name format a30
  SQL> col pdb  format a30

  SQL> select name, pdb
       from   v$services
       order  by name;

  NAME                           PDB
  ------------------------------ ------------------------------
  SYS$BACKGROUND                 CDB$ROOT
  SYS$USERS                      CDB$ROOT
  xe                             CDB$ROOT   <- CDB(Container Database)
  xeXDB                          CDB$ROOT
  xepdb1                         XEPDB1     <- PDB(Pluggable Database), 우리가 사용할 DB

  SQL> show con_name

  CON_NAME
  ------------------------------
  CDB$ROOT

  SQL> alter session set container = xepdb1;

  SQL> show con_name

  SQL> exit

  C:\Users\COM> sqlplus system/oracle@localhost:1521/xepdb1     <- PDB에 관리자로 접속

  SQL> show con_name

  CON_NAME
  ------------------------------
  XEPDB1

  SQL> create user ace
       identified by ace
       default tablespace users
       temporary tablespace temp;

  SQL> alter user ace
       quota unlimited on users;

  SQL> grant connect, 
             resource,
             create session, 
             create table,
             create procedure,
             create trigger,
             create view,
             create sequence
       to ace;
      
  SQL> exit

  C:\Users\COM> sqlplus ace/ace@localhost:1521/xepdb1

  SQL> show con_name

  CON_NAME
  ------------------------------
  XEPDB1

  SQL> show user
  USER은 "ACE"입니다

18장. 람다식

18.2.2 메서드 참조

  • 추상 메서드를 직접 구현하는 대신, 이미 구현이 완료된 메서드를 참조하는 것이다.

RefOfInstanceMethod_Type2_2.java

package lambda;

interface A {
	int abc(String str);
}

public class RefOfInstanceMethod_Type2_2 {
	public static void main(String[] args) {
		A a1 = new A() {
			@Override
			public int abc(String str) {
				return str.length();
			}
		};
		
		A a2 = (String str) -> str.length();
		
		A a3 = String::length;
		System.out.println(a1.abc("안녕"));
		System.out.println(a2.abc("안녕"));
		System.out.println(a3.abc("안녕"));
	}
}

18.2.3 생성자 참조

  • 생성자 참조에서 참조하는 생성자는 크게 배열 객체 생성자와 클래스 객체 생성자로 나뉜다.

배열 생성자 참조

RefOfArrayConstructor.java

package lambda;

interface AA {
	int[] abc(int len);
}

public class RefOfArrayConstructor {
	public static void main(String[] args) {
		AA a1 = new AA() {
			@Override
			public int[] abc(int len) {
				return new int[len];
			}
		};
		AA a2 = (int len) -> {
			return new int[len];
		};
		
		AA a3 = int[]::new;
		int[] array1 = a1.abc(3);
		System.out.println(array1.length);
		int[] array2 = a1.abc(3);
		System.out.println(array2.length);
		int[] array3 = a1.abc(3);
		System.out.println(array3.length);
	}
}

클래스 생성자 참조

RefOfClassConstructor_2.java

package lambda;

interface AAA {
	BBB abc(int k);
}
class BBB{
	BBB(){
		System.out.println("첫 번째 생성자");
	}
	BBB(int k){
		System.out.println("두 번째 생성자");
	}
}

public class RefOfClassConstructor_2 {
	public static void main(String[] args) {
		AAA a1 = new AAA() {
			@Override
			public BBB abc(int k) {
				return new BBB(3);
			}
		};
		AAA a2 = (int k) -> new BBB(3);
		AAA a3 = BBB::new;
		a1.abc(3);
		a2.abc(3);
		a3.abc(3);
	}

}

19장. 자바 입출력

19.1 파일 관리와 문자셋

19.1.1 자바로 파일 관리하기

파일 객체로 생성하기

  • 자바의 File 클래스는 파일과 폴더를 관리하는 클래스로, 4개의 생성자 중 하나를 이용해 객체를 생성한다.
    • File(String pathname)
    • File(File parent, String child)
    • File(String parent, String child)
    • File(URI uri)

CreateFileObject.java

package file;

import java.io.File;
import java.io.IOException;

public class CreateFileObject {
	public static void main(String[] args) throws IOException {
		File tempDir = new File("/Users/jeong-yoon/temp");
		if(!tempDir.exists())
			tempDir.mkdir();
		System.out.println(tempDir.exists());
		File newFile = new File("/Users/jeong-yoon/temp/newFile.txt");
		if(!newFile.exists())
			newFile.createNewFile();
		System.out.println(newFile.exists());
		System.out.println();
		
		File newFile2 = new File("\\Users\\jeong-yoon\\temp\\newFile.txt");
		File newFile3 = new File("/Users/jeong-yoon/temp/newFile.txt");
		File newFile4 = new File("Users" + File.separator + "jeong-yoon" + File.separator + "temp" + File.separator + "newFile.txt");
		
		System.out.println(newFile2.exists());
		System.out.println(newFile2.exists());
		System.out.println(newFile2.exists());
		System.out.println();
		
		File newFile5 = new File("/Users/jeong-yoon/abc/newFile.txt");
		File newFile6 = new File("/Users/jeong-yoon/abc/bcd/newFile.txt");
		System.out.println(newFile5.getAbsolutePath());
		System.out.println(newFile6.getAbsolutePath());
		
		System.out.println(System.getProperty("user.dir"));
		File newFile7 = new File("newFile1.txt");
		File newFile8 = new File("bcd/newFile2.txt");
		System.out.println(newFile7.getAbsolutePath());
		System.out.println(newFile8.getAbsolutePath());
	}

}

파일 클래스의 주요 메서드

package file;

import java.io.File;

public class FileMethods {
	public static void main(String[] args) {
		File tempDir = new File("/Users/jeong-yoon/temp");
		if(!tempDir.exists())
			tempDir.mkdir();
		File file = new File("/Users/jeong-yoon");
		
		System.out.println("절대 경로: " + file.getAbsolutePath());
		System.out.println("폴더(?): " + file.isDirectory());
		System.out.println("파이(?): " + file.isFile());
		System.out.println("파일/폴더: " + file.getName());
		System.out.println("부모 폴더: " + file.getAbsolutePath());
		File newfile1 = new File("/Users/jeong-yoon/temp/abc");
		System.out.println(newfile1.mkdir());
		File newfile2 = new File("/Users/jeong-yoon/temp/bcd/cde");
		System.out.println(newfile2.mkdir());
		System.out.println(newfile2.mkdirs());
		File[] fnames = file.listFiles();
		for(File fname: fnames) {
			System.out.println((fname.isDirectory()? "폴더: ":"파일: ") + fname.getName());
		}
	}
}

19.1.2 자바에서 문자셋 이용하기

한글 전용 문자셋 - EUC-KR, MS949

EUCKRvsMS949.java

package file;

import java.io.UnsupportedEncodingException;

public class EUCKRvsMS949 {
	public static void main(String[] args) throws UnsupportedEncodingException{
		byte[] b1 = "a".getBytes("EUC-KR");
		byte[] b2 = "a".getBytes("MS949");
		System.out.println(b1.length);
		System.out.println(b2.length);
		System.out.println(new String(b1, "EUC-KR"));
		System.out.println(new String(b2, "MS949"));
		System.out.println();
		byte[] b3 = "가".getBytes("EUC-KR");
		byte[] b4 = "가".getBytes("MS949");
		System.out.println(b3.length);
		System.out.println(b4.length);
		System.out.println(new String(b3, "EUC-KR"));
		System.out.println(new String(b4, "MS949"));
		System.out.println();
		byte[] b5 = "봵".getBytes("EUC-KR");
		byte[] b6 = "봵".getBytes("MS949");
		System.out.println(b5.length);
		System.out.println(b6.length);
		System.out.println(new String(b5, "EUC-KR"));
		System.out.println(new String(b6, "MS949"));
		System.out.println();
	}
}

대표적인 유니코드 문자셋 - UTF-16, UTF-8

UTF16vsUTF8.java

package file;

import java.io.UnsupportedEncodingException;

public class UTF16vsUTF8 {
	public static void main(String[] args) throws UnsupportedEncodingException {
		byte[] b1 = "abc".getBytes("UTF-16");
		byte[] b2 = "abc".getBytes("UTF-8");
		System.out.println(b1.length);
		System.out.println(b2.length);
		for(byte b: b1)
			System.out.printf("%02X ", b);
		System.out.println();
		for(byte b: b2)
			System.out.printf("%02X ", b);
		System.out.println();
		System.out.println(new String(b1, "UTF-16"));
		System.out.println(new String(b2, "UTF-8"));
		System.out.println();
		byte[] b3 = "가나다".getBytes("UTF-16");
		byte[] b4 = "가나다".getBytes("UTF-8");
		System.out.println(b3.length);
		System.out.println(b4.length);
		for(byte b: b3)
			System.out.printf("%02X", b);
		System.out.println();
		for(byte b: b3)
			System.out.printf("%02X", b);
		System.out.println();
		for(byte b: b4)
			System.out.printf("%02X", b);
		System.out.println();
		System.out.println(new String(b3, "UTF-16"));
		System.out.println(new String(b4, "UTF-8"));
		System.out.println();
	}

}

CreateChasetObject.java

package file;
import java.nio.charset.Charset;

public class CreateChasetObject {
	public static void main(String[] args) {
		Charset cs1 = Charset.defaultCharset();
		Charset cs2 = Charset.defaultCharset();
		Charset cs3 = Charset.defaultCharset();
		
		System.out.println(cs1);
		System.out.println(cs2);
		System.out.println(cs3);
		System.out.println();
		System.out.println(Charset.isSupported("MS949"));
		System.out.println(Charset.isSupported("UTF-8"));
	}

}

19.2 byte 단위 입출력

19.2.1 byte 단위 입출력과 char 단위 입출력

FileInputStream_1.java

package file;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

public class FileInputStream_1 {
	public static void main(String[] args) throws IOException {
		File inFile = new File("src/sec02_fileinputoutputstream/files/FileInput-Stream1.txt");
		InputStream is = new FileInputStream(inFile);
		int data;
		while((data = is.read()) != -1) {
			System.out.println("읽은 데이터: " + (char)data + "남은 바이트 수: " + is.available());
		}
		is.close();
	}
}

19.2.2 InputStream과 OutStream의 상속 구조

BufferedInputOutputStream.java

package file;

import java.io.File;
import java.io.OutputStream;

public class BufferedInputOutputStream {
	public static void main(String[] args) {
		File orgfile = new File("src/sec02_fileinputoutputstream/files/mycat_ori-gin.jpg");
		File copyfile1 = new File("src/sec02_fileinputoutputstream/files/mycat_copy1.jpg");
		File copyfile2= new File("src/sec02_fileinputoutputstream/files/mycat_copy2.jpg");
		long start, end, time1, time2;
		start = System.nanoTime();
		try(InputStram is = new FileInputStream(orgfile);
				OutputStream os = new FileOutputStream(copyfile1);){
			int data;
			while((data = is.read(()) != -1){
				os.write(data);
			}
		}
	}
}

19.3 char 단위 입출력

19.3.6 InputStreamReader/OutputStreamWriter로 Reader/Writer 객체 생성하기

OutputStreamWriter_1.java

package file;

import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.io.OutputStream;

public class OutputStreamWriter_1 {
	public static void main(String[] args) {
		File outputStreamWriter1 = new File("src/sec05_readerwriter/files/OutputStreamWriter1.txt");
		try(Writer writer = new FileWriter(outputStreamWriter1);) {
			writer.write("OuputStreamWriter1 예제파일입니다.\n".toCharArray());
			writer.write("한글과 영문이 모두 포함돼 있습니다.");
			writer.write('\n');
			writer.write("Good Bye!!!n\n");
			writer.flush();
		} catch(IOException e) {}
		File outputStreamWriter2 = new File("src/sec05_readerwriter/files/Output-StreamWriter2.txt");
		try(OutputStream os = new FileOutputStream(outputStreamWriter2, false);
				OutputStreamWriter osw = new OutputStreamWriter(os, "MS959");){
				osw.write("OutputStreamWriter1 예제파일입니다.\n".toCharArray());
				osw.write("한글과 영문이 모두 포함돼 있습니다.");
				osw.write('\n');
				osw.write("Good Bye!!!\n\n");
				osw.flush();
				System.out.println(osw.getEncoding());
		} catch(IOException e) {}
	}

}
profile
개발 기록장
post-custom-banner

0개의 댓글