source: main/trunk/model-sites-dev/cambridge-museum/collect/waikato-independent/pre-import/EditableDatabaseTable/src/jquery/datatables/model/DataRepository.java@ 34511

Last change on this file since 34511 was 34511, checked in by davidb, 4 years ago

Evolution of code away from Company model to one that uses RecordHashmap to represent the rows coming out of the JDBC database

File size: 4.4 KB
Line 
1package jquery.datatables.model;
2
3import java.sql.*;
4import java.util.ArrayList;
5import java.util.LinkedList;
6import java.util.List;
7
8public class DataRepository
9{
10 private static String JDBC_URI = null;
11 private static String JDBC_DB_TABLE = null;
12
13 static {
14 try {
15 // Included to trigger maven including the necessary Java class and native files
16 // in the generated 'with-dependencies.jar' file
17
18 // Looks like this step could be avoided if more details are stored in the maven
19 // pom.xml file:
20 // https://stackoverflow.com/questions/16725377/no-suitable-driver-found-sqlite
21
22 Class.forName("org.sqlite.JDBC");
23 }
24 catch (Exception e) {
25 e.printStackTrace();
26 }
27
28 }
29
30 /// <summary>
31 /// Singleton collection of companies
32 /// </summary>
33 private static List<RecordHashmap> RecordDataList = null;
34
35 private static ArrayList<String> _columnLabels = null;
36
37 protected static ArrayList<String> initJDBCColumnLabels(Connection connection, String table_name)
38 {
39 // Read in column labels into ArrayList
40 ArrayList<String> column_labels = null;
41
42 try {
43 Statement st = connection.createStatement();
44 ResultSet rs = st.executeQuery("SELECT * from " + table_name);
45
46 ResultSetMetaData rs_md = rs.getMetaData();
47
48 int columnCount = rs_md.getColumnCount();
49
50 column_labels = new ArrayList<String>(columnCount);
51
52 for (int i=1; i<=columnCount; i++) {
53 String heading = rs_md.getColumnLabel(i);
54 column_labels.add(heading); // add even if null, so can keep count
55 if (heading == null) {
56 System.err.println("*** Encountered null column label at column postion: " + i);
57 }
58 }
59 }
60 catch (Exception e) {
61 e.printStackTrace();
62 }
63
64 return column_labels;
65 }
66
67 public static ArrayList<String> getColumnHeadings()
68 {
69 return _columnLabels;
70 }
71
72 public static void init(String jdbc_uri, String jdbc_db_table)
73 {
74 JDBC_URI = jdbc_uri;
75 JDBC_DB_TABLE = jdbc_db_table;
76
77 System.err.println("Opening: '" + JDBC_URI + "' ...");
78 try (Connection connection = DriverManager.getConnection(JDBC_URI)) {
79 System.err.println("... connnected");
80
81 _columnLabels = initJDBCColumnLabels(connection, JDBC_DB_TABLE);
82
83 connection.close();
84 }
85 catch (SQLException ex) {
86 ex.printStackTrace();
87 }
88 }
89
90
91 /// <summary>
92 /// Method that returns all companies used in this example
93 /// </summary>
94 /// <returns>List of companies</returns>
95 public static List<RecordHashmap> GetRecords()
96 {
97 if (RecordDataList == null) {
98 System.err.println("Opening: '" + JDBC_URI + "' to query ...");
99
100 try (Connection connection = DriverManager.getConnection(JDBC_URI)) {
101 System.err.println("... connnected");
102
103 if (_columnLabels == null) {
104 System.err.println("Error: Column Labels should already be initialized, but are null");
105 return null;
106 }
107 else {
108 int column_count = _columnLabels.size();
109
110 if (column_count>=2) {
111 Statement input_statement = connection.createStatement();
112 ResultSet input_resultset = input_statement.executeQuery("SELECT * from " + JDBC_DB_TABLE);
113
114 RecordDataList = new LinkedList<RecordHashmap>();
115
116 int line_count = 0;
117
118 while (input_resultset.next()) {
119
120 int id = input_resultset.getInt(1);
121 RecordHashmap record_hashmap = new RecordHashmap(id, column_count);
122
123 for (int i=2; i<=column_count; i++) {
124 String val = input_resultset.getString(i);
125 String key = _columnLabels.get(i-1); // columnLabels arraylist is from 0, not 1 like sql columns
126
127 record_hashmap.setFieldValue(key,val);
128 }
129
130 RecordDataList.add(record_hashmap);
131
132 line_count++;
133 if ((line_count % 1000) == 0) {
134 System.out.println("Processed lines " + (line_count - 999) + "-" + line_count);
135 }
136
137 if (line_count == 5000) {
138 // ******
139 System.err.println("******* During development, artifically cutting off the Database read in at 5000 entries");
140 break;
141 }
142 }
143 }
144 else {
145 System.err.println("Warning: ignoring database table as it only has one column to it");
146 }
147
148 }
149
150 connection.close();
151 }
152 catch (SQLException ex) {
153 ex.printStackTrace();
154 }
155
156 }
157
158 return RecordDataList;
159 }
160
161}
Note: See TracBrowser for help on using the repository browser.