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

原创 2001年05月29日 15:49:00

 

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

// * 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爱好者)

Java 哈希表例子

import java.util.*; /** * * Title: 哈希表操作 * * * Description: 这是一个权限认证的例子,使用了哈希表作为数据的存储 * ...
  • ilikeprograming
  • ilikeprograming
  • 2013年11月03日 09:13
  • 3196

Hashtables(哈希表)

标签: 杂谈  Hashtables(哈希表)在计算机领域中已不是一个新概念了。它们是用来加快计算机的处理速度的,用当今的标准来处理,速度非常慢,而它们可以让你在查询许...
  • hongtaq
  • hongtaq
  • 2012年09月22日 21:33
  • 289

最近学习Lucene,在别人基础上,做了一个小例子

最近学习Lucene,在别人基础上,做了一个小例子 ,以便共同学习!import java.io.InputStream;import lia.handlingtypes.framework.Docu...
  • pengchua
  • pengchua
  • 2007年06月21日 16:32
  • 1995

MySQL之——日期时间Extract函数介绍

MySQL日期时间Extract函数的优点在于可以选取日期时间的各个部分,从年一直到微秒,让我们对MySQL日期时间的处理更为轻松。 MySQL 日期时间 Extract(选取)函数。 1. 选取日期...
  • l1028386804
  • l1028386804
  • 2016年04月14日 21:58
  • 1254

Oracle日期处理——Extract()函数

1.2个日期相差的天数: --相减得到相差的天数 select to_date('2012-10-18 15:29:01', 'yyyy-mm-dd hh24:mi:ss') - ...
  • qincidong
  • qincidong
  • 2012年12月26日 20:05
  • 1892

<Android 基础(八)> Palette

Palette,颜色萃取
  • poorkick
  • poorkick
  • 2016年06月26日 15:52
  • 808

java中哈希表及其应用详解

http://blog.csdn.net/u010297957/article/details/51974340 什么是哈希表 数组和向量都可以存储对象,但对象的存储位置是随机的,也就是说对象...
  • xiaoxik
  • xiaoxik
  • 2017年07月10日 15:52
  • 621

集成环境搭建及遇到的问题 jetty8+hudson2.2.0+sonar2.11+mysql5.5.1

一、概述 笔者将详细介绍集成环境搭建及需要注意的事项。此些点都摸索了许多天完成的。一些问题是在搭建过程中实际遇到过的。当然大多数都是环境配置的问题。如果遇到集成环境的一些问题,欢迎和笔者联系。笔...
  • bxyz1203
  • bxyz1203
  • 2011年11月17日 11:01
  • 4094

使用mybatis 操作数据库的实例程序

MyBatis 是支持普通 SQL 查询,存储过程和高级映射的优秀持久层框架。 MyBatis 消除了几乎所有的 JDBC 代码和参数的手工设置以及对结果集的检索。...
  • huan_chen
  • huan_chen
  • 2017年05月12日 21:03
  • 797

Appium使用之helloworld

使用 python3 利用 Appium 进行 android 自动化测试
  • zxz_tsgx
  • zxz_tsgx
  • 2016年11月17日 18:17
  • 2751
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:JAVA 使用哈希表操作数据库的例子 Using Hashtables to Store & Extract results from a Database.
举报原因:
原因补充:

(最多只允许输入30个字)