컨트롤러를 사용하면
자동으로 컴포넌트로 인식하여 스프링이 생성되면서 내장한 컴포넌트 스캔이 작동
추상화 공통점을 추려냄
패키지가 다르면 임포트가 필요함,
같은 패키지면 static이 아니면 생성만 하면됨
private는 변수에
public은 메소드에
같은 폴더 패키지라면 default라서
public이 없어도 작동하지만 항상 가져와서 쓸 것을 생각하여 메소드에 퍼블릭을 붙여준다.
동일 패키지가 아닌경우가 많기 때문에 private를 적지 않고 default 상태로 비슷한 경험이 될 수 있다.
getter setter 는 이클립스 우클릭> 소스> 제네릭 게터 세터 클릭> 자동 완성됨.
다형성
부모로 선언 자식으로 생성
pulic append(List list){
return list.add(10);
}
list list1 = new ArrayList();
list list2 = new LinkedList();
list list3 = new Vector();
Map map1 = new HashMap();
Map map2 = new ConcurrentHashMap();
Map map3 = new Hashtable();
append(list1)
append(list2)
append(list3) 부모형식으로 만들어 져서 대입 가능
pulic append(ArrayList list){
return list.add(10);
} 로 만들경우
list 1,2,3은 list 형식이기 때문에 형변환을 통해 대입가
● 현실세계 : 설계도 è 객체 ● 자바 : 클래스 è 인스턴스 ● 클래스에는 객체를 생성하기 위한 필드와 메소드가 정의 ● 클래스로부터 만들어진 객체를 해당 클래스의 인스턴스(instance) 라고 함 ● 하나의 클래스로부터 여러 개의 인스턴스를 만들 수 있음
Override 부모의 메소드를 재정의 하는것
Overload 같은 이름의 메소드를 여러개 만들어 두는
추상클래스, 상속
상속 = Extends 재정의, 추가
추상클래스 = 구현목적 오버라이드(필수)
class=> class extends
interface -> interface extends
interfave -> class implements
class-> interface x
public class 데이터수집1 {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
URL url = new URL("http://ggoreb.com/hrd");
URLConnection con = url.openConnection();
InputStream in = con.getInputStream();
String result = "";
while(true) {
int data = in.read();
if(data == -1) break;
char c = (char) data;
result += c;
}
System.out.println(result);
public class 데이터수집2 {
public static void main(String[] args) throws IOException {
URL url = new URL("http://ggoreb.com/quiz/%EC%9A%B4%EC%88%98%EC%A2%8B%EC%9D%80%EB%82%A0.txt");
URLConnection con = url.openConnection();
InputStream in = con.getInputStream();
InputStreamReader isr = new InputStreamReader(in, "euc-kr");
BufferedReader reader = new BufferedReader(isr);
String result = "";
while(true) {
String data = reader.readLine();
if(data == null) break;
result += data + "\n";
}
System.out.println(result);
}
인코딩의 차이가 있다.
public class 데이터수집3 {
public static void main(String[] args) throws IOException {
Connection con = Jsoup.connect("http://ggoreb.com/hrd");
Document doc = con.get();
Elements items = doc.select("a"); //a태그를 다 가져온다.
for (int i = 0; i < items.size(); i++) {
Element item = items.get(i);
String text = item.text();
String href = item.attr("href");
System.out.println(text);
System.out.println(href);
}
}
}
public class 데이터수집3 {
public static void main(String[] args) throws IOException {
Connection con = Jsoup.connect("https://pyony.com/posts");
Document doc = con.get();
Elements items = doc.select("small"); //a태그를 다 가져온다.
for (int i = 0; i < items.size(); i++) {
Element item = items.get(i);
String text = item.text();
String href = item.attr("href");
System.out.println(text);
// System.out.println(href);
}
public class 데이터수집2 {
public static void main(String[] args) {
URL url;
try {
url = new URL("http://ggoreb.com/quiz/harry_potter.txt");
URLConnection con = url.openConnection();
InputStream in = con.getInputStream();
InputStreamReader isr = new InputStreamReader(in, "euc-kr");
BufferedReader reader = new BufferedReader(isr);
String result = "";
while(true) {
String data = reader.readLine();
if(data == null) break;
result += data + "\n";
}
System.out.println(result);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
InputStream in=null;
try {
in = new FileInputStream("c:/users/user/eclipse-workspace/JavaStudy/test.db");
int data = in.read();
System.out.println(data);
System.out.println(4/0);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}catch(ArithmeticException e){
e.printStackTrace();
}
finally {
try {
in.close();
//오류가 났을경우 캐치로 이동하기 때문에 파이널리에서 닫아준다.
} catch (IOException e) {
e.printStackTrace();
}
}
//프로그램을 종료시키지 않으려고 사용합니다
Scanner s = new Scanner(System.in);
s.next();
}
}
test db
close를 해주지 않으면 계속 사용중 test.db파일을 수정할 수 없음
finally로 닫아줌
'스프링' 카테고리의 다른 글
초보자도 스프링에서 DB 데이터 접근하는 방법이 있다? (google, naver 개발자도 반한 JPA에 대한 서술 중에서.. (!10만 view 돌파)) (0) | 2023.09.22 |
---|---|
자바4일 sql 1일 (1) | 2023.09.21 |
스프링2일 (0) | 2023.09.19 |
스프링부트1일차 (0) | 2023.09.18 |
0915스프링 (0) | 2023.09.15 |