当前位置: 技术问答>java相关
servlet中如何存放、传递、调用数据库中字段类型不同的数据集的问题----及不同类型对象的存取
来源: 互联网 发布时间:2015-05-02
本文导语: 比如我的一个表(AA)里有三个字段,ID,NAME,CONTENT; 字段类型分别为:int,varchar,ntext;----------及字段类型不同; 问: 我想通过一个servlet调用出表AA里的n条数据集,然后在jsp端一条一条纪录的显示出来,该如何处理...
比如我的一个表(AA)里有三个字段,ID,NAME,CONTENT;
字段类型分别为:int,varchar,ntext;----------及字段类型不同;
问:
我想通过一个servlet调用出表AA里的n条数据集,然后在jsp端一条一条纪录的显示出来,该如何处理?及如何将不同类型的多条对象先存放于某个集合,再能一一取出来??
请详细说明最好能举例示意。
----针对相同类型的对象,我采用的是将各字段存入数组,然后将该数组代表的每个记录集通过循环放入Vector;不知此方法是否妥当,应该有更简便通用的方法吧?!
困扰几天了,谢谢了!:)
字段类型分别为:int,varchar,ntext;----------及字段类型不同;
问:
我想通过一个servlet调用出表AA里的n条数据集,然后在jsp端一条一条纪录的显示出来,该如何处理?及如何将不同类型的多条对象先存放于某个集合,再能一一取出来??
请详细说明最好能举例示意。
----针对相同类型的对象,我采用的是将各字段存入数组,然后将该数组代表的每个记录集通过循环放入Vector;不知此方法是否妥当,应该有更简便通用的方法吧?!
困扰几天了,谢谢了!:)
|
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
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