分享
 
 
 

JAVA 使用哈希表操作数据库的例子 Using Hashtables to Store & Extract results from a Database.

王朝java/jsp·作者佚名  2006-01-08
窄屏简体版  字體: |||超大  

使用哈希表操作数据库的例子

// * 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 it creates a hashtable of size 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

//

//////////////////////////////////////////////////////////////////////////

//

// Please note the following.

// -I suspect using a Vector would give much faster results .

// -If 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 array

ResultSetMetaData 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 array

while (ars_data.next())

{

// 3) If we are at the last index of our hashtable then expand it

// by another 20 indexes

if (li_rowcount == lht_results.length)

{

Hashtable[] lht_temp = new Hashtable[lht_results.length + 20];

for (int li_loop = 0; li_loop < lht_results.length ; li_loop++)

{

lht_temp[li_loop] = lht_results[li_loop];

}

lht_results = lht_temp;

}

// 4) loop through our column information and add it to our hash array

Hashtable lht_row = new Hashtable(1);

for ( int i = 1; i <= li_columns; i++)

{

Object luo_value = null;

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 appears

for (int i=0; i<aht_data.length; i++)

{

if (aht_data == null)

{

li_total_rows = i;

break;

}

}

// 2) rebuild a new hashtable array of the right size

// and reload it with your data

if (li_total_rows < aht_data.length)

{ lht_temp = new Hashtable[li_total_rows];

for (int i=0; i<li_total_rows; i++)

{

lht_temp[i] = aht_data[i];

}

aht_data = lht_temp;

lht_temp = null;

}

return aht_data;

}

private Hashtable columnOrder(ResultSetMetaData ameta_data, int ai_columns)

{

// 1) Size the Hashtable to be slighly larger than column count

// and load factor to 1 so the hash table wont have to resize itself.

Hashtable lht_row = new Hashtable((ai_columns + 3),1);

try

{ // 2) Store how many columns we have.

lht_row.put("Column_Count",String.valueOf(ai_columns));

// 3) Loop thru and store each column label and use its position

// number as its key

for ( int i = 1; i <= ai_columns; i++)

{

lht_row.put(String.valueOf(i),ameta_data.getColumnLabel(i));

}

}

catch (SQLException e)

{ // 4 Return a null result if an error happens

lht_row = null;

}

return lht_row;

}

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 then return empty file

ls_tabfile = "";

}

else

{

// 2) first get column headers

int 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 column

String[] ls_indexes = new String[li_column_count];

for(int icol = 0; icol < li_column_count; icol++)

{

ls_indexes[icol] = ahash_data[0].get(String.valueOf(icol+1)).toString();

if(ab_header) ls_tabfile = ls_tabfile + ls_indexes[icol] + " ";

}

// 5) Include the headers in the file if user requested them

if(ab_header) ls_tabfile = ls_tabfile + " ";

// 6) loop through and gather tha data to display

for (int irow=1; irow < ahash_data.length; irow++)

{ if (ahash_data[irow] != null)

{

for(int icol = 0; icol < li_column_count; icol++)

{

ls_tabfile = ls_tabfile + ahash_data[irow].get(ls_indexes[icol]).toString() + " ";

}

ls_tabfile = ls_tabfile + " ";

}

}

}

return ls_tabfile;

}

private String hashToTab(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 return empty file

ls_tabfile = "";

}

else

{

// 2) IF requested print out the header files

if (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 display

for (int i=0; i < ahash_data.length; i++)

{ Hashtable lhash_row = ahash_data[i];

if (lhash_row != null)

{ // 4) Loop thru each column and prints the columns data

for(Enumeration l_enum = lhash_row.keys(); l_enum.hasMoreElements();)

{

String ls_col = l_enum.nextElement().toString() ;

ls_tabfile = ls_tabfile + lhash_row.get(ls_col).toString() + " ";

}

ls_tabfile = ls_tabfile + " ";

}

}

}

return ls_tabfile;

}

(作者:来源:jsp爱好者)

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有