본문 바로가기
비트 장기/SQL

7/31 복습(java db추가 )

by woohyun22 2018. 8. 3.


밑에 넣어주는거 developer에 넣어주는건지 어떤식으로 돌아가는건지 확실히 알기


나중에 한번씩 더 써보기


// toString() 문자열 출력 함수

// length() 길이를 알려 주는 함수

// charAt()  0부터 시작해서 n번 째 글자를 받아준다.

// equals 두 개의 문자열을 비교해준다.

// indexOF 문자가 있는 위치를 찾아준다.

// replace 원하는 글자를 바꿔준다.

// substring 원하는 범위 만큼 문자를 출력해준다.

// toLowerCase, toUpperCase 소문자, 대문자 변환

// format 숫자와 문자열을 이어주는 함수%d,%s




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package prj99;
 
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
class Tiger{
 
    static void func01() {
        System.out.println("함수 1번 콜");
    }
    void func02(int a, int b) {
        System.out.println("함수 2번 콜");
    }
    int func03() {
        return 1000;
    }
    int func04(int a, int b) {
        return a+b;
    }
    static void func05() {
        System.out.println("함수 5번 콜"); 
    }
}
public class hello {
    public static void main(String[] args) {
        String s1 = "무궁화꽃이피었습니다";
        /*System.out.println(s1);
        System.out.println(s1.toString());// 이것이 정석이다. 
        System.out.println(s1.length());
        s1.charAt(3); 
        char c1 = s1.charAt(3);//0부터 시작 3까지 = 4번째
        System.out.println(c1);
        System.out.println(s1.charAt(3));
        String s2 = "무궁화꽃이피었습니다";
        System.out.println(s1.equals(s2));
        if(s1.equals(s2)) {
            System.out.println("패스워드가 정확합니다.");
        } else {
            System.out.println("패스워드가 틀립니다.");            
        }
        int n2 = s1.indexOf("꽃이");//첫글자가 0번쨰
        System.out.println(n2);*/
        /*String s3 = s1.replace("무궁화", "개나리");
        System.out.println(s3);
        System.out.println(s1); // s1이 변하는 것은 아니다.
        // 꼭 글자 수가 같지 않아도 된다.
        s1 = s1.replace("무궁화", "무화과나무");// 자기가 자기 것을 바꿀 수도 있다.
        System.out.println(s1);
        // substring 원하는 범위 만큼 문자를 출력해준다.
        // 시작 숫자에서 마지막 숫자-1까지 잘라준다.
        // 글자 수는 마지막 숫자 - 시작 숫자
        String s4 = s1.substring(3, 6);
        System.out.println(s4);
        // 4번 부터 끝까지 출력된다.
        String s5 = s1.substring(4);
        System.out.println(s5);
        // 소문자, 대문자로 변환해준다.
        String s6 = "Apple";
        System.out.println(s6);
        String s7 = s6.toLowerCase();
        System.out.println(s7);
        String s8 = s6.toUpperCase();
        System.out.println(s8);
         */
        System.out.println("-------------------");
        Tiger tiger = new Tiger();
        tiger.func01();
        tiger.func02(35);
        System.out.println(tiger.func03());
        System.out.println(tiger.func04(5,5));
 
        tiger.func05();
        // final 함수는 객체를 생성하지 않아도 된다. static
        Tiger.func05();
        Tiger.func01();
 
        // "%d호랑이" %d 뒤에는 반드시 숫자가 따라온다.
        // %d 자리에 30이 들어간다.
        // "%d호랑이%d",30,50 두 개를 넣을 수 있다.
        System.out.println("-------------------");
        String s9 = String.format("%d호랑이%d",30,50);
        System.out.println(s9);
 
        // %s는 문자열을 넣어준다.
        String s10 = String.format("%d호랑이%s",100,"거북이");
        System.out.println(s10);
 
        int num = 99;
        String s12 = "나비";
        String s11 = String.format("%d호랑이%s",num,s12);
        System.out.println(s11);
 
 
        //String sql="INSERT into banana values('개나리',25,1000,'하회마을')";
        String name = "강감찬";
        int age = 25;
        int salary = 1000;
        String home = "서울";
        String s13 = String.format("insert into banana values('%s',%d,%d,'%s')",name,age,salary,home);
        System.out.println(s13);
 
    }        
}

 
 
cs



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package prj99;
class MyThread extends Thread{
    @Override
    public void run() {
        System.out.println("처음 예제");
    }
}
interface TestInterface{
    void func01();
}
class KongThread implements Runnable{
    public void run() {
        System.out.println("4");            
    }
};
public class hello {
    public static void main(String[] args) {
        MyThread n1 = new MyThread();
        n1.start();
        Thread n2 = new Thread() {
            @Override
            public void run() {
                System.out.println(2);
            }
        };
        n2.start();
        new Thread() {
            public void run() {
                System.out.println("세번쨰");
            }
        }.start();
 
        KongThread kong = new KongThread();
        //
        Thread n3 = new Thread(kong);
        n3.start();
        //kong만 가는게 아니라 부모인 MYthread 먼저 실행
 
        MyThread n5 = new MyThread();
        n5.start();
        try {
            Thread.sleep(3000);
        } catch (Exception e) {
 
        }
        System.out.println("앵무새");
        //n5.start();  thread는 start를 한번이상 쓸수없다.
        //n5.stop();//함수가 사라졌다.쓰지마라 deprecation    
    }//thread 1,2,3은 쓰자
}
cs



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package prj99;
 
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.util.Random;
 
class Dao{
    void insert(String name, int age, int salary, String home) {
        try {
 
            //1단계 : class 확인
            //현재 찾고 싶은 클래스를 찾아준다.
            String driver ="oracle.jdbc.driver.OracleDriver"
            System.out.println(Class.forName(driver));
            System.out.println(1);
 
            //2단계 : 로그인
            Connection con=null;
            String dburl="jdbc:oracle:thin:@127.0.0.1:1521:orcl";//dburl은 어디꼬 사용?
            String username="c##jwh"
            String password="1"
 
            con=DriverManager.getConnection(dburl,username,password);            
 
            System.out.println(2);
 
            //3단계: 쿼리문 작성 및 컴파일
            PreparedStatement ps=null;
 
            /* String name = "강감찬";
            int age = 26;
            int salary = 300;
            String home = "서울"; */
            //String sql="INSERT into banana values('개나리',25,1000,'하회마을')";
            String sql = String.format("insert into banana values('%s',%d,%d,'%s')",name,age,salary,home);
 
            System.out.println(sql);
 
            ps = con.prepareStatement(sql); //기계어번역
 
            System.out.println(3);
 
            //4단계: 실행
            ps.executeUpdate(); //f5번 누른것과 같다.
            System.out.println(4);
 
            //5단계: 접속 종료
            ps.close(); 
            System.out.println(5);
 
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
    }
}
public class hello {
    public static void main(String[] args) {
        Dao dao = new Dao();
        dao.insert("세종대왕",50,1500,"조선시대");
        dao.insert("자라"12300"바다");
 
        String[] ar = {"서울","대구","대전","부산"};
 
        for (int i = 0; i < 15; i++) {
            int r1 = (new Random()).nextInt(100);
            int r2 = (new Random()).nextInt(1000);
            int r3 = (new Random()).nextInt(4);
            dao.insert("곰돌이"+i, r1, r2, ar[r3]);
        }
    }        
}
 

cs


728x90

'비트 장기 > SQL' 카테고리의 다른 글

developer 오류  (0) 2018.08.15
7/31 복습 보충하기  (0) 2018.08.03
7/30 복습()  (0) 2018.08.03
7/26 복습 (sql 기본구문)  (0) 2018.08.02
7/30 배움()  (0) 2018.07.30

댓글