Hashtable ht=new Hashtable;
ResultSet rs=statement.executeQuery(String sql);
int columnCount = rsmd.getColumnCount();
for (int i = 1; i <= columnCount; i++)
{
Object obj=rs.getObject(i);
ht.put(i,new String(obj));
}
ResultSet rs=statement.executeQuery(String sql);
int columnCount = rsmd.getColumnCount();
for (int i = 1; i <= columnCount; i++)
{
Object obj=rs.getObject(i);
ht.put(i,new String(obj));
}
import java.sql.*;public class User
{
private Connection con=null;
//===========创建用户号码信息内类=============
class UserInfo
{
private int id;
private String phone;
public void setId(int id)
{
this.id=id;
}
public int getId()
{
return this.id;
}
public void setName(String phone)
{
this.phone=phone;
}
public String getName()
{
return this.phone;
}
}
//===========================================
public Collection getUserData()throws Exception
{
Collection userData=new ArrayList();
con=Conn.getCon(); //调用数据库连接类连接指定服务器的方法
Statement stmt=con.createStatement();
ResultSet rec=stmt.executeQuery("select top 10 phone from tt333");
while(rec.next())
{
UserInfo userInfo=new UserInfo();
userInfo.setId(rec.getInt("id"));
userInfo.setName(rec.getString("phone"));
userData.add(userInfo);
}
rec.close();
stmt.close();
con.close();
//========返回Collection ===============
return userData;
}
}
HashTable 可以说是一维的,但是 ResultSet 是二维的啊!
如果是为了 Servialize,建议你根据数据字段定义一个 class 或者 Object[],再把这东西放在一个 Vector 中去。