-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJDBCAdapter.java
More file actions
532 lines (470 loc) · 18.6 KB
/
Copy pathJDBCAdapter.java
File metadata and controls
532 lines (470 loc) · 18.6 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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
/*
* @author dyf 2012
* To change this template, choose Tools | Templates
* and open the template in the editor.
* http://download.oracle.com/javase/1.4.2/docs/api/javax/swing/JTable.html
* http://download.oracle.com/javase/1.4.2/docs/api/javax/swing/table/TableModel.html
* http://mindprod.com/jgloss/jtable.html
* http://mindprod.com/jgloss/calendar.html
* http://dmivic.chat.ru/JDBC/introTOC.doc.html
* http://www.javaportal.ru/java/articles/JDBC_java_BD.html
* http://www.javable.com/tutorials/fesunov/lesson8/
* http://habrahabr.ru/blogs/java/75661/ - Public Morosov
* http://download.oracle.com/javase/tutorial/reflect/member/ctorLocation.html - поиск конструктора
* http://www.quizful.net/post/java-reflection-api
* Class.getDeclaredConstructor (String.class). NewInstance ( "HERESMYARG");
* Class.getDeclaredConstructors(types list).newInstance(args list);
*/
package JAdapter;
import autopartner.Asrt;
import autopartner.Tools;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableModel;
// Класс для работы с БД,связи БД-модель и грида.
public class JDBCAdapter extends AbstractTableModel implements ModelInterface {
protected JTableData jtData = null;
private static Logger log = Logger.getLogger(ConnManager.class.getName());
private static final long serialVersionUID = 1L;
protected boolean blockWorkWithDB = false;
protected boolean isTableEditable = true;
protected String nameConnect = "";
protected DBInterface dbTable = null;
// --------------------------------Constructors----------------
protected JDBCAdapter() {
super();
log.fine("try get connect...");
this.dbTable = new JDBTable();
this.jtData = new JTableData();
}
public JDBCAdapter(String TableName, String PrimaryKey, String Query) {
this();
log.fine("costructor with parapetrs:" + TableName + "," + PrimaryKey + "," + Query);
JDBCAdapterConnect(TableName, PrimaryKey, Query);
}
public JDBCAdapter(String Url, String UserName, String Password,
String TableName, String PrimaryKey, String Query) {
super();
//driverName=DrvName;
log.info("Full construcotr for Remote access.");
JDBCAdapterConnect(TableName, PrimaryKey, Query);
}
public JDBCAdapter(DBInterface dbi,String tableName, String primaryKey, String query) {
this();
this.dbTable=dbi;
JDBCAdapterConnect(tableName, primaryKey, query);
}
//---------------------------------------------------
protected void JDBCAdapterConnect(String tableName, String primaryKey, String query) {
log.info("Try create model");
boolean r=false;
r=this.dbTable.Connect(this.jtData,tableName,primaryKey,query);
if (r){
this.CreateModel(true);//пробуем создать таблицу
}else{
log.warning("error: connect("+tableName+","+primaryKey+","+query+")");
}
}
//---------------------------------Others protected--------------------------
protected int GetMaxID() {
int max = 0, curID = 0;
int col = this.GetColumnIndex(this.dbTable.getPrmKey());
for (int i = 0; i < getRowCount(); i++) {
curID = this.GetId(col);
if (max < curID) {
max = curID;
}
}
return max;
}
protected Vector<Object> GetHeader() {
Vector<Object> head = new Vector();
for (int i = 0; i < this.getColumnCount(); i++) {
head.addElement(this.jtData.columnNames[i]);
}
return head;
}
protected String GetHeaderString() {
String head = "";
for (int i = 0; i < this.getColumnCount(); i++) {
head = head + " " + this.getColumnName(i);
}
return head;
}
protected Vector<Object> GetAllData() {
Vector<Object> resSet = new Vector();
for (int i = 0; i < this.jtData.rows.size(); i++) {
resSet.addElement(this.jtData.rows.get(i));
}
return resSet;
}
protected void CreateModel(boolean newTable) {//fl= true-(пере)создать модель, false-обновить данные(структура не меняется)
this.dbTable.fillData(newTable);
if (newTable == true) {
fireTableStructureChanged();
} else {
fireTableDataChanged();
}
}
protected int GetInt(int row, int col) {
int id = 0;
Object tmpO = getValueAt(row, col);
if (tmpO == null) {
return -1;
}
try {
id = new Integer(tmpO.toString());
} catch (Exception e) {
System.err.println("error convert:" + e);
}
return id;
}
protected Vector<Object> CreateEmptyRow() {
Vector<Object> tmpObj = new Vector();
for (int i = 0; i < this.jtData.vColObject.size(); i++) {
tmpObj.addElement(null);//vColObject.get(i));
}
return tmpObj;
}
protected String GetStringAt(int row, int col) {
if ((col < 0) || (col > getColumnCount())) {
return "Null";
}
if ((row < 0) || (row > getRowCount())) {
return "Null";
}
Object obj = getValueAt(row, col);
if (obj == null) {
return "Null";
}
return obj.toString();
}
public void DeleteColumn(String colName) {
this.dbTable.DeleteColumn(colName);
this.CreateModel(true); //БД изменилась - перечитать данные, чтобы обновить таблицу и модель
}
public void AddColumn(String colName, String type, String afterColumnName) {//пример "CHAR(10) NULL" или "FLOAT(8,2) DEFAULT 0"
this.dbTable.AddColumn(colName, type, afterColumnName);
this.CreateModel(true); //БД изменилась - перечитать данные, чтобы обновить таблицу и модель
}
public void AddColumnLast(String colName, String type) {//пример "CHAR(10) NULL" или "FLOAT(8,2) DEFAULT 0"
this.dbTable.AddColumnLast(colName, type);
this.CreateModel(true); //БД изменилась - перечитать данные, чтобы обновить таблицу и модель
}
public void UpdateSructure() {
this.CreateModel(true);
}
public boolean AddRemoteData(String Url, String UserName, String Password) {
this.dbTable.AddRemoteData(Url, UserName, Password);
this.blockWorkWithDB = true;//теперь обновлять и т.п. нельзя,т.к. потеряем результат объединения
fireTableDataChanged();
return true;
}
/**
* Добавить данные из другой таблицы
* @param data - произаольная модель(TableModel)
* @return - истина,если данные добавились
* @see "добавляет только,если совпадают имена столбцов!"
*/
public boolean AddData(TableModel data) {
if (data.getRowCount() < 1) {
return false;
}
//итак,есть 2 табле модел,надо их объединить:
//1.ищем совпадающие колонки
Map<Integer, String> columnNum = new HashMap<Integer, String>();
//List<String> columnName=new ArrayList();
String name1 = "", name2 = "";
for (int i = 0; i < this.getColumnCount(); i++) {
name1 = this.getColumnName(i);
for (int j = 0; j < data.getColumnCount(); j++) {
name2 = data.getColumnName(j);
if (name1.equals(name2)) {
//columnName.add(name1);
columnNum.put(j, name2);//запоминаем индекс и имя столбца
break;
}
}
}
if (columnNum.isEmpty() == true) {
return false;
}
//2.добавляем данные
//Iterator Iter=columnName.iterator();
Vector newRow = null;
Object tmpObj = null;
for (int iRow = 0; iRow < data.getRowCount(); iRow++) {//проходим по строкам
newRow = this.CreateEmptyRow();
for (Map.Entry<Integer, String> entry : columnNum.entrySet()) {//перебираем все совпавшие колонки
tmpObj = data.getValueAt(iRow, entry.getKey());//получаем значение
newRow.setElementAt(tmpObj, entry.getKey());//заполняем
}
this.jtData.rows.addElement(newRow);//добавляем новую,заполненную строку
int numID = this.GetMaxID() + 1;//текущий максимум
int prmColumn = this.GetColumnIndex(this.dbTable.getPrmKey());
this.setValueAt(numID + 1, this.getRowCount(), prmColumn);
}
this.blockWorkWithDB = true;//теперь обновлять и т.п. нельзя,т.к. потеряем результат объединения
return true;
}
/**
* Сохраняет текущие данные во временную таблицу БД,используя текущее соединение
* @param name - имя временной таблицы в БД
* @return - истина в случае удачи
* @see "Если накидать данные и сохранить их,то можно в последствии сделать по ним выборку для группировки и сортировки"
*/
public boolean SaveAllToTempDBTable(String tableName) {
this.dbTable.SaveAllToTempDBTable(tableName);
return true;
}
/**
* Ищет строку по id
* @param id
* @return возвращает строку
*/
public int FindRowID(int id) {
if (this.getRowCount() < 1) {
Asrt.perr("Нельзя найти,пустая таблица!");
return -1;//предупреждение - пустая таблица!
}
//получаем номер колонки с первичным ключом
int colPrimaryKey = GetColumnIndex(this.dbTable.getPrmKey());
//проходим по всей колонке
Object tmpObj = null;
Object Id = id;
for (int i = 0; i < getRowCount(); i++) {
tmpObj = getValueAt(i, colPrimaryKey);
if (tmpObj != null) {
if (tmpObj.equals(Id)) {
return i;
}
}
}
Asrt.perr("Не найден ИД=" + id);
return -1;
}
//---------------------------- GET -----------------------------------
/**
* Возвращает столбец
* @param name-имя столбца
* @return вектор-столбец
*/
public Vector<String> GetColumnDataVector(String name) {
Vector<String> strList = new Vector<String>();
if (Asrt.is_str(name) == false) {
return strList;
}
// if (name == null) {
// Asrt.perr("Collumn "+name + " not found!");
// return strList;
// }
//
// if (name.length() == 0) {
// return strList;
// }
int col = GetColumnIndex(name);
if (col < 0) {
Asrt.perr("Column " + name + " not found");
return strList;
}
String str = null;
Object obj = null;
for (int i = 0; i < getRowCount(); i++) {
obj = getValueAt(i, col);
str = null;
if (obj != null) {
str = obj.toString();
}
if (str == null) {
strList.add("Null");
} else {
strList.add(str); //проверить работу при получении null от гета!
}
}
return strList;
}
/**
* Получить ИД строки по номеру строки
* @param rowModel - номер строки
* @return ИД строки
*/
public int GetId(int rowModel) {
if (Asrt.is_int_negative(rowModel) == true) {
return -1;
}
int colModel = GetColumnIndex(this.dbTable.getPrmKey());
int res = GetInt(rowModel, colModel);
return res;
}
/**
* Получить значение ячейки модели
* @param rowModel - строка
* @param fieldName - имя столбца
* @return значение ячейки
*/
public Object GetValues(int rowModel, String fieldName) {
int colModel = GetColumnIndex(fieldName);
return getValueAt(rowModel, colModel);
}
public TableModel GetModel() {
return this;
}
/**
* получить номер столбца по имнени
* @param colName - имя столбца
* @return номер столбца
*/
public int GetColumnIndex(String colName) {
if (Asrt.is_str(colName) == false) {
return -1;
}
String clName = null;
for (int col = 0; col < getColumnCount(); col++) {
clName = getColumnName(col);
if (clName.equalsIgnoreCase(colName)) {
return col;
}
}
//Asrt.perr("Не найден столбец:" + colName);
return -1;
}
public final String[] GetColumnNames() {
return this.jtData.columnNames;//не безопасно - возможно поменять извне
}
//-------------------------- SET --------------------------------------
public void SetTableEditable(boolean flag) {
isTableEditable = flag;
}
public void SetColumnEditable(boolean flag, String columnName) {
int col = this.GetColumnIndex(columnName);
if (Asrt.is_int_negative(col) == true) {
return;
}
this.jtData.bIsEditableColumn[col] = flag;
}
//----------------- =Ф-ции обновляющие данные таблицы из БД= ----------------------
public void MarkRowDeleted(int id) {
if (this.blockWorkWithDB == true) {
return;
}
this.dbTable.MarkRowDeleted(id);
this.Update();//БД изменилась - перечитать данные,чтобы обновить таблицу и модель
}
/**
* Удаляет строку по id
* @param id
*/
public void DeleteRow(int id) {
if (this.blockWorkWithDB == true) {
return;
}
if (Asrt.is_int_negative(id) == true) {
return;
}
this.dbTable.DeleteRow(id);
this.Update();//БД изменилась - перечитать данные,чтобы обновить таблицу и модель
}
/**
* Добавить пустую строку
* @return возвращает ИД!
*/
public int AddRow() {
if (this.blockWorkWithDB == true) {
return -1;
}
fireTableDataChanged();
return this.dbTable.AddRow();
}
public void UpdateRow(int id) {
if (this.blockWorkWithDB == true) {
return;
}
this.dbTable.UpdateRow(id);
fireTableDataChanged();
}
public void Update() {
if (this.blockWorkWithDB == true) {
return;
}
CreateModel(false);
}
//-----------=Интерфейс TableModel=------------------------------------------
@Override
public Class getColumnClass(int column) {
// @Override
// public Class<?> getColumnClass(int col) {
// Class<?> c = Object.class;
// try {
// c = (Class<?>) vColObject.get(col);
// } catch (RuntimeException e) {
// System.err.println(e);
// }
// return c;
// }
if ((column < 0) || (column > getColumnCount())) {
System.err.println("getColumnClass error: column=" + column);
return String.class;
}
return JDBTable.getColClass(jtData.columnType[column]);
//return vColObject.get(column).getClass();
}
@Override
public boolean isCellEditable(int row, int column) {
if ((column < 0) || (column > getColumnCount())) {
return false;
}
if ((row < 0) || (row > getRowCount())) {
return false;
}
return this.jtData.bIsEditableColumn[column] && isTableEditable;
}
@Override//Возвращает количество полей таблицы
public int getColumnCount() {
return jtData.columnNames.length;
}
@Override//Возвращает количество записей
public int getRowCount() {
return jtData.rows.size();
}
@Override
public String getColumnName(int column) {
if ((column < 0) || (column > getColumnCount())) {
return "null";// возвращаем строку!
}
return new String(jtData.columnNames[column]);// проверить!!
}
@Override//Возвращает значение ячейки
public Object getValueAt(int aRow, int aColumn) {
if ((aColumn < 0) || (aColumn > getColumnCount())) {
return "getValueAt num column error";
}
if ((aRow < 0) || (aRow > getRowCount())) {
return "getValueAt num column error";
}
Vector row = (Vector) jtData.rows.elementAt(aRow);
Object t = null;//new Object();
t = row.elementAt(aColumn);
return t;
}
@Override
public void setValueAt(Object value, int row, int column) {//редактирует только this.tableName;
if ((column < 0) || (column > getColumnCount())) {
return;
}
if ((row < 0) || (row > getRowCount())) {
return;
}
// PreparedStatement pstmt = connection.prepareStatement(query);
// pstmt.executeUpdate(); // выполнить изменение
// pstmt.close();
// теперь осталось, изменить значение вектора rows
Object val;
//без этой функции работать не будет,если в setElementAt() вставлять непосредственно value а не val
val = this.dbTable.dbRepresentation(column, value);
Vector dataRow = (Vector) jtData.rows.elementAt(row);
dataRow.setElementAt(val, column);
}
};