본문 바로가기

옛날

[지방] 공개과제 문제1 완벽소스

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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package initialize;
 
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.StringTokenizer;
 
public class Initialize {
    static Connection conn = null;
    static Statement stmt = null;
    
    String driver = "com.mysql.jdbc.Driver";
    static String db = "jdbc:mysql://127.0.0.1/";
    String option = "?useSSL=false&characterEncoding=utf8&user=root&password=5071";
    
    static String sql = "";
    
    public Initialize() {
        try {
            Class.forName(driver);
            conn = DriverManager.getConnection(db+option); 
        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        }
        
    }
    
    public static void existDB () {
        if(sql!="") {sql="";}
        try {
            System.out.println("DB 중복 검사 중...");
            stmt = conn.createStatement();
            sql = "DROP DATABASE IF EXISTS company_5071;";
            stmt.executeUpdate(sql);
            System.out.println("company_5071 DB 생성 중...");
            sql = "CREATE DATABASE `company_5071`;";
            stmt.executeUpdate(sql);
            db = "jdbc:mysql://127.0.0.1/company_5071";
            System.out.println("DB 생성 완료!");
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    
    public static void createTable () {
        if(sql!="") {sql="";}
        try {
            System.out.println("테이블 생성 중...");
            stmt = conn.createStatement();
            sql = "CREATE TABLE `admin` ("
                    + "`name` varchar(20) NOT NULL,"
                    + "`passwd` varchar(20) NOT NULL,"
                    + "`position` varchar(20),"
                    + "`jumin` char(14),"
                    + "`inputDate` date,"
                    + "PRIMARY KEY(`name`,`passwd`));";
            stmt.executeUpdate(sql);
            sql = "CREATE TABLE `customer` ("
                    + "`code` char(7) NOT NULL,"
                    + "`name` varchar(20) NOT NULL,"
                    + "`birth` date,"
                    + "`tel` varchar(20),"
                    + "`address` varchar(100),"
                    + "`company` varchar(20),"
                    + "PRIMARY KEY(`code`,`name`));";
            stmt.executeUpdate(sql);
            sql = "CREATE TABLE `contract` ("
                    + "`customerCode` char(7) NOT NULL,"
                    + "`contractName` varchar(20) NOT NULL,"
                    + "`regPrice` int,"
                    + "`regDate` date NOT NULL,"
                    + "`monthPrice` int,"
                    + "`adminName` varchar(20) NOT NULL);";
            stmt.executeUpdate(sql);
            System.out.println("테이블 생성 완료!");
        } catch(SQLException e) {
            e.printStackTrace();
        }
    }
    
    public static void insertData (String tableName, int columnCount) {
        if(sql!="") {sql="";}
        try {
            FileInputStream fi = new FileInputStream("C://Users/김벼ㅇ머/Desktop/"+tableName+".txt");
            InputStreamReader is = new InputStreamReader(fi, "UTF-8");
            BufferedReader br = new BufferedReader(is);
            StringTokenizer st;
            
            String readRow = "";
            int columnHead = 1;
            
            stmt = conn.createStatement();
            
            while ((readRow=br.readLine())!=null) {
                st = new StringTokenizer(readRow,"    ");
                int i = 0;
                String[] arr = new String[columnCount];
                
                while(st.hasMoreTokens()) {
                    arr[i++= st.nextToken();
                }
                
                if(columnHead!=1) {
                    if(tableName == "admin") {
                        sql = "INSERT INTO company_5071.admin VALUES ("
                                + "'"+arr[0]+"','"+arr[1]+"','"+arr[2]+"','"+arr[3]+"','"+arr[4]+"');";
                    } else if(tableName == "customer") {
                        sql = "INSERT INTO company_5071.customer VALUES ("
                                + "'"+arr[0]+"','"+arr[1]+"','"+arr[2]+"','"+arr[3]+"','"+arr[4]+"','"+arr[5]+"');";
                    } else if(tableName == "contract") {
                        sql = "INSERT INTO company_5071.contract VALUES ("
                                + "'"+arr[0]+"','"+arr[1]+"','"+arr[2]+"','"+arr[3]+"','"+arr[4]+"','"+arr[5]+"');";
                    }
                    stmt.executeUpdate(sql);
                }
                columnHead++;
            }
            
            
            if(br!=null) br.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    
    public static void createUser () {
        if(sql!="") {sql="";}
        try { 
            System.out.println("아이디 생성 중..");
            stmt = conn.createStatement();
            sql = "DROP USER IF EXISTS user@127.0.0.1";
            stmt.executeUpdate(sql);
            sql = "CREATE USER user@127.0.0.1 IDENTIFIED BY '1234';";
            stmt.executeUpdate(sql);
            sql = "GRANT SELECT,INSERT,DELECT,UPDATE ON company_5071.* TO user@127.0.0.1";
            System.out.println("아이디 생성 완료!..");
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    
    public static void Close() {
        System.out.println("객체 종료 중...");
        try {if(conn!=null) conn.close();}catch (SQLException e) {e.printStackTrace();}
        try {if(stmt!=null) stmt.close();}catch (SQLException e) {e.printStackTrace();}
        System.out.println("객체 종료 성공!");
    }
    
    public static void main(String[] args) {
        new Initialize();
        existDB();
        new Initialize();
        createTable();
        insertData("admin",5);
        insertData("customer",6);
        insertData("contract",6);
        createUser();
        Close();
    }
}
cs