-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDBHelper.java
More file actions
165 lines (129 loc) · 4.8 KB
/
Copy pathDBHelper.java
File metadata and controls
165 lines (129 loc) · 4.8 KB
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package sdsu;
import java.util.*;
import java.sql.*;
public class DBHelper implements java.io.Serializable {
private static String connectionURL = "jdbc:mysql://opatija:3306/jadrn000?user=jadrn052&password=stopper";
private static Connection connection = null;
private static Statement statement = null;
private static ResultSet resultSet = null;
public DBHelper() {}
public static Vector runQuery(String s) {
String answer = "";
Vector<String []> answerVector = null;
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
connection = DriverManager.getConnection(connectionURL);
statement = connection.createStatement();
resultSet = statement.executeQuery(s);
ResultSetMetaData rsmd = resultSet.getMetaData();
answerVector = new Vector<String []>();
while(resultSet.next()) {
String [] row = new String[rsmd.getColumnCount()];
for(int i=0; i < rsmd.getColumnCount(); i++)
row[i] = resultSet.getString(i+1);
answerVector.add(row);
}
}
catch(Exception e) {
e.printStackTrace();
}
//////////////////////////////////////////////////////////////////////////////////
// The finally clause always runs, and closes resources if open.
// DO NOT OMIT THIS!!!!!!
finally {
try {
if(resultSet != null)
resultSet.close();
if(statement != null)
statement.close();
if(connection != null)
connection.close();
}
catch(SQLException e) {
answer += e;
}
//////////////////////////////////////////////////////////////////////////
}
return answerVector;
}
public static Vector<String []> doQuery(String s) {
String user = "jadrn052";
String password = "stopper";
String database = "jadrn052";
String connectionURL = "jdbc:mysql://opatija:3306/" + database +
"?user=" + user + "&password=" + password;
Connection connection = null;
Statement statement = null;
ResultSet resultSet = null;
Vector<String[]> v = new Vector<String[]>();
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
connection = DriverManager.getConnection(connectionURL);
statement = connection.createStatement();
resultSet = statement.executeQuery(s);
ResultSetMetaData md = resultSet.getMetaData();
int numCols = md.getColumnCount();
while(resultSet.next()) {
String [] tmp = new String[numCols];
for(int i=0; i < numCols; i++)
tmp[i] = resultSet.getString(i+1); // resultSet getString is 1 based
v.add(tmp);
}
}
catch(Exception e) {
e.printStackTrace();
}
// IMPORTANT, you must make sure that the resultSet, statement and connection
// are closed, or a memory leak occurs in Tomcat.
finally {
try {
resultSet.close();
statement.close();
connection.close();
}
catch(SQLException e) {} // don't do anything if the connection is not open.
}
return v;
}
}
/*
// This method is appropriate for DB operations that do not return a result
// set, but rather the number of affected rows. This includes INSERT and UPDATE
public static int doUpdate(String s) {
int know = -1;
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
connection = DriverManager.getConnection(connectionURL);
statement = connection.createStatement();
know = statement.executeUpdate(s);
}
catch(Exception e) {
e.printStackTrace();
}
// IMPORTANT, you must make sure that the statement and connection
// are closed, or a memory leak occurs in Tomcat.
finally {
try {
statement.close();
connection.close();
}
catch(SQLException e) { }
}
return know;
}
}
/*
public static String getQueryResultTable(Vector<String []> v) {
StringBuffer toReturn = new StringBuffer();
toReturn.append("<table>");
for(int i=0; i < v.size(); i++) {
String [] tmp = v.elementAt(i);
toReturn.append("<tr>");
for(int j=0; j < tmp.length; j++)
toReturn.append("<td>" + tmp[j] + "</td>");
toReturn.append("</tr>");
}
toReturn.append("</table>");
return toReturn.toString();
}
*/