tobby48 5 years ago
parent
commit
90e866715e

src/kr/co/swh/lecture/database/java/FunctionTest.java → src/kr/co/swh/lecture/database/java/DatabaseUtil.java View File

@@ -19,52 +19,51 @@ import java.util.List;
19 19
  * @author : tobby48
20 20
  * @version : v1.0
21 21
  */
22
-public class FunctionTest {
22
+public class DatabaseUtil {
23
+	String JDBC_DRIVER = "com.mysql.jdbc.Driver";  
24
+	String DB_URL;
23 25
 
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";
26
+	String USERNAME;
27
+	String PASSWORD;
29 28
 
29
+	Connection connection = null;
30 30
 	
31
-	public boolean dbInsert(List<Store> stores) {
32
-		boolean result = false;
33
-		Connection connection;
31
+	public DatabaseUtil(String dbType, String dbUrl, String dbName, String user, String password2) {
32
+		// TODO Auto-generated constructor stub
33
+
34
+		
35
+		DB_URL = "jdbc:mysql://" + dbUrl + ":3306/" + dbName;
36
+		USERNAME = user;
37
+		PASSWORD = password2;
38
+		
34 39
 		try {
35 40
 			Class.forName("com.mysql.jdbc.Driver");
36 41
 			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) {
42
+		} catch (ClassNotFoundException | SQLException e) {
47 43
 			// TODO Auto-generated catch block
48 44
 			e.printStackTrace();
45
+		}
46
+	}
47
+	
48
+	public void close() {
49
+		try {
50
+			connection.close();
49 51
 		} catch (SQLException e) {
50 52
 			// TODO Auto-generated catch block
51 53
 			e.printStackTrace();
52
-			connection.rollback();
53 54
 		}
54
-		return result;
55
-
56 55
 	}
57
-	
58
-	public boolean dbInsert(Store store) {
56
+
57
+	public boolean dbInsert(List<Store> stores) {
58
+		
59 59
 		boolean result = false;
60
-		Connection connection;
60
+		Statement statement = null;
61 61
 		try {
62
-			Class.forName("com.mysql.jdbc.Driver");
63
-			connection = DriverManager.getConnection(DB_URL,USERNAME,PASSWORD);
64
-			System.out.println("MariaDB 연결.");
65
-			Statement statement = connection.createStatement();
66
-			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);
67
-			result = statement.execute(sql);
62
+			statement = connection.createStatement();
63
+			for(Store object : stores) {
64
+				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);
65
+				result = statement.execute(sql);
66
+			}
68 67
 
69 68
 		} catch (ClassNotFoundException e) {
70 69
 			// TODO Auto-generated catch block
@@ -72,21 +71,18 @@ public class FunctionTest {
72 71
 		} catch (SQLException e) {
73 72
 			// TODO Auto-generated catch block
74 73
 			e.printStackTrace();
74
+		} finally {
75
+			statement.close();
75 76
 			
76 77
 		}
77 78
 		return result;
78
-
79 79
 	}
80
-	
81
-	public boolean dbInsert(String addr, String code, String lat, String lng, String name, String type) {
80
+
81
+	public boolean dbInsert(Store store) {
82 82
 		boolean result = false;
83
-		Connection connection;
84 83
 		try {
85
-			Class.forName("com.mysql.jdbc.Driver");
86
-			connection = DriverManager.getConnection(DB_URL,USERNAME,PASSWORD);
87
-			System.out.println("MariaDB 연결.");
88 84
 			Statement statement = connection.createStatement();
89
-			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);
85
+			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);
90 86
 			result = statement.execute(sql);
91 87
 
92 88
 		} catch (ClassNotFoundException e) {
@@ -95,99 +91,56 @@ public class FunctionTest {
95 91
 		} catch (SQLException e) {
96 92
 			// TODO Auto-generated catch block
97 93
 			e.printStackTrace();
94
+
98 95
 		}
99 96
 		return result;
100 97
 
101 98
 	}
102
-	
103
-	public int dbUpdate() {
104
-		int result = 0;
105
-		Connection connection;
106
-		try {
107
-			Class.forName("com.mysql.jdbc.Driver");
108
-			connection = DriverManager.getConnection(DB_URL,USERNAME,PASSWORD);
109
-			System.out.println("MariaDB 연결.");
110
-			Statement statement = connection.createStatement();
111
-			result = statement.executeUpdate("update a set c1 = v1;");		//	result = 업데이터된 행의 숫자
112 99
 
113
-		} catch (ClassNotFoundException e) {
114
-			// TODO Auto-generated catch block
115
-			e.printStackTrace();
116
-		} catch (SQLException e) {
117
-			// TODO Auto-generated catch block
118
-			e.printStackTrace();
119
-		}
120
-		return result;
121 100
 
122
-	}
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 101
 
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 102
 
143
-	}
144
-	
145
-	public static void name() {
146
-		
147
-	}
148
-	
149
-	
150
-	
151
-	
152
-	
153 103
 	public static void main(String[] args) {
154 104
 		Store s1 = new Store();
155
-		
105
+
156 106
 		StoreDB s = new StoreDB();
157 107
 		s.dbInsert(s1);
158
-		
108
+
159 109
 		StoreDB s2 = new StoreDB();
160 110
 		s2.dbInsert(s1);
161
-		
162 111
 
163
-		
164
-		name();
165
-		
166
-		
167
-		
168
-		
169
-		
112
+		FunctionTest tt = new FunctionTest();
113
+		tt.play();
114
+
115
+
116
+
117
+
118
+
119
+
120
+
121
+
122
+
170 123
 		FunctionTest test = new FunctionTest();
171
-		
124
+
172 125
 		test.name();
173
-		
174
-		
175
-		
126
+
127
+
128
+
176 129
 		Store s = new Store();
177 130
 		s.setAddr("123");
178
-		
131
+
179 132
 		boolean r = test.dbInsert(s);
180
-		
181
-		
182
-		
183
-//		test.dbInsert("sdf", "sdf", "sdf", "sdf", "sdf", "sdf");
133
+
134
+
135
+
136
+		//		test.dbInsert("sdf", "sdf", "sdf", "sdf", "sdf", "sdf");
184 137
 		Connection connection = null;
185 138
 		Statement statement = null;
186 139
 		try{
187 140
 			Class.forName("com.mysql.jdbc.Driver");
188 141
 			connection = DriverManager.getConnection(DB_URL,USERNAME,PASSWORD);
189 142
 			System.out.println("MariaDB 연결.");
190
-			
143
+
191 144
 			statement = connection.createStatement();
192 145
 			while() {
193 146
 				statement.execute(sql)
@@ -195,21 +148,21 @@ public class FunctionTest {
195 148
 			statement.close();
196 149
 			statement = connection.createStatement();
197 150
 
198
-			
151
+
199 152
 			//	insert
200 153
 			boolean result = statement.execute("insert into employees(name,c2) values('가준', v2);");
201
-//			boolean result = statement.execute(String.format("insert into a(c1,c2) values(%d, %s);", v1, v2));
154
+			//			boolean result = statement.execute(String.format("insert into a(c1,c2) values(%d, %s);", v1, v2));
202 155
 			if(result) System.out.println("ResultSet 값 있다.");
203 156
 			else System.out.println("업데이트 된 행이 있거나 리턴되는 값이 없는 경우");
204
-					
157
+
205 158
 			//	update
206
-//			int result = statement.executeUpdate("update a set c1 = v1;");		//	result = 업데이터된 행의 숫자
207
-//			if(result > 0) System.out.println("정상처리");
208
-//			else System.out.println("비정상처리");
209
-				
159
+			//			int result = statement.executeUpdate("update a set c1 = v1;");		//	result = 업데이터된 행의 숫자
160
+			//			if(result > 0) System.out.println("정상처리");
161
+			//			else System.out.println("비정상처리");
162
+
210 163
 			//	select
211 164
 			ResultSet rs = statement.executeQuery("SELECT * FROM employees;");
212
-			
165
+
213 166
 			while(rs.next()){
214 167
 				int employee_id = rs.getInt("employee_id");
215 168
 				String name = rs.getString("name");

+ 17
- 0
src/kr/co/swh/lecture/database/java/School.java View File

@@ -0,0 +1,17 @@
1
+package kr.co.swh.lecture.database.java;
2
+
3
+public class School {
4
+
5
+	public static void main(String[] args) {
6
+		// TODO Auto-generated method stub
7
+		
8
+		String dbUrl = "127.0.0.1";
9
+		String dbName = "heejae";
10
+		String user = "root";
11
+		String password = "swhacademy!";
12
+		
13
+		DatabaseUtil u = new DatabaseUtil(dbUrl, dbName, user, password);
14
+//		u.dbInsert(stores)
15
+	}
16
+
17
+}

+ 30
- 0
src/kr/co/swh/lecture/database/java/Wheather.java View File

@@ -0,0 +1,30 @@
1
+package kr.co.swh.lecture.database.java;
2
+
3
+public class Wheather {
4
+
5
+	public static void main(String[] args) {
6
+		// TODO Auto-generated method stub
7
+		
8
+		//	수집코드
9
+		
10
+		//	데이터베이스 저장
11
+		String dbUrl = "127.0.0.1";
12
+		String dbName = "market";
13
+		String user = "root";
14
+		String password = "swhacademy!";
15
+
16
+		
17
+		
18
+		DatabaseUtil u = new DatabaseUtil(dbUrl, dbName, user, password);
19
+		u.DB_URL
20
+		u.dbInsert(stores)
21
+		u.close();
22
+		
23
+		
24
+		
25
+		DatabaseUtil u1 = new DatabaseUtil();
26
+		u1.dbInsert(store);
27
+//		u.dbInsert(stores)
28
+	}
29
+
30
+}