標籤:
從資料庫提取資料通過jstl顯示在jsp頁面上
1.ConnectDB.java串連資料庫,把資料轉換成list
public class ConnectDB {
private final static String strDriver = "oracle.jdbc.driver.OracleDriver";
private final static String strConnect = "jdbc:oracle:thin:@localhost:1521:ORCL";
private final static String strDBUserName = "scott";
private final static String strDBPWD = "tiger";
private Connection conn = null;
private Statement stat = null;
private ResultSet rs = null;
//串連oracle資料庫
private void getConnectFromOracle() throws ClassNotFoundException,
SQLException {
Class.forName(strDriver);// 執行個體化oracle.jdbc.driver.OracleDriver
conn = DriverManager.getConnection(strConnect, strDBUserName, strDBPWD);
stat = conn.createStatement();
}
//記錄基每行資料存放到值對象,然後把值對象存放一個鏈表
public List<EMPVO> getListWithVOFromRS() {
String sql = "select * from emp";
List<EMPVO> rowList = new LinkedList<EMPVO>();
EMPVO emp = null;
try {
getConnectFromOracle();
rs = stat.executeQuery(sql);
while (rs.next()) {
emp = new EMPVO();
// 為了簡單僅取2個值
emp.setEname(rs.getString("ename"));
emp.setHiredate(rs.getString("hiredate"));
rowList.add(emp);
}
closeConnection();
} catch (ClassNotFoundException e) {
} catch (SQLException e) {
}
return rowList;
}
//記錄基每行資料存放到1個鏈表,然後把這個鏈表存放另一個鏈表
public List<List<String>> getListWithListFromRS() {
String sql = "select * from emp";
List<List<String>> rowList = new LinkedList<List<String>>();
List<String> colList = null;
try {
getConnectFromOracle();
rs = stat.executeQuery(sql);
int columnCount = rs.getMetaData().getColumnCount();
while (rs.next()) {
colList = new LinkedList<String>();
for (int i = 1; i <= columnCount; i++) {
colList.add(rs.getString(i));
}
rowList.add(colList);
}
closeConnection();
} catch (ClassNotFoundException e) {
} catch (SQLException e) {
}
return rowList;
}
//關閉資料庫
public void closeConnection() throws SQLException {
if (rs != null)
rs.close();
if (stat != null)
stat.close();
if (conn != null)
conn.close();
}
}
2.EMPVO.java emp表的值對象
public class EMPVO {
private String empno;
private String ename;
private String job;
private String mgr;
private String hiredate;
private String sal;
private String comm;
private String deptno;
public String getComm() {
return comm;
}
public void setComm(String comm) {
this.comm = comm;
}
public String getDeptno() {
return deptno;
}
public void setDeptno(String deptno) {
this.deptno = deptno;
}
public String getEmpno() {
return empno;
}
public void setEmpno(String empno) {
this.empno = empno;
}
public String getEname() {
return ename;
}
public void setEname(String ename) {
this.ename = ename;
}
public String getHiredate() {
return hiredate;
}
public void setHiredate(String hiredate) {
this.hiredate = hiredate;
}
public String getJob() {
return job;
}
public void setJob(String job) {
this.job = job;
}
public String getMgr() {
return mgr;
}
public void setMgr(String mgr) {
this.mgr = mgr;
}
public String getSal() {
return sal;
}
public void setSal(String sal) {
this.sal = sal;
}
}
3.JstlServlet 調用ConnectDB,把List放入request對象,跳轉到jstl.jsp
public class JstlServlet extends HttpServlet {
private static final long serialVersionUID = 7129164252442979467L;
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
ConnectDB db = new ConnectDB();
request.setAttribute("listinlist", db.getListWithListFromRS());
request.setAttribute("voinlist", db.getListWithVOFromRS());
request.getRequestDispatcher("jstl.jsp").forward(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
4.jstl.jsp
<%@ page language="java" pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<body>
<table>
<tr><h1>listinlist</h1></tr>
<c:forEach items="${listinlist}" var="rowlist">
<tr>
<c:forEach items="${rowlist}" var="collist">
<td>${collist}</td>
</c:forEach>
</tr>
</c:forEach>
</table>
<table>
<tr><h1>voinlist</h1></tr>
<c:forEach items="${voinlist}" var="empvo">
<tr>
<td>${empvo.ename}</td><td>${empvo.hiredate}</td>
</tr>
</c:forEach>
</table>
</body>
</html>
從資料庫提取資料通過jstl顯示在jsp頁面上