-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTable.java
More file actions
executable file
·162 lines (94 loc) · 3.53 KB
/
Table.java
File metadata and controls
executable file
·162 lines (94 loc) · 3.53 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
import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
public class Table extends Database {
private String db_name;
private Connection con;
private String tableName;
private int columnAmt;
private LinkedHashMap<String, Column> columns = new LinkedHashMap<>();
/* Holds the column objects that are within the table */
public Table() {}
public Table(String name, String db_name, Connection con) {
this.con= con;
this.tableName = name;
this.db_name = db_name;
importColumns();
}
/* Loads the columns and the values they contain*/
/* Automatically is called when a Table object is created */
private void importColumns() {
String column = "SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = '"+tableName+"' AND TABLE_SCHEMA = '" +db_name + "';";
try { Statement statement = this.con.createStatement();
ResultSet query = statement.executeQuery(column);
int counter = 0;
while(query.next()) { /* While there is another column */
String columnName = query.getString(1);
Column col = new Column(con, tableName, columnName, counter);
/* ^ Create Column object ^ */
/* Counter is index location of column within table
* Values are auto-populated when column is initialized
*/
ArrayList<String> colVals = retrieveColumnValues(this.tableName, columnName);
/* Creates ArrayList of the column's values */
col.setColumnValues(colVals);
/* Set's column's values to the Column object that was created */
columns.put(col.getColumnName(), col);
counter ++;
/* Adds column to ArrayList consisting of the Table object's columns */
}
this.columnAmt = this.columns.size();
} catch(Exception e) {System.out.println(e);}
}
/* Gets the values within a column */
private ArrayList<String> retrieveColumnValues(String tableName, String columnName) {
ArrayList<String> values = new ArrayList<String>(0);
try { Statement statement = this.con.createStatement();
ResultSet query = statement.executeQuery("SELECT " +db_name+"."+tableName+"."+columnName+" FROM " + db_name + "." + tableName + ";");
while(query.next()) {
values.add(query.getString(1));
}
} catch (Exception e) {System.out.println(e);};
return values;
}
/* Returns column names */
public ArrayList<String> getColumnNames(){
ArrayList<String> colNames = new ArrayList<>();
if(columns.isEmpty() == false) {
for (String key : columns.keySet()) {
colNames.add(key);
}
}
return colNames;
}
/* Lists Column Names */
public void listColNames() {
if(columns.isEmpty() == false) {
for (String key : columns.keySet()) {
System.out.println(key);
}
}
}
public Column getColumnObject(String columnName) {
return columns.get(columnName);
}
/* Returns amount of columns in the form of an int */
public int getColumnAmount() {
return columnAmt;
}
public int getColumnSize(String name) {
return columns.get(name).size();
}
public HashMap<String, Column> getColumns(){
return columns;
}
public void listColValues(String columnName) {
if (columns.containsKey(columnName)) {
ArrayList<String> values = columns.get(columnName).getColumnValues();
for (int i = 0; i <= values.size() -1; i++) {
System.out.println(values.get(i));
}
}
}
}