tobby48 4 years ago
parent
commit
8eb30fe7fb

+ 0
- 228
src/kr/co/swh/lecture/database/java/DatabaseUtil.java View File

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

+ 0
- 41
src/kr/co/swh/lecture/database/java/Employee.java View File

@@ -1,41 +0,0 @@
1
-package kr.co.swh.lecture.database.java;
2
-
3
-public class Employee {
4
-	private int employee_id;
5
-	private String name;
6
-	private double hourly_pay;
7
-	private long employee_contact;
8
-	
9
-	public Employee(int employee_id, String name, double hourly_pay, long employee_contact) {
10
-		this.employee_id = employee_id;
11
-		this.name = name;
12
-		this.hourly_pay = hourly_pay;
13
-		this.employee_contact = employee_contact;
14
-	}
15
-	
16
-	public int getEmployee_id() {
17
-		return employee_id;
18
-	}
19
-	public void setEmployee_id(int employee_id) {
20
-		this.employee_id = employee_id;
21
-	}
22
-	public String getName() {
23
-		return name;
24
-	}
25
-	public void setName(String name) {
26
-		this.name = name;
27
-	}
28
-	public double getHourly_pay() {
29
-		return hourly_pay;
30
-	}
31
-	public void setHourly_pay(double hourly_pay) {
32
-		this.hourly_pay = hourly_pay;
33
-	}
34
-	public long getEmployee_contact() {
35
-		return employee_contact;
36
-	}
37
-	public void setEmployee_contact(long employee_contact) {
38
-		this.employee_contact = employee_contact;
39
-	}
40
-	
41
-}

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

@@ -1,17 +0,0 @@
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
-}

+ 0
- 48
src/kr/co/swh/lecture/database/java/Store.java View File

@@ -1,48 +0,0 @@
1
-package kr.co.swh.lecture.database.java;
2
-
3
-public class Store {
4
-	private String addr;
5
-	private String code;
6
-	private String lat;
7
-	private String lng;
8
-	private String name;
9
-	private String type;
10
-	
11
-	
12
-	public String getAddr() {
13
-		return addr;
14
-	}
15
-	public void setAddr(String addr) {
16
-		this.addr = addr;
17
-	}
18
-	public String getCode() {
19
-		return code;
20
-	}
21
-	public void setCode(String code) {
22
-		this.code = code;
23
-	}
24
-	public String getLat() {
25
-		return lat;
26
-	}
27
-	public void setLat(String lat) {
28
-		this.lat = lat;
29
-	}
30
-	public String getLng() {
31
-		return lng;
32
-	}
33
-	public void setLng(String lng) {
34
-		this.lng = lng;
35
-	}
36
-	public String getName() {
37
-		return name;
38
-	}
39
-	public void setName(String name) {
40
-		this.name = name;
41
-	}
42
-	public String getType() {
43
-		return type;
44
-	}
45
-	public void setType(String type) {
46
-		this.type = type;
47
-	}
48
-}

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

@@ -1,186 +0,0 @@
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
-}

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

@@ -1,40 +0,0 @@
1
-package kr.co.swh.lecture.database.java;
2
-
3
-import java.util.List;
4
-
5
-public class Wheather {
6
-
7
-	public static void main(String[] args) {
8
-		// TODO Auto-generated method stub
9
-		
10
-		//	수집코드
11
-		
12
-		//	데이터베이스 저장
13
-		String dbUrl = "127.0.0.1";
14
-		String dbName = "market";
15
-		String user = "root";
16
-		String password = "swhacademy!";
17
-
18
-		
19
-		
20
-		DatabaseUtil u = new DatabaseUtil(dbUrl, dbName, user, password);
21
-//		u.dbInsert(stores)
22
-		List<Employee> l = u.dbSelect("차");
23
-		for(Employee e : l) {
24
-			spsp
25
-			
26
-			sdfsd
27
-			sdf
28
-			df
29
-			d
30
-			d
31
-		}
32
-		
33
-		
34
-		
35
-		DatabaseUtil u1 = new DatabaseUtil();
36
-		u1.dbInsert(store);
37
-//		u.dbInsert(stores)
38
-	}
39
-
40
-}