-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathQueryJDBC.java
More file actions
executable file
·257 lines (202 loc) · 6.63 KB
/
QueryJDBC.java
File metadata and controls
executable file
·257 lines (202 loc) · 6.63 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package queryrunner;
import java.sql.Connection;
import java.sql.*;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Scanner;
import java.util.ArrayList;
/**
*
* @author mckeem
*/
public class QueryJDBC {
public Connection m_conn = null;
static final String DB_DRV = "com.mysql.jdbc.Driver";
String m_error="";
String m_url;
String m_user;
String [] m_headers;
String [][] m_allRows;
int m_updateAmount = 0;
QueryJDBC ()
{
m_updateAmount = 0;
}
public String GetError()
{
return m_error;
}
public String [] GetHeaders()
{
return this.m_headers;
}
public String [][] GetData()
{
return this.m_allRows;
}
public int GetUpdateCount()
{
return m_updateAmount;
}
// We think we can always setString on Parameters. Not sure
// if this is true.
// GetString on Results is fine though
public boolean ExecuteQuery(String szQuery, String [] parms, boolean [] likeparms)
{
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
int nColAmt;
boolean bOK = true;
// Try to get the columns and the amount of columns
try
{
preparedStatement=this.m_conn.prepareStatement(szQuery);
int nParamAmount = parms.length;
for (int i=0; i < nParamAmount; i++)
{
String parm = parms[i];
if (likeparms[i] == true)
{
parm += "%";
}
preparedStatement.setString(i+1, parm);
}
//preparedStatement.setString(1, "%" + szContact + "%");
resultSet=preparedStatement.executeQuery();
ResultSetMetaData rsmd = resultSet.getMetaData();
nColAmt = rsmd.getColumnCount();
m_headers = new String [nColAmt];
for (int i=0; i< nColAmt; i++)
{
m_headers[i] = rsmd.getColumnLabel(i+1);
}
//
int amtRow = 0;
while(resultSet.next()){
amtRow++;
}
if (amtRow > 0)
{
this.m_allRows= new String [amtRow][nColAmt];
resultSet.beforeFirst();
int nCurRow = 0;
while(resultSet.next())
{
for (int i=0; i < nColAmt; i++)
{
m_allRows[nCurRow][i] = resultSet.getString(i+1);
}
nCurRow++;
}
}
else
{
this.m_allRows= new String [1][nColAmt];
for (int i=0; i < nColAmt; i++)
{
m_allRows[0][i] = "";
}
}
preparedStatement.close();
resultSet.close();
}
catch (SQLException ex)
{
bOK = false;
this.m_error = "SQLException: " + ex.getMessage();
this.m_error += "SQLState: " + ex.getSQLState();
this.m_error += "VendorError: " + ex.getErrorCode();
System.out.println("SQLException: " + ex.getMessage());
System.out.println("SQLState: " + ex.getSQLState());
System.out.println("VendorError: " + ex.getErrorCode());
return false;
}
return true;
}
public boolean ExecuteUpdate(String szQuery, String [] parms)
{
PreparedStatement preparedStatement = null;
boolean bOK = true;
m_updateAmount=0;
// Try to get the columns and the amount of columns
try
{
preparedStatement=this.m_conn.prepareStatement(szQuery);
int nParamAmount = parms.length;
for (int i=0; i < nParamAmount; i++)
{
preparedStatement.setString(i+1, parms[i]);
}
m_updateAmount =preparedStatement.executeUpdate();
preparedStatement.close();
}
catch (SQLException ex)
{
bOK = false;
this.m_error = "SQLException: " + ex.getMessage();
this.m_error += "SQLState: " + ex.getSQLState();
this.m_error += "VendorError: " + ex.getErrorCode();
System.out.println("SQLException: " + ex.getMessage());
System.out.println("SQLState: " + ex.getSQLState());
System.out.println("VendorError: " + ex.getErrorCode());
return false;
}
return true;
}
public boolean ConnectToDatabase(String host, String user, String pass, String database)
{
String url;
url = "jdbc:mysql://";
url += host;
url +=":3306/";
url += database;
url +="?useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC";
try
{
Class.forName(DB_DRV).newInstance();
m_conn = DriverManager.getConnection(url,user,pass);
}
catch (SQLException ex)
{
m_error = "SQLException: " + ex.getMessage() +
ex.getSQLState() +
ex.getErrorCode();
return false;
}
catch (Exception ex)
{
// handle the error
m_error = "SQLException: " + ex.getMessage();
return false;
}
return true;
}
/* Document this function
// TODO
*/
public boolean CloseDatabase()
{
try
{
m_conn.close();
}
catch (SQLException ex)
{
m_error = "SQLException: " + ex.getMessage();
m_error = "SQLState: " + ex.getSQLState();
m_error = "VendorError: " + ex.getErrorCode();
return false;
}
catch (Exception ex)
{
m_error = "Error was " + ex.toString();
return false;
}
return true;
}
}