forked from cochung08/cse532project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQueryFunctions.java
106 lines (70 loc) · 2.38 KB
/
QueryFunctions.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import java.sql.*;
import java.util.LinkedHashMap;
import java.util.Vector;
import com.ibm.db2.jcc.am.ResultSet;
public class QueryFunctions {
public static void updateArticleTable(
LinkedHashMap<String, String> updateMap) {
String tableName = "article2";
try {
String tmp0 = "UPDATE " + tableName + " SET ";
String tmp1 = "";
for (String key : updateMap.keySet()) {
if (!key.equalsIgnoreCase("ARTICLE_ID"))
tmp1 = tmp1 + key + " = ?, ";
}
tmp1 = tmp1.substring(0, tmp1.length() - 2) + " ";
String tmp2 = "WHERE ARTICLE_ID = " + updateMap.get("ARTICLE_ID");
String updated_Query = tmp0 + tmp1 + tmp2;
System.out.println(updated_Query);
PreparedStatement ps_update = DatabaseConnection.conn
.prepareStatement(updated_Query);
int j = 1;
for (String key : updateMap.keySet()) {
if (!key.equalsIgnoreCase("ARTICLE_ID")) {
ps_update.setString(j, updateMap.get(key));
j++;
}
}
ps_update.executeUpdate();
DatabaseConnection.conn.commit();
} catch (SQLException e) {
e.printStackTrace();
}
}
public static LinkedHashMap<String, String> searchQuery(String searchTable,
String searchField, String searchValue) {
try {
LinkedHashMap<String, String> fieldValue = new LinkedHashMap<String, String>();
;
String search_query = "SELECT * from " + searchTable + " where "
+ searchField + " = " + searchValue;
Statement st = DatabaseConnection.conn.createStatement();
ResultSet rset = (ResultSet) st.executeQuery("SELECT * FROM "
+ searchTable);
ResultSetMetaData md = rset.getMetaData();
Vector<String> fieldInArticleTable = new Vector<String>();
for (int i = 1; i <= md.getColumnCount(); i++) {
fieldInArticleTable.add(md.getColumnLabel(i));
}
PreparedStatement ps_search = DatabaseConnection.conn
.prepareStatement(search_query);
ResultSet rs = (ResultSet) ps_search.executeQuery();
DatabaseConnection.conn.commit();
while (rs.next()) {
for (int i = 0; i < fieldInArticleTable.size(); i++) {
String tmp = rs.getString(fieldInArticleTable.get(i));
fieldValue.put(fieldInArticleTable.get(i), tmp);
// System.out.println(fieldInArticleTable.get(i) + ": " +
// tmp);
}
}
rs.close();
return fieldValue;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}
}