tobby48 5 years ago
parent
commit
18a8541a7f

+ 84
- 2
src/kr/co/swh/lecture/database/java/FunctionTest.java View File

@@ -5,6 +5,7 @@ import java.sql.DriverManager;
5 5
 import java.sql.ResultSet;
6 6
 import java.sql.SQLException;
7 7
 import java.sql.Statement;
8
+import java.util.List;
8 9
 
9 10
 /**
10 11
  * <pre>
@@ -27,6 +28,33 @@ public class FunctionTest {
27 28
 	static final String PASSWORD = "xxxxxxx";
28 29
 
29 30
 	
31
+	public boolean dbInsert(List<Store> stores) {
32
+		boolean result = false;
33
+		Connection connection;
34
+		try {
35
+			Class.forName("com.mysql.jdbc.Driver");
36
+			connection = DriverManager.getConnection(DB_URL,USERNAME,PASSWORD);
37
+			connection.setAutoCommit(false);
38
+			System.out.println("MariaDB 연결.");
39
+			Statement statement = connection.createStatement();
40
+			for(Store object : stores) {
41
+				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);
42
+				result = statement.execute(sql);
43
+			}
44
+			connection.commit();
45
+
46
+		} catch (ClassNotFoundException e) {
47
+			// TODO Auto-generated catch block
48
+			e.printStackTrace();
49
+		} catch (SQLException e) {
50
+			// TODO Auto-generated catch block
51
+			e.printStackTrace();
52
+			connection.rollback();
53
+		}
54
+		return result;
55
+
56
+	}
57
+	
30 58
 	public boolean dbInsert(Store store) {
31 59
 		boolean result = false;
32 60
 		Connection connection;
@@ -44,6 +72,7 @@ public class FunctionTest {
44 72
 		} catch (SQLException e) {
45 73
 			// TODO Auto-generated catch block
46 74
 			e.printStackTrace();
75
+			
47 76
 		}
48 77
 		return result;
49 78
 
@@ -92,13 +121,66 @@ public class FunctionTest {
92 121
 
93 122
 	}
94 123
 	
124
+	public int dbUpdate(Store store) {
125
+		int result = 0;
126
+		Connection connection;
127
+		try {
128
+			Class.forName("com.mysql.jdbc.Driver");
129
+			connection = DriverManager.getConnection(DB_URL,USERNAME,PASSWORD);
130
+			System.out.println("MariaDB 연결.");
131
+			Statement statement = connection.createStatement();
132
+			result = statement.executeUpdate("update a set c1 = v1;");		//	result = 업데이터된 행의 숫자
133
+
134
+		} catch (ClassNotFoundException e) {
135
+			// TODO Auto-generated catch block
136
+			e.printStackTrace();
137
+		} catch (SQLException e) {
138
+			// TODO Auto-generated catch block
139
+			e.printStackTrace();
140
+		}
141
+		return result;
142
+
143
+	}
144
+	
145
+	public static void name() {
146
+		
147
+	}
148
+	
149
+	
150
+	
151
+	
152
+	
95 153
 	public static void main(String[] args) {
154
+		Store s1 = new Store();
155
+		
156
+		StoreDB s = new StoreDB();
157
+		s.dbInsert(s1);
158
+		
159
+		StoreDB s2 = new StoreDB();
160
+		s2.dbInsert(s1);
161
+		
162
+
163
+		
164
+		name();
165
+		
166
+		
167
+		
168
+		
96 169
 		
97 170
 		FunctionTest test = new FunctionTest();
98
-//		test.dbInsert("sdf", "sdf", "sdf", "sdf", "sdf", "sdf");
99
-		test.dbInsert(store)
100 171
 		
172
+		test.name();
173
+		
174
+		
175
+		
176
+		Store s = new Store();
177
+		s.setAddr("123");
101 178
 		
179
+		boolean r = test.dbInsert(s);
180
+		
181
+		
182
+		
183
+//		test.dbInsert("sdf", "sdf", "sdf", "sdf", "sdf", "sdf");
102 184
 		Connection connection = null;
103 185
 		Statement statement = null;
104 186
 		try{

+ 186
- 0
src/kr/co/swh/lecture/database/java/StoreDB.java View File

@@ -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
+}