-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTableMethods.java
More file actions
205 lines (156 loc) · 6.52 KB
/
TableMethods.java
File metadata and controls
205 lines (156 loc) · 6.52 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
import java.sql.*;
import java.util.*;
public class TableMethods {
/* ========================================================================
* Select all returns 2-d array with all fields
* SELECT * FROM tableName;
* @param String, Connection
* @return String[][]
* =======================================================================*/
public static String[][] selectAll(String tableName, Connection con) throws SQLException{
Statement stmt =con.createStatement();
String query = "SELECT * FROM " + tableName + ";";
ResultSet rs = stmt.executeQuery(query);
ResultSetMetaData rsmd = rs.getMetaData();
int columnCount = rsmd.getColumnCount();
int rowCount = getRowCount(tableName,con);
String[][] arr = new String[rowCount][columnCount];
int r = 0;
while(rs.next()){
for(int c = 0 ; c < columnCount ; c++){
arr[r][c] = rs.getString(c+1);
}
++r;
}
return arr;
}
/* ========================================================================
* Only returns column information that you ask for
* @param String, String[], Connection
* @return String[][]
* =======================================================================*/
public static String[][] selectXFrom(String tableName, String[] ids, Connection con) throws SQLException{
Statement stmt =con.createStatement();
String search = "";
for(int i = 0; i < ids.length ; i++){
search += ids[i];
if (i < (ids.length-1)){
search += ", ";
}
}
String query = "SELECT " + search + " FROM " + tableName + ";";
ResultSet rs = stmt.executeQuery(query);
ResultSetMetaData rsmd = rs.getMetaData();
int columnCount = ids.length;
int rowCount = getRowCount(tableName,con);
String[][] arr = new String[rowCount][columnCount];
int r = 0;
while(rs.next()){
for(int c = 0 ; c < columnCount ; c++){
arr[r][c] = rs.getString(c+1);
}
++r;
}
return arr;
}
/* ========================================================================
* Select Where
* @param String, String[], Connection
* @return String[][]
* =======================================================================*/
public static String[][] selectWhere(String tableName, String id, int low, int high, Connection con) throws SQLException{
Statement stmt =con.createStatement();
String query = "SELECT * FROM " + tableName +
" WHERE " + id + ">" + low + " and " + id + " < " + high + ";";
ResultSet rs = stmt.executeQuery(query);
ResultSetMetaData rsmd = rs.getMetaData();
int columnCount = rsmd.getColumnCount();
int rowCount = 0;
while (rs.next()){
rowCount = rs.getInt(1);
}
String[][] arr = new String[rowCount][columnCount];
int r = 0;
while(rs.next()){
for(int c = 0 ; c < columnCount ; c++){
arr[r][c] = rs.getString(c+1);
}
++r;
}
return arr;
}
/* ========================================================================
* get All returns all column indexes from any given table
* @param String
* @return ArrayList<String>
* ========================================================================*/
public static ArrayList<String> getAllColumnNames(String tableName,Connection con) throws SQLException {
DatabaseMetaData meta = con.getMetaData();
ArrayList<String> list = new ArrayList<String>();
ResultSet rs = meta.getColumns(null, null, tableName,null);
while(rs.next()) {
String columns = rs.getString("COLUMN_NAME");
list.add(columns);
}
return list;
}
/* ========================================================================
* get Unique returns all unique column indexes from any given table
* @param String
* @return ArrayList<String>
* ========================================================================*/
public static ArrayList<String> getUniqueColumns(String tableName, Connection con) throws SQLException {
DatabaseMetaData meta = con.getMetaData();
ArrayList<String> list = new ArrayList<String>();
ResultSet rs = meta.getIndexInfo(null, null, tableName, true, true);
while(rs.next()) {
String columns = rs.getString("COLUMN_NAME");
list.add(columns);
}
return list;
}
public static int[] getColumnTypes(String tableName, Connection con) throws SQLException{
Statement st =con.createStatement();
ResultSet rs = st.executeQuery("SELECT * FROM " + tableName);
ResultSetMetaData rsmd = rs.getMetaData();
int columnCount = rsmd.getColumnCount();
int[] a = new int[columnCount];
for(int r = 0 ; r < columnCount ; r++){
a[r] = rsmd.getColumnType(r + 1);
}
return a;
}
public static String[] getColumnTypeNames(String tableName, Connection con) throws SQLException{
Statement st =con.createStatement();
ResultSet rs = st.executeQuery("SELECT * FROM " + tableName);
ResultSetMetaData rsmd = rs.getMetaData();
int columnCount = rsmd.getColumnCount();
String[] a = new String[columnCount];
for(int r = 0 ; r < columnCount ; r++){
a[r] = rsmd.getColumnTypeName(r + 1);
}
return a;
}
/* ========================================================================
* insertAll
* ======================================================================*/
public static void insertAll(Connection con, String query) throws SQLException{
PreparedStatement insert = con.prepareStatement(query);
insert.executeUpdate();
insert.close();
}
/* ======================================================================
* count rows - returns the number of row in a table
* @params String, Connection
* @return int
* =====================================================================*/
public static int getRowCount(String table, Connection con) throws SQLException{
int count = 0;
Statement stmt =con.createStatement();
ResultSet res = stmt.executeQuery("SELECT COUNT(*) FROM "+table);
while (res.next()){
count = res.getInt(1);
}
return count;
}
}