|
@@ -0,0 +1,186 @@
|
|
1
|
+package kr.co.swh.lecture.database.java;
|
|
2
|
+
|
|
3
|
+import java.sql.Connection;
|
|
4
|
+import java.sql.DriverManager;
|
|
5
|
+import java.sql.ResultSet;
|
|
6
|
+import java.sql.SQLException;
|
|
7
|
+import java.sql.Statement;
|
|
8
|
+import java.util.List;
|
|
9
|
+
|
|
10
|
+/**
|
|
11
|
+ * <pre>
|
|
12
|
+ * kr.co.swh.lecture.database.java
|
|
13
|
+ * MariaDBSelect.java
|
|
14
|
+ *
|
|
15
|
+ * 설명 :데이터베이스 검색 예제
|
|
16
|
+ * </pre>
|
|
17
|
+ *
|
|
18
|
+ * @since : 2017. 10. 26.
|
|
19
|
+ * @author : tobby48
|
|
20
|
+ * @version : v1.0
|
|
21
|
+ */
|
|
22
|
+public class StoreDB {
|
|
23
|
+
|
|
24
|
+ final static String JDBC_DRIVER = "com.mysql.jdbc.Driver";
|
|
25
|
+ static final String DB_URL = "jdbc:mysql://127.0.0.1:3306/market";
|
|
26
|
+
|
|
27
|
+ static final String USERNAME = "root";
|
|
28
|
+ static final String PASSWORD = "xxxxxxx";
|
|
29
|
+
|
|
30
|
+ public Connection init() {
|
|
31
|
+ Connection connection = null;
|
|
32
|
+ try {
|
|
33
|
+ Class.forName("com.mysql.jdbc.Driver");
|
|
34
|
+ connection = DriverManager.getConnection(DB_URL,USERNAME,PASSWORD);
|
|
35
|
+ System.out.println("MariaDB 연결.");
|
|
36
|
+
|
|
37
|
+ } catch (ClassNotFoundException e) {
|
|
38
|
+ // TODO Auto-generated catch block
|
|
39
|
+ e.printStackTrace();
|
|
40
|
+ } catch (SQLException e) {
|
|
41
|
+ // TODO Auto-generated catch block
|
|
42
|
+ e.printStackTrace();
|
|
43
|
+ }
|
|
44
|
+ return connection;
|
|
45
|
+ }
|
|
46
|
+
|
|
47
|
+ public boolean dbInsert(List<Store> stores) {
|
|
48
|
+ boolean result = false;
|
|
49
|
+ Statement statement = null;
|
|
50
|
+ Connection connection = init();
|
|
51
|
+ try {
|
|
52
|
+ statement = connection.createStatement();
|
|
53
|
+ for(Store object : stores) {
|
|
54
|
+ String sql = String.format("insert into store(addr, code, lat, lng, name, type) values('%s','%s','%s','%s','%s','%s')", store.getAddr(), store.getCode(), lat, lng, name, type);
|
|
55
|
+ result = statement.execute(sql);
|
|
56
|
+ }
|
|
57
|
+ connection.commit();
|
|
58
|
+
|
|
59
|
+ } catch (ClassNotFoundException e) {
|
|
60
|
+ // TODO Auto-generated catch block
|
|
61
|
+ e.printStackTrace();
|
|
62
|
+ } catch (SQLException e) {
|
|
63
|
+ // TODO Auto-generated catch block
|
|
64
|
+ e.printStackTrace();
|
|
65
|
+ connection.rollback();
|
|
66
|
+ } finally {
|
|
67
|
+ statement.close();
|
|
68
|
+ }
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+ Connection connection;
|
|
75
|
+ try {
|
|
76
|
+ Class.forName("com.mysql.jdbc.Driver");
|
|
77
|
+ connection = DriverManager.getConnection(DB_URL,USERNAME,PASSWORD);
|
|
78
|
+ connection.setAutoCommit(false);
|
|
79
|
+ System.out.println("MariaDB 연결.");
|
|
80
|
+ Statement statement = connection.createStatement();
|
|
81
|
+ for(Store object : stores) {
|
|
82
|
+ String sql = String.format("insert into store(addr, code, lat, lng, name, type) values('%s','%s','%s','%s','%s','%s')", store.getAddr(), store.getCode(), lat, lng, name, type);
|
|
83
|
+ result = statement.execute(sql);
|
|
84
|
+ }
|
|
85
|
+ connection.commit();
|
|
86
|
+
|
|
87
|
+ } catch (ClassNotFoundException e) {
|
|
88
|
+ // TODO Auto-generated catch block
|
|
89
|
+ e.printStackTrace();
|
|
90
|
+ } catch (SQLException e) {
|
|
91
|
+ // TODO Auto-generated catch block
|
|
92
|
+ e.printStackTrace();
|
|
93
|
+ connection.rollback();
|
|
94
|
+ }
|
|
95
|
+ return result;
|
|
96
|
+
|
|
97
|
+ }
|
|
98
|
+
|
|
99
|
+ public boolean dbInsert(Store store) {
|
|
100
|
+ boolean result = false;
|
|
101
|
+ Connection connection;
|
|
102
|
+ try {
|
|
103
|
+ Class.forName("com.mysql.jdbc.Driver");
|
|
104
|
+ connection = DriverManager.getConnection(DB_URL,USERNAME,PASSWORD);
|
|
105
|
+ System.out.println("MariaDB 연결.");
|
|
106
|
+ Statement statement = connection.createStatement();
|
|
107
|
+ String sql = String.format("insert into store(addr, code, lat, lng, name, type) values('%s','%s','%s','%s','%s','%s')", store.getAddr(), store.getCode(), lat, lng, name, type);
|
|
108
|
+ result = statement.execute(sql);
|
|
109
|
+
|
|
110
|
+ } catch (ClassNotFoundException e) {
|
|
111
|
+ // TODO Auto-generated catch block
|
|
112
|
+ e.printStackTrace();
|
|
113
|
+ } catch (SQLException e) {
|
|
114
|
+ // TODO Auto-generated catch block
|
|
115
|
+ e.printStackTrace();
|
|
116
|
+
|
|
117
|
+ }
|
|
118
|
+ return result;
|
|
119
|
+
|
|
120
|
+ }
|
|
121
|
+
|
|
122
|
+ public boolean dbInsert(String addr, String code, String lat, String lng, String name, String type) {
|
|
123
|
+ boolean result = false;
|
|
124
|
+ Connection connection;
|
|
125
|
+ try {
|
|
126
|
+ Class.forName("com.mysql.jdbc.Driver");
|
|
127
|
+ connection = DriverManager.getConnection(DB_URL,USERNAME,PASSWORD);
|
|
128
|
+ System.out.println("MariaDB 연결.");
|
|
129
|
+ Statement statement = connection.createStatement();
|
|
130
|
+ String sql = String.format("insert into store(addr, code, lat, lng, name, type) values('%s','%s','%s','%s','%s','%s')", addr, code, lat, lng, name, type);
|
|
131
|
+ result = statement.execute(sql);
|
|
132
|
+
|
|
133
|
+ } catch (ClassNotFoundException e) {
|
|
134
|
+ // TODO Auto-generated catch block
|
|
135
|
+ e.printStackTrace();
|
|
136
|
+ } catch (SQLException e) {
|
|
137
|
+ // TODO Auto-generated catch block
|
|
138
|
+ e.printStackTrace();
|
|
139
|
+ }
|
|
140
|
+ return result;
|
|
141
|
+
|
|
142
|
+ }
|
|
143
|
+
|
|
144
|
+ public int dbUpdate() {
|
|
145
|
+ int result = 0;
|
|
146
|
+ Connection connection;
|
|
147
|
+ try {
|
|
148
|
+ Class.forName("com.mysql.jdbc.Driver");
|
|
149
|
+ connection = DriverManager.getConnection(DB_URL,USERNAME,PASSWORD);
|
|
150
|
+ System.out.println("MariaDB 연결.");
|
|
151
|
+ Statement statement = connection.createStatement();
|
|
152
|
+ result = statement.executeUpdate("update a set c1 = v1;"); // result = 업데이터된 행의 숫자
|
|
153
|
+
|
|
154
|
+ } catch (ClassNotFoundException e) {
|
|
155
|
+ // TODO Auto-generated catch block
|
|
156
|
+ e.printStackTrace();
|
|
157
|
+ } catch (SQLException e) {
|
|
158
|
+ // TODO Auto-generated catch block
|
|
159
|
+ e.printStackTrace();
|
|
160
|
+ }
|
|
161
|
+ return result;
|
|
162
|
+
|
|
163
|
+ }
|
|
164
|
+
|
|
165
|
+ public int dbUpdate(Store store) {
|
|
166
|
+ int result = 0;
|
|
167
|
+ Connection connection;
|
|
168
|
+ try {
|
|
169
|
+ Class.forName("com.mysql.jdbc.Driver");
|
|
170
|
+ connection = DriverManager.getConnection(DB_URL,USERNAME,PASSWORD);
|
|
171
|
+ System.out.println("MariaDB 연결.");
|
|
172
|
+ Statement statement = connection.createStatement();
|
|
173
|
+ result = statement.executeUpdate("update a set c1 = v1;"); // result = 업데이터된 행의 숫자
|
|
174
|
+
|
|
175
|
+ } catch (ClassNotFoundException e) {
|
|
176
|
+ // TODO Auto-generated catch block
|
|
177
|
+ e.printStackTrace();
|
|
178
|
+ } catch (SQLException e) {
|
|
179
|
+ // TODO Auto-generated catch block
|
|
180
|
+ e.printStackTrace();
|
|
181
|
+ }
|
|
182
|
+ return result;
|
|
183
|
+
|
|
184
|
+ }
|
|
185
|
+
|
|
186
|
+}
|