|
@@ -0,0 +1,157 @@
|
|
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
|
+
|
|
9
|
+/**
|
|
10
|
+ * <pre>
|
|
11
|
+ * kr.co.swh.lecture.database.java
|
|
12
|
+ * MariaDBSelect.java
|
|
13
|
+ *
|
|
14
|
+ * 설명 :데이터베이스 검색 예제
|
|
15
|
+ * </pre>
|
|
16
|
+ *
|
|
17
|
+ * @since : 2017. 10. 26.
|
|
18
|
+ * @author : tobby48
|
|
19
|
+ * @version : v1.0
|
|
20
|
+ */
|
|
21
|
+public class FunctionTest {
|
|
22
|
+
|
|
23
|
+ final static String JDBC_DRIVER = "com.mysql.jdbc.Driver";
|
|
24
|
+ static final String DB_URL = "jdbc:mysql://127.0.0.1:3306/market";
|
|
25
|
+
|
|
26
|
+ static final String USERNAME = "root";
|
|
27
|
+ static final String PASSWORD = "xxxxxxx";
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+ public boolean dbInsert(Store store) {
|
|
31
|
+ boolean result = false;
|
|
32
|
+ Connection connection;
|
|
33
|
+ try {
|
|
34
|
+ Class.forName("com.mysql.jdbc.Driver");
|
|
35
|
+ connection = DriverManager.getConnection(DB_URL,USERNAME,PASSWORD);
|
|
36
|
+ System.out.println("MariaDB 연결.");
|
|
37
|
+ Statement statement = connection.createStatement();
|
|
38
|
+ 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);
|
|
39
|
+ result = statement.execute(sql);
|
|
40
|
+
|
|
41
|
+ } catch (ClassNotFoundException e) {
|
|
42
|
+ // TODO Auto-generated catch block
|
|
43
|
+ e.printStackTrace();
|
|
44
|
+ } catch (SQLException e) {
|
|
45
|
+ // TODO Auto-generated catch block
|
|
46
|
+ e.printStackTrace();
|
|
47
|
+ }
|
|
48
|
+ return result;
|
|
49
|
+
|
|
50
|
+ }
|
|
51
|
+
|
|
52
|
+ public boolean dbInsert(String addr, String code, String lat, String lng, String name, String type) {
|
|
53
|
+ boolean result = false;
|
|
54
|
+ Connection connection;
|
|
55
|
+ try {
|
|
56
|
+ Class.forName("com.mysql.jdbc.Driver");
|
|
57
|
+ connection = DriverManager.getConnection(DB_URL,USERNAME,PASSWORD);
|
|
58
|
+ System.out.println("MariaDB 연결.");
|
|
59
|
+ Statement statement = connection.createStatement();
|
|
60
|
+ 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);
|
|
61
|
+ result = statement.execute(sql);
|
|
62
|
+
|
|
63
|
+ } catch (ClassNotFoundException e) {
|
|
64
|
+ // TODO Auto-generated catch block
|
|
65
|
+ e.printStackTrace();
|
|
66
|
+ } catch (SQLException e) {
|
|
67
|
+ // TODO Auto-generated catch block
|
|
68
|
+ e.printStackTrace();
|
|
69
|
+ }
|
|
70
|
+ return result;
|
|
71
|
+
|
|
72
|
+ }
|
|
73
|
+
|
|
74
|
+ public int dbUpdate() {
|
|
75
|
+ int result = 0;
|
|
76
|
+ Connection connection;
|
|
77
|
+ try {
|
|
78
|
+ Class.forName("com.mysql.jdbc.Driver");
|
|
79
|
+ connection = DriverManager.getConnection(DB_URL,USERNAME,PASSWORD);
|
|
80
|
+ System.out.println("MariaDB 연결.");
|
|
81
|
+ Statement statement = connection.createStatement();
|
|
82
|
+ result = statement.executeUpdate("update a set c1 = v1;"); // result = 업데이터된 행의 숫자
|
|
83
|
+
|
|
84
|
+ } catch (ClassNotFoundException e) {
|
|
85
|
+ // TODO Auto-generated catch block
|
|
86
|
+ e.printStackTrace();
|
|
87
|
+ } catch (SQLException e) {
|
|
88
|
+ // TODO Auto-generated catch block
|
|
89
|
+ e.printStackTrace();
|
|
90
|
+ }
|
|
91
|
+ return result;
|
|
92
|
+
|
|
93
|
+ }
|
|
94
|
+
|
|
95
|
+ public static void main(String[] args) {
|
|
96
|
+
|
|
97
|
+ FunctionTest test = new FunctionTest();
|
|
98
|
+// test.dbInsert("sdf", "sdf", "sdf", "sdf", "sdf", "sdf");
|
|
99
|
+ test.dbInsert(store)
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+ Connection connection = null;
|
|
103
|
+ Statement statement = null;
|
|
104
|
+ try{
|
|
105
|
+ Class.forName("com.mysql.jdbc.Driver");
|
|
106
|
+ connection = DriverManager.getConnection(DB_URL,USERNAME,PASSWORD);
|
|
107
|
+ System.out.println("MariaDB 연결.");
|
|
108
|
+
|
|
109
|
+ statement = connection.createStatement();
|
|
110
|
+ while() {
|
|
111
|
+ statement.execute(sql)
|
|
112
|
+ }
|
|
113
|
+ statement.close();
|
|
114
|
+ statement = connection.createStatement();
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+ // insert
|
|
118
|
+ boolean result = statement.execute("insert into employees(name,c2) values('가준', v2);");
|
|
119
|
+// boolean result = statement.execute(String.format("insert into a(c1,c2) values(%d, %s);", v1, v2));
|
|
120
|
+ if(result) System.out.println("ResultSet 값 있다.");
|
|
121
|
+ else System.out.println("업데이트 된 행이 있거나 리턴되는 값이 없는 경우");
|
|
122
|
+
|
|
123
|
+ // update
|
|
124
|
+// int result = statement.executeUpdate("update a set c1 = v1;"); // result = 업데이터된 행의 숫자
|
|
125
|
+// if(result > 0) System.out.println("정상처리");
|
|
126
|
+// else System.out.println("비정상처리");
|
|
127
|
+
|
|
128
|
+ // select
|
|
129
|
+ ResultSet rs = statement.executeQuery("SELECT * FROM employees;");
|
|
130
|
+
|
|
131
|
+ while(rs.next()){
|
|
132
|
+ int employee_id = rs.getInt("employee_id");
|
|
133
|
+ String name = rs.getString("name");
|
|
134
|
+ double hourly_pay = rs.getDouble("hourly_pay");
|
|
135
|
+ long employee_contact = rs.getLong("employee_contact");
|
|
136
|
+ System.out.println("employee_id : " + employee_id);
|
|
137
|
+ System.out.println("name: " + name);
|
|
138
|
+ System.out.println("hourly_pay: " + hourly_pay);
|
|
139
|
+ System.out.println("employee_contact: " + employee_contact);
|
|
140
|
+ System.out.println(rs.getInt(1)); // 첫번 째 열
|
|
141
|
+ }
|
|
142
|
+ rs.close();
|
|
143
|
+ }catch(SQLException se1){
|
|
144
|
+ se1.printStackTrace();
|
|
145
|
+ }catch(Exception ex){
|
|
146
|
+ ex.printStackTrace();
|
|
147
|
+ }finally{
|
|
148
|
+ try{
|
|
149
|
+ if(statement!=null) statement.close();
|
|
150
|
+ if(connection!=null) connection.close();
|
|
151
|
+ }catch(SQLException se2){
|
|
152
|
+ }
|
|
153
|
+ }
|
|
154
|
+ System.out.println("MariaDB 연결종료.");
|
|
155
|
+ }
|
|
156
|
+
|
|
157
|
+}
|