⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 0172.htm

📁 JspServlet教程专栏 对javaservlet讲述的非常详细
💻 HTM
📖 第 1 页 / 共 2 页
字号:
<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>//&nbsp; ********************************************************************&nbsp; <br>
//&nbsp; *&nbsp; This&nbsp; code&nbsp; is&nbsp; distributed&nbsp; in&nbsp; the&nbsp; hope&nbsp; that&nbsp; it&nbsp; will&nbsp; be&nbsp; useful,&nbsp; *<br>
//&nbsp; *&nbsp; but&nbsp; WITHOUT&nbsp; ANY&nbsp; WARRANTY;&nbsp; without&nbsp; even&nbsp; the&nbsp; implied&nbsp; warranty&nbsp; of&nbsp; *<br>
//&nbsp; *&nbsp; MERCHANTABILITY&nbsp; or&nbsp; FITNESS&nbsp; FOR&nbsp; A&nbsp; PARTICULAR&nbsp; PURPOSE.&nbsp; *<br>
//&nbsp; ********************************************************************<br>
//<br>
//&nbsp; Using&nbsp; Hashtables&nbsp; to&nbsp; Store&nbsp; &&nbsp; Extract&nbsp; results&nbsp; from&nbsp; a&nbsp; Database.<br>
//&nbsp; <br>
//&nbsp; These&nbsp; functions&nbsp; are&nbsp; an&nbsp; example&nbsp; on&nbsp; how&nbsp; to&nbsp; get&nbsp; the&nbsp; data&nbsp; out&nbsp; of&nbsp; a<br>
//&nbsp; resultset&nbsp; from&nbsp; a&nbsp; database&nbsp; and&nbsp; store&nbsp; the&nbsp; values&nbsp; in&nbsp; a&nbsp; Hashtable.<br>
//&nbsp; several&nbsp; of&nbsp; the&nbsp; function&nbsp; are&nbsp; then&nbsp; an&nbsp; example&nbsp; on&nbsp; how&nbsp; to&nbsp; get&nbsp; the&nbsp; data<br>
//&nbsp; out&nbsp; of&nbsp; the&nbsp; hashtables.&nbsp; Why&nbsp; would&nbsp; you&nbsp; want&nbsp; to&nbsp; do&nbsp; all&nbsp; this&nbsp; work?<br>
//&nbsp; Maintaining&nbsp; a&nbsp; database&nbsp; connection&nbsp; over&nbsp; the&nbsp; web&nbsp; is&nbsp; expensive.&nbsp; By<br>
//&nbsp; dumping&nbsp; the&nbsp; data&nbsp; into&nbsp; a&nbsp; hashtable&nbsp; you&nbsp; can&nbsp; minimize&nbsp; the&nbsp; amount&nbsp; of<br>
//&nbsp; time&nbsp; you&nbsp; stay&nbsp; connected&nbsp; to&nbsp; your&nbsp; database.&nbsp; Also&nbsp; by&nbsp; storing&nbsp; the&nbsp; data<br>
//&nbsp; in&nbsp; a&nbsp; hashtable&nbsp; offers&nbsp; flexible&nbsp; way&nbsp; to&nbsp; pass&nbsp; your&nbsp; data&nbsp; from&nbsp; <br>
//&nbsp; object&nbsp; to&nbsp; object.<br>
//&nbsp; <br>
//&nbsp; This&nbsp; is&nbsp; a&nbsp; set&nbsp; of&nbsp; five&nbsp; functions<br>
//<br>
//&nbsp; Function&nbsp; makeHashTable&nbsp; :&nbsp; <br>
//&nbsp; Takes&nbsp; a&nbsp; database&nbsp; ResultSet&nbsp; and&nbsp; places&nbsp; the&nbsp; data&nbsp; into&nbsp; a&nbsp; <br>
//&nbsp; Hashtable&nbsp; array&nbsp; for&nbsp; later&nbsp; use.<br>
//<br>
//&nbsp; Function&nbsp; cleanHashTable&nbsp; :&nbsp; <br>
//&nbsp; Takes&nbsp; a&nbsp; Hashtable&nbsp; array&nbsp; and&nbsp; removes&nbsp; the&nbsp; unused&nbsp; portion&nbsp; <br>
//&nbsp; of&nbsp; a&nbsp; hashtable&nbsp; array.&nbsp; For&nbsp; example:&nbsp; You&nbsp; use&nbsp; makeHashTable&nbsp; <br>
//&nbsp; and&nbsp; since&nbsp; it&nbsp; allocates&nbsp; the&nbsp; hashtable&nbsp; array&nbsp; in&nbsp; chunks&nbsp; of&nbsp; 20,<br>
//&nbsp; its&nbsp; possible&nbsp; that&nbsp; it&nbsp; creates&nbsp; a&nbsp; hashtable&nbsp; of&nbsp; size&nbsp; 40,&nbsp; but&nbsp; <br>
//&nbsp; only&nbsp; the&nbsp; first&nbsp; 22&nbsp; indexes&nbsp; are&nbsp; used.&nbsp; So&nbsp; makeHashTable&nbsp; calls&nbsp; <br>
//&nbsp; the&nbsp; cleanHashTable&nbsp; function&nbsp; which&nbsp; resizes&nbsp; the&nbsp; Hashtable&nbsp; <br>
//&nbsp; array&nbsp; to&nbsp; only&nbsp; 22&nbsp; indexes.<br>
//<br>
//Function&nbsp; columnOrder:<br>
//&nbsp; Since&nbsp; a&nbsp; Hashtable&nbsp; does&nbsp; not&nbsp; guarantee&nbsp; to&nbsp; maintain&nbsp; the&nbsp; order<br>
//&nbsp; of&nbsp; the&nbsp; elements&nbsp; put&nbsp; into&nbsp; it.&nbsp; This&nbsp; function&nbsp; produces&nbsp; a<br>
//&nbsp; hashtable&nbsp; to&nbsp; store&nbsp; the&nbsp; column&nbsp; order&nbsp; of&nbsp; the&nbsp; ResultSet<br>
//<br>
//Function&nbsp; hastToTabFile<br>
//&nbsp; An&nbsp; example&nbsp; on&nbsp; how&nbsp; to&nbsp; take&nbsp; a&nbsp; hashtable&nbsp; produced&nbsp; by&nbsp; the<br>
//&nbsp; makeHashTable&nbsp; function&nbsp; and&nbsp; turn&nbsp; it&nbsp; into&nbsp; a&nbsp; tab&nbsp; delimited<br>
//&nbsp; output&nbsp; string&nbsp; (used&nbsp; to&nbsp; download&nbsp; a&nbsp; dataresult&nbsp; as&nbsp; a&nbsp; flatfile)<br>
//&nbsp; This&nbsp; function&nbsp; uses&nbsp; a&nbsp; the&nbsp; results&nbsp; from&nbsp; the&nbsp; columnOrder<br>
//&nbsp; function&nbsp; to&nbsp; navigate&nbsp; the&nbsp; hashtable.&nbsp; If&nbsp; this&nbsp; function&nbsp; can't&nbsp; <br>
//&nbsp; find&nbsp; this&nbsp; index&nbsp; hashtable,&nbsp; it&nbsp; then&nbsp; passes&nbsp; the&nbsp; hashtable<br>
//&nbsp; to&nbsp; the&nbsp; hashToTab&nbsp; Function&nbsp; to&nbsp; step&nbsp; through&nbsp; the&nbsp; hashtable&nbsp; using<br>
//&nbsp; enumeration&nbsp; methods.<br>
//<br>
//Function&nbsp; hashToTab<br>
//&nbsp; If&nbsp; no&nbsp; index&nbsp; hasharray&nbsp; was&nbsp; found&nbsp; then&nbsp; this&nbsp; function&nbsp; uses<br>
//&nbsp; Enumeration&nbsp; to&nbsp; step&nbsp; through&nbsp; the&nbsp; Hashtable&nbsp; and&nbsp; return&nbsp; a&nbsp; <br>
//&nbsp; result<br>
//<br>
//////////////////////////////////////////////////////////////////////////<br>
//<br>
//&nbsp; Please&nbsp; note&nbsp; the&nbsp; following.<br>
//&nbsp; -I&nbsp; suspect&nbsp; using&nbsp; a&nbsp; Vector&nbsp; would&nbsp; give&nbsp; much&nbsp; faster&nbsp; results&nbsp; .<br>
//&nbsp; -If&nbsp; you&nbsp; are&nbsp; using&nbsp; Java&nbsp; 1.2&nbsp; You&nbsp; should&nbsp; consider&nbsp; using&nbsp; an&nbsp; ArrayList,<br>
//&nbsp; HashSet&nbsp; or&nbsp; TreeSet&nbsp; rather&nbsp; than&nbsp; a&nbsp; Hashtable&nbsp; or&nbsp; a&nbsp; Vector.<br>
//&nbsp; -Use&nbsp; a&nbsp; Hashtable&nbsp; or&nbsp; Vector&nbsp; when&nbsp; you&nbsp; want&nbsp; java&nbsp; 1.1.x&nbsp; compatibility&nbsp; <br>
//<br>
//////////////////////////////////////////////////////////////////////////<br>
<br>
public&nbsp; Hashtable[]&nbsp; makeHashTable(ResultSet&nbsp; ars_data)<br>
{<br>
int&nbsp; li_columns&nbsp; =&nbsp; 0;<br>
int&nbsp; li_rowcount&nbsp; =&nbsp; 0;<br>
<br>
Hashtable[]&nbsp; lht_results&nbsp; =&nbsp; new&nbsp; Hashtable[20];<br>
<br>
<br>
try<br>
{&nbsp; //&nbsp; 1)get&nbsp; the&nbsp; column&nbsp; count&nbsp; and&nbsp; store&nbsp; our&nbsp; column&nbsp; order&nbsp; information<br>
//&nbsp; in&nbsp; our&nbsp; first&nbsp; index&nbsp; of&nbsp; our&nbsp; Hashtable&nbsp; array<br>
ResultSetMetaData&nbsp; lmeta_data&nbsp; =&nbsp; ars_data.getMetaData();<br>
<br>
li_columns&nbsp; =&nbsp; lmeta_data.getColumnCount();<br>
<br>
if&nbsp; (li_columns&nbsp; >&nbsp; 0)<br>
{&nbsp; lht_results[li_rowcount]&nbsp; =&nbsp; columnOrder(lmeta_data,li_columns);<br>
li_rowcount++;<br>
}<br>
<br>
<br>
//&nbsp; 2)loop&nbsp; through&nbsp; the&nbsp; result&nbsp; set&nbsp; and&nbsp; add&nbsp; the&nbsp; data&nbsp; 1&nbsp; row&nbsp; at&nbsp; a&nbsp; time&nbsp; to<br>
//&nbsp; the&nbsp; hashtable&nbsp; array<br>
while&nbsp; (ars_data.next())<br>
{<br>
//&nbsp; 3)&nbsp; If&nbsp; we&nbsp; are&nbsp; at&nbsp; the&nbsp; last&nbsp; index&nbsp; of&nbsp; our&nbsp; hashtable&nbsp; then&nbsp; expand&nbsp; it<br>
//&nbsp; by&nbsp; another&nbsp; 20&nbsp; indexes<br>
if&nbsp; (li_rowcount&nbsp; ==&nbsp; lht_results.length)<br>
{<br>
Hashtable[]&nbsp; lht_temp&nbsp; =&nbsp; new&nbsp; Hashtable[lht_results.length&nbsp; +&nbsp; 20];<br>
for&nbsp; (int&nbsp; li_loop&nbsp; =&nbsp; 0;&nbsp; li_loop&nbsp; &lt;&nbsp; lht_results.length&nbsp; ;&nbsp; li_loop++)<br>
{<br>
lht_temp[li_loop]&nbsp; =&nbsp; lht_results[li_loop];<br>
}<br>
lht_results&nbsp; =&nbsp; lht_temp;<br>
}<br>
<br>
//&nbsp; 4)&nbsp; loop&nbsp; through&nbsp; our&nbsp; column&nbsp; information&nbsp; and&nbsp; add&nbsp; it&nbsp; to&nbsp; our&nbsp; hash&nbsp; array<br>
Hashtable&nbsp; lht_row&nbsp; =&nbsp; new&nbsp; Hashtable(1);<br>
for&nbsp; (&nbsp; int&nbsp; i&nbsp; =&nbsp; 1;&nbsp; i&nbsp; &lt;=&nbsp; li_columns;&nbsp; i++)<br>
{<br>
Object&nbsp; luo_value&nbsp; =&nbsp; null;<br>
try<br>
{<br>
luo_value&nbsp; =&nbsp; ars_data.getObject(i);<br>
}<br>
catch(Exception&nbsp; e){}<br>
if&nbsp; (luo_value&nbsp; ==null)&nbsp; luo_value&nbsp; =&nbsp; new&nbsp; String("");<br>
lht_row.put(lmeta_data.getColumnLabel(i),luo_value);<br>
}<br>
<br>
lht_results[li_rowcount]&nbsp; =&nbsp; lht_row;<br>
li_rowcount++;<br>
<br>
<br>
}<br>
}<br>
catch(SQLException&nbsp; e)<br>
{<br>
}<br>
<br>
<br>
if&nbsp; (lht_results[0]&nbsp; ==&nbsp; null)&nbsp; <br>
{<br>
return&nbsp; null;<br>
}<br>
<br>
<br>
return&nbsp; cleanHashTable(lht_results);<br>
}<br>
<br>
<br>
private&nbsp; Hashtable[]&nbsp; cleanHashTable(Hashtable[]&nbsp; aht_data)<br>
{<br>
Hashtable[]&nbsp; lht_temp&nbsp; =&nbsp; null;<br>
int&nbsp; li_total_rows&nbsp; =&nbsp; aht_data.length;<br>
<br>
//&nbsp; 1)&nbsp; loop&nbsp; thru&nbsp; and&nbsp; determine&nbsp; where&nbsp; the&nbsp; first&nbsp; null&nbsp; row&nbsp; appears<br>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -