我用以下方法返回数据库信息,但不知如何在JSP页面中使用,把所有记录显示在JSP页面上。清指教,谢!!!
public static ArrayList excuteQuery(String SQLClause) throws SQLException {
Connection connection = DBConnectionManager.getConnection();
Statement statement = connection.createStatement();
ArrayList arrayList = new ArrayList();
SQLException exception = null;
ResultSet resultSet = null; try {
resultSet = statement.executeQuery(SQLClause);
ResultSetMetaData metaData = resultSet.getMetaData();
int columnCount = metaData.getColumnCount(); while (resultSet.next()) {
Hashtable dataTable = new Hashtable();
for (int i = 1; i <= columnCount; i++) {
Object columnData = resultSet.getObject(i);
if (!(columnData == null))
dataTable.put(metaData.getColumnName(i), columnData);
}
arrayList.add(dataTable);
}
} catch (SQLException e) {
exception = e;
} finally {
if (resultSet != null)
resultSet.close();
statement.close();
connection.close();
if (exception != null)
throw exception;
}
return arrayList;
}

解决方案 »

  1.   

    List list = YourClass.executeQuery(....);         //YourClass: 你的类的名称
    if (list !=null){
       for (int i=0; i<list.size(); i++){
           HashTable has = (HashTable)list.get(i);    //一条记录
           Enumeration enu = has.keys();              //所有字段的名称
           if (enu.hasMoreElements()){
              String key = (String)enu.nextElements();
              Object obj = has.get(key);              //记录中的一个字段的值
           }
       }
    }