📄 0172.htm
字号:
<html>
<head>
<title>新时代软件教程:操作系统 主页制作 服务器 设计软件 网络技术 编程语言 文字编辑</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<style>
<!--
body, table {font-size: 9pt; font-family: 宋体}
a {text-decoration:none}
a:hover {color: red;text-decoration:underline}
.1 {background-color: rgb(245,245,245)}
-->
</style>
</head>
<p align="center"><script src="../../1.js"></script></a>
<p align="center"><big><strong>使用哈希表操作数据库的例子</strong></big></p>
<div align="right">---摘自互联网</div>
<br>// ******************************************************************** <br>
// * This code is distributed in the hope that it will be useful, *<br>
// * but WITHOUT ANY WARRANTY; without even the implied warranty of *<br>
// * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. *<br>
// ********************************************************************<br>
//<br>
// Using Hashtables to Store & Extract results from a Database.<br>
// <br>
// These functions are an example on how to get the data out of a<br>
// resultset from a database and store the values in a Hashtable.<br>
// several of the function are then an example on how to get the data<br>
// out of the hashtables. Why would you want to do all this work?<br>
// Maintaining a database connection over the web is expensive. By<br>
// dumping the data into a hashtable you can minimize the amount of<br>
// time you stay connected to your database. Also by storing the data<br>
// in a hashtable offers flexible way to pass your data from <br>
// object to object.<br>
// <br>
// This is a set of five functions<br>
//<br>
// Function makeHashTable : <br>
// Takes a database ResultSet and places the data into a <br>
// Hashtable array for later use.<br>
//<br>
// Function cleanHashTable : <br>
// Takes a Hashtable array and removes the unused portion <br>
// of a hashtable array. For example: You use makeHashTable <br>
// and since it allocates the hashtable array in chunks of 20,<br>
// its possible that it creates a hashtable of size 40, but <br>
// only the first 22 indexes are used. So makeHashTable calls <br>
// the cleanHashTable function which resizes the Hashtable <br>
// array to only 22 indexes.<br>
//<br>
//Function columnOrder:<br>
// Since a Hashtable does not guarantee to maintain the order<br>
// of the elements put into it. This function produces a<br>
// hashtable to store the column order of the ResultSet<br>
//<br>
//Function hastToTabFile<br>
// An example on how to take a hashtable produced by the<br>
// makeHashTable function and turn it into a tab delimited<br>
// output string (used to download a dataresult as a flatfile)<br>
// This function uses a the results from the columnOrder<br>
// function to navigate the hashtable. If this function can't <br>
// find this index hashtable, it then passes the hashtable<br>
// to the hashToTab Function to step through the hashtable using<br>
// enumeration methods.<br>
//<br>
//Function hashToTab<br>
// If no index hasharray was found then this function uses<br>
// Enumeration to step through the Hashtable and return a <br>
// result<br>
//<br>
//////////////////////////////////////////////////////////////////////////<br>
//<br>
// Please note the following.<br>
// -I suspect using a Vector would give much faster results .<br>
// -If you are using Java 1.2 You should consider using an ArrayList,<br>
// HashSet or TreeSet rather than a Hashtable or a Vector.<br>
// -Use a Hashtable or Vector when you want java 1.1.x compatibility <br>
//<br>
//////////////////////////////////////////////////////////////////////////<br>
<br>
public Hashtable[] makeHashTable(ResultSet ars_data)<br>
{<br>
int li_columns = 0;<br>
int li_rowcount = 0;<br>
<br>
Hashtable[] lht_results = new Hashtable[20];<br>
<br>
<br>
try<br>
{ // 1)get the column count and store our column order information<br>
// in our first index of our Hashtable array<br>
ResultSetMetaData lmeta_data = ars_data.getMetaData();<br>
<br>
li_columns = lmeta_data.getColumnCount();<br>
<br>
if (li_columns > 0)<br>
{ lht_results[li_rowcount] = columnOrder(lmeta_data,li_columns);<br>
li_rowcount++;<br>
}<br>
<br>
<br>
// 2)loop through the result set and add the data 1 row at a time to<br>
// the hashtable array<br>
while (ars_data.next())<br>
{<br>
// 3) If we are at the last index of our hashtable then expand it<br>
// by another 20 indexes<br>
if (li_rowcount == lht_results.length)<br>
{<br>
Hashtable[] lht_temp = new Hashtable[lht_results.length + 20];<br>
for (int li_loop = 0; li_loop < lht_results.length ; li_loop++)<br>
{<br>
lht_temp[li_loop] = lht_results[li_loop];<br>
}<br>
lht_results = lht_temp;<br>
}<br>
<br>
// 4) loop through our column information and add it to our hash array<br>
Hashtable lht_row = new Hashtable(1);<br>
for ( int i = 1; i <= li_columns; i++)<br>
{<br>
Object luo_value = null;<br>
try<br>
{<br>
luo_value = ars_data.getObject(i);<br>
}<br>
catch(Exception e){}<br>
if (luo_value ==null) luo_value = new String("");<br>
lht_row.put(lmeta_data.getColumnLabel(i),luo_value);<br>
}<br>
<br>
lht_results[li_rowcount] = lht_row;<br>
li_rowcount++;<br>
<br>
<br>
}<br>
}<br>
catch(SQLException e)<br>
{<br>
}<br>
<br>
<br>
if (lht_results[0] == null) <br>
{<br>
return null;<br>
}<br>
<br>
<br>
return cleanHashTable(lht_results);<br>
}<br>
<br>
<br>
private Hashtable[] cleanHashTable(Hashtable[] aht_data)<br>
{<br>
Hashtable[] lht_temp = null;<br>
int li_total_rows = aht_data.length;<br>
<br>
// 1) loop thru and determine where the first null row appears<br>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -