這個內容也是根據企業學員的要求准備的
輸入: select ID as 編號
輸出:
編號
姓名
年齡
要求是如果SQL變動
因為現在Hibernate用的比較廣泛
DAO
/**
* 根據查詢語句返回結果
* @param hql
* @param args
* @return
*/
public List queryAllForReport( final String hql
List list = getHibernateTemplate()
public Object doInHibernate(Session session)
throws HibernateException
Query query = session
for(int i =
query
}
List list = query
list
return list;
}
});
// Hibernate做count計算返回一般都是對象
return list;
}
測試代碼:
List list = dao
name as 登錄名
System
for(Object[] row : list) {
for(Object v : row) {
System
}
System
}
最後不得不回到JDBC
﹤%@ page language=
﹤%@ taglib prefix=
﹤!DOCTYPE HTML PUBLIC
﹤html﹥
﹤head﹥
﹤title﹥SQL報表﹤/title﹥
﹤meta http
﹤meta http
﹤meta http
﹤/head﹥
﹤body﹥
﹤form action=
﹤textarea name=sql cols=
﹤input type=submit value=查詢﹥
﹤/form﹥
﹤c:if test=
﹤%
//new oracle
Connection conn = DriverManager
Statement stmt = conn
ResultSet rs = stmt
if(rs == null) {
stmt
conn
return;
}
// 取列名
ResultSetMetaData meta = rs
int cols = meta
ArrayList colNames = new ArrayList();
for(int i =
colNames
}
request
%﹥
﹤table border=
﹤tr﹥
﹤c:forEach items=
﹤td﹥${c}﹤/td﹥
﹤/c:forEach﹥
﹤/tr﹥
﹤%
while(rs
colNames
for(int i =
Object value = rs
System
// TODO 更多格式化控制
if(value instanceof java
value = rs
java
value = df
}
if(value instanceof java
java
value = v
// 要求輸出時候最少顯示兩位小數
java
format
format
value = format
}
colNames
}
request
%﹥
﹤tr﹥
﹤c:forEach items=
﹤td﹥${c}﹤/td﹥
﹤/c:forEach﹥
﹤/tr﹥
﹤%
}
rs
stmt
conn
%﹥
﹤/table﹥
﹤/c:if﹥
﹤/body﹥
﹤/html﹥
用到了JSTL和EL
From:http://tw.wingwit.com/Article/program/Java/JSP/201311/20746.html