-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMySQLJDBC.java
165 lines (122 loc) · 5.53 KB
/
MySQLJDBC.java
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
package MySQL_JAVA;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.sql.*;
import java.util.Scanner;
public class MySQLJDBC {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try{
Class.forName("com.mysql.jdbc.Driver");
System.out.println("Connecting to database...");
conn = DriverManager.getConnection("jdbc:mysql://localhost/mydb?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC",
"root", "3502");
/*명령어 인터페이스(CLI, Command Line Interface)*/
System.out.println("========================================");
System.out.println("(0) 종료");
System.out.println("(1) 릴레이션 생성 및 데이터 추가");
System.out.println("(2) 제목을 이용한 검색");
System.out.println("(3) 관객수를 이용한 검색");
System.out.println("(4) 개봉일을 이용한 검색");
System.out.println("========================================");
System.out.println("원하는 번호를 입력 하시오");
int num = sc.nextInt();
sc.nextLine();
stmt = conn.createStatement();
if(num == 0) System.out.println("프로그램종료");
if(num == 1) { /*실습 2 - 2 릴레이션 생성 및 데이터 추가*/
StringBuilder sb = new StringBuilder();
String sql = sb.append("create table movie (")
.append("id char(3),")
.append("title varchar (100),")
.append("company varchar (50),")
.append("releasedate date,")
.append("country varchar (10),")
.append("totalscreen int,")
.append("profit numeric (15,2),")
.append("totalnum int,")
.append("grade varchar (50),")
.append("primary key (id));").toString();
stmt.execute(sql);
File file = new File("movie_data.txt");
FileReader filereader = new FileReader(file);
BufferedReader bufReader = new BufferedReader(filereader);
String line;
String prefix = "INSERT INTO movie VALUES( '";
while((line = bufReader.readLine()) != null){
//System.out.println(line);
String str2 = "";
int comMacnt = 0;
for(int i = 1; i < line.length(); i++) {
if(line.charAt(i) == '|') {
comMacnt++;
if(comMacnt < 5) {
str2 = str2 + "' , '";
}
else if(comMacnt == 5) {
str2 = str2 + "' , ";
}
else if(comMacnt > 5 && comMacnt < 8) {
str2 = str2 + " , ";
}
else if(comMacnt == 8) {
str2 = str2 + " , '";
}
}
else str2 = str2 + line.charAt(i);
}
str2 =prefix + str2 + "' );";
stmt.execute(str2);
}
rs = stmt.executeQuery("SELECT * FROM movie");
while(rs.next()) {
System.out.println(rs.getString(1) + "|" + rs.getString(2) + "|" + rs.getString(3) +
"|" + rs.getDate(4) + "|" + rs.getString(5) + "|" + rs.getInt(6) +
"|" + rs.getDouble(7) + "|" + rs.getInt(8) + "|" + rs.getString(9));
}
}
if(num == 2) {/*실습 2 - 3 제목을 이용한 검색*/
String input = sc.nextLine();
String query = "SELECT * FROM movie WHERE title LIKE '%" + input + "%';";
rs = stmt.executeQuery(query);
while(rs.next()) {// 출력
System.out.println(rs.getString(1) + "|" + rs.getString(2) + "|" + rs.getString(3) +
"|" + rs.getDate(4) + "|" + rs.getString(5) + "|" + rs.getInt(6) +
"|" + rs.getDouble(7) + "|" + rs.getInt(8) + "|" + rs.getString(9));
}
}
if(num == 3) {/*실습 2 - 4 관객 수를 이용한 검색*/
int customer = sc.nextInt();
String query = "SELECT * FROM movie WHERE totalnum > " + customer + ";";
rs = stmt.executeQuery(query);
while(rs.next()) {// 출력
System.out.println(rs.getString(1) + "|" + rs.getString(2) + "|" + rs.getString(3) +
"|" + rs.getDate(4) + "|" + rs.getString(5) + "|" + rs.getInt(6) +
"|" + rs.getDouble(7) + "|" + rs.getInt(8) + "|" + rs.getString(9));
}
}
if(num == 4) {/*실습 2 - 5 개봉 일를 이용한 검색*/
String pre_date = sc.nextLine();
String post_date = sc.nextLine();
String query = "SELECT * FROM movie WHERE releasedate BETWEEN '" + pre_date + "' AND '" + post_date + "';";
rs = stmt.executeQuery(query);
while(rs.next()) {
System.out.println(rs.getString(1) + "|" + rs.getString(2) + "|" + rs.getString(3) +
"|" + rs.getDate(4) + "|" + rs.getString(5) + "|" + rs.getInt(6) +
"|" + rs.getDouble(7) + "|" + rs.getInt(8) + "|" + rs.getString(9));
}
}
stmt.close();
conn.close();
sc.close();
}catch (SQLException ex) {
ex.printStackTrace();
} catch (Exception e){
e.printStackTrace();
}
}
}