Java uses a hash table to operate the database Using Hashtables to Store & Extract Results from A Database.

zhaozj2021-02-11  194

Example of using a hash table

// * This code is distributed in the hope that it will be useful, * // * but WITHOUT ANY WARRANTY;. Without even the implied warranty of * // * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE * // **** *********************************************************** ************** / // USING HashTables to Store &

Extract results from a Database.// // These functions are an example on how to get the data out of a // resultset from a database and store the values ​​in a Hashtable.// several of the function are then an example on how to get the data // out of the hashtables. Why would you want to do all this work? // Maintaining a database connection over the web is expensive. By // dumping the data into a hashtable you can minimize the amount of // time you stay connected to your database Also by storing the data // in a hashtable offers flexible way to pass your data from // object to object.// // This is a set of five functions //// Function makeHashTable.: // Takes a database ResultSet and places the data into a // Hashtable array for later use.//// Function cleanHashTable:. // Takes a Hashtable array and removes the unused portion // of a hashtable array for example: You use Makehashtable // and Since It Allocates The Hashtable Array In Chunks of 20, // ITS Possible That Creates A Hashtable of Siz e 40, but // only the first 22 indexes are used So makeHashTable calls // the cleanHashTable function which resizes the Hashtable // array to only 22 indexes.////Function columnOrder:. // Since a Hashtable does not guarantee to maintain the order // of the elements put into it. This function produces a // hashtable to store the column order of the ResultSet //// function hastToTabFile // An example on how to take a hashtable produced by the // makeHashTable function and turn it into a tab delimited // output string (used to download a dataresult as a flatfile) // This function uses a the results from the columnOrder // function to navigate the hashtable. If this function can`

t // find this index hashtable, it then passes the hashtable // to the hashToTab Function to step through the hashtable using // enumeration methods.////Function hashToTab // If no index hasharray was found then this function uses // ENUMERATION TO Step Through The Hashtable and Return A // Result /////// --i Suspect Using a Vector Would Give Much Faster Results .// - IV You Are Using Java 1.2 You Should consider using an ArrayList, // HashSet or TreeSet rather than a Hashtable or a Vector.// -Use a Hashtable or Vector when you want java 1.1.x compatibility //// public Hashtable [] makeHashTable (ResultSet ars_data) {int li_columns = 0; int li_rowcount = 0; Hashtable [] lht_results = new Hashtable [20]; try {// 1) get the column count and store our column order information // in our first index of our Hashtable arrayResultSetMetaData lmeta_data = ars_data.getMetaData (); li_columns = lmeta_data.getColumnCount (); if (li_columns> 0) {lht_results [li_rowcount] = columnorder (lmeta_ Data, li_columns; li_rowcount ;} // 2) loop through the result set and add the data 1 row at a time to // the Hashtable ArrayWhile (ARS_DATA.NEXT ()) {// 3) ife at the last index of our hashtable then expand it // by another 20 indexesif (li_rowcount == lht_results.length) {Hashtable [] lht_temp = new Hashtable [lht_results.length 20]; for (int li_loop = 0; li_loop

Try {Luo_Value = ARS_DATA.GETObject (i);} catch (exception e) {} if (luo_value == null) Luo_Value = new string (""); lht_row.put (lmeta_data.getcolumnlabel (i), luo_value);} lht_results [li_rowcount] = lht_row; li_rowcount ;}} catch (SQLException e) {} if (lht_results [0] == null) {return null;} return cleanHashTable (lht_results);} private Hashtable [] cleanHashTable (Hashtable [] aht_data ) {Hashtable [] lht_temp = null; int li_total_rows = aht_data.length; // 1) loop thru and determine where the first null row appearsfor (int i = 0; i

} public string hasttotabfile (hashtable [] AHASH_DATA, BOOLEAN AB_HEADER) {// **************************************************** *************************************** / / AHASH_DATA: Array of Hashtables To Convert To Tabfile // Ab_ Header: True If You Want THE TAB File to include the headers // ********************************************************** ************************ STRING LS_TABFILE = ""; if (ahash_data == null) {// 1) if no data the return Empty filels_tabfile = "";} else {// 2) first get column headersint li_column_count = 0; String ls_column_count = ahash_data [0] .get ( "column_Count") toString ();. try {li_column_count = Integer.parseInt (ls_column_count);} catch (NumberFormatException e) {// 3) since this hashtable doesnt have the the column data stashed // treat it as a normal hashtable array return hashToTab (ahash_data, ab_header);} // 4) Gather up each columns label / key name also Build up the header columnstring [] ls_indexes = new string [li_column_count]; for (int ICol = 0; ICOL

} private string hashtotab (havehtable [] AHASH_DATA, BOOLEAN AB_HEADER) {// **************************************************** *************************************** / / AHASH_DATA: Array of Hashtables To Convert To Tabfile // Ab_ Header: True If You Want THE TAB File to include the headers // ********************************************************** ************************* STRING LS_TABFILE = ""; if (ahash_data == null) {// 1) if no data return Empty files_tabfile = " ";} else {// 2) IF requested print out the header filesif (ab_header) {for (Enumeration lenum_header = ahash_data [0] .keys (); lenum_header.hasMoreElements ();) {String ls_col = lenum_header.nextElement () .tostring () ""; ls_tabfile = ls_tabfile ls_col;} ls_tabfile = ls_tabfile "" "" "" "" "" "" "" "" "" // 3) loop through the rows and gather tha data to displayfor (int i = 0; i

转载请注明原文地址:https://www.9cbs.com/read-3676.html

New Post(0)