servlet中如何存放、传递、调用数据库中字段类型不同的数据集的问题----及不同类型对象的存取

cleverfish 2002-04-03 09:23:19
比如我的一个表(AA)里有三个字段,ID,NAME,CONTENT;
字段类型分别为:int,varchar,ntext;----------及字段类型不同;
问:
我想通过一个servlet调用出表AA里的n条数据集,然后在jsp端一条一条纪录的显示出来,该如何处理?及如何将不同类型的多条对象先存放于某个集合,再能一一取出来??
请详细说明最好能举例示意。

----针对相同类型的对象,我采用的是将各字段存入数组,然后将该数组代表的每个记录集通过循环放入Vector;不知此方法是否妥当,应该有更简便通用的方法吧?!

困扰几天了,谢谢了!:)
...全文
26 点赞 收藏 11
写回复
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
cleverfish 2002-04-08
谢谢各位,尤其是:pengji(彭乃超),我也找到了一些方法。结账!
回复
chrischen79 2002-04-05
楼上的代码我粗粗的看了一下,重点看一下public Vector select(String strQuery)这个method就可以了。
回复
pengji 2002-04-05
package com.hzs.order;



import java.io.IOException;


import java.sql.*;


import java.util.Vector;


import java.util.*;



public class DBHandle {


// Field


private Vector result = new Vector();


private Connection conn = null;



// Construct


public DBHandle() {


}



// Connect to database


public void connect() throws DBException, IOException {


try {


Class.forName("oracle.jdbc.driver.OracleDriver");


conn = DriverManager.getConnection("jdbc:oracle:thin:system/828112@192.9.200.50:1521:mydb");


} catch(ClassNotFoundException E) {


throw new DBException("Connnect: " + String.valueOf(E.getMessage()));


} catch(SQLException E) {


throw new DBException("SQLException: " + String.valueOf(E.getMessage()));


} catch(Exception E) {


throw new DBException("Exception: " + String.valueOf(E.getMessage()));


}


}



public void transaction() throws DBException, IOException {


try {


conn.setAutoCommit(false);


} catch(SQLException E) {


throw new DBException("SQLException: " + String.valueOf(E.getMessage()));


} catch(Exception E) {


throw new DBException("Exception: " + String.valueOf(E.getMessage()));


}


}



// Executes a SQL statement that returns a single ResultSet.


public Vector select(String strQuery) throws DBException, IOException {


result = new Vector();



try {


Statement stmt = conn.createStatement();


ResultSet rs = stmt.executeQuery(strQuery);


ResultSetMetaData rsmd = rs.getMetaData();


int FieldNum = rsmd.getColumnCount();



while (rs.next()) {


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


result.addElement(rs.getString(i));


}



rs.close();


stmt.close();


} catch(SQLException E) {


throw new DBException("SQLException: " + String.valueOf(E.getMessage()));


} catch(Exception E) {


throw new DBException("Exception: " + String.valueOf(E.getMessage()));


}


return result;


}



// Executes an SQL INSERT, UPDATE or DELETE statement.


public int update(String strUpdate) throws DBException, IOException {


int stmtInt=-1;


try {


Statement stmt = conn.createStatement();


stmtInt = stmt.executeUpdate(strUpdate);


stmt.close();


} catch(SQLException E) {


throw new DBException("SQLException: " + String.valueOf(E.getMessage()));


} catch(Exception E) {


throw new DBException("Exception: " + String.valueOf(E.getMessage()));


}


return stmtInt;


}



// commit


public void commit() throws DBException, IOException {


try {


conn.commit();


} catch(SQLException E) {


throw new DBException("SQLException: " + String.valueOf(E.getMessage()));


} catch(Exception E) {


throw new DBException("Exception: " + String.valueOf(E.getMessage()));


}


}



// rollback


public void rollback() throws DBException, IOException {


try {


conn.rollback();


} catch(SQLException E) {


throw new DBException("SQLException: " + String.valueOf(E.getMessage()));


} catch(Exception E) {


throw new DBException("Exception: " + String.valueOf(E.getMessage()));


}


}



// close


public void close() throws DBException, IOException {


try {


conn.close();


} catch(SQLException E) {


throw new DBException("SQLException: " + String.valueOf(E.getMessage()));


} catch(Exception E) {


throw new DBException("Exception: " + String.valueOf(E.getMessage()));


}


}



public static String toChinese(String strValue) {


try {


if (strValue == null)


return null;


else


return new String(strValue.getBytes("8859_1"), "gb2312");


} catch(Exception e) {


return null;


}


}



public static float toFloat(String str){


return Float.parseFloat(str);


}


}






package com.hzs.order;



public class DBException extends Exception {


public DBException(String desc) {


super(desc);


}


}

回复
cleverfish 2002-04-04
哪位大虾给个示例好吗??
回复
cleverfish 2002-04-03
to pengji(彭乃超):谢谢提醒!这方法是不是就是所谓的UserBean?!

另,和用hastable的区别、利弊是什么?
回复
cleverfish 2002-04-03
chenyuan_tongji(chenyuan_tongji) 的回答较贴近我问的意思,

还有没有详细点的,需要补充的?继续!
回复
pengji 2002-04-03
可以用VECTOR,也可以自己做一个类里面方一条记录,并设置相应的GET,SET方法,当从数据库中取出几条记录就生成几个实例,最后放如一个VECTOR也可以的!
回复
chrischen79 2002-04-03
如果是为了输出在页面上,则可以使用hashtable,用数据库表的filed name做key,相应的值用getString取出来作为value。
回复
zjp009 2002-04-03
比如一个RECORD中有int,varchar,ntext,则:
...
while( oRst.next() )
{
iInt = oRst.getInt("...");
str1 = oRst.getString("...");
str2 = oRst.getString("...");
...
}

对于使用VECTOR来存储是可以的。
回复
cleverfish 2002-04-03
谁能提供一个简单、完整的示例吗!!!!谢谢!谢谢!
回复
pengji 2002-04-03
优点数据封装更清晰,一条记录是一个CLASS实例,缺点是效率比较底!
回复
发动态
发帖子
Web 开发
创建于2007-09-28

7.9w+

社区成员

Java Web 开发
申请成为版主
社区公告
暂无公告