Extracting data from a database is displayed on the JSP page by Jstl
1.connectdb.java connect to the database and convert the data into a 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;
Connecting to an Oracle database
private void Getconnectfromoracle () throws ClassNotFoundException,
SQLException {
Class.forName (strdriver);//instantiation of Oracle.jdbc.driver.OracleDriver
conn = Drivermanager.getconnection (StrConnect, Strdbusername, strdbpwd);
Stat = Conn.createstatement ();
}
Record base each row of data is stored to a value object, and then the value object is stored in a linked list
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 ();
To simply take 2 values
Emp.setename (rs.getstring ("ename"));
Emp.sethiredate (rs.getstring ("HireDate"));
Rowlist.add (EMP);
}
CloseConnection ();
} catch (ClassNotFoundException e) {
} catch (SQLException e) {
}
return rowlist;
}
Record base each row of data to 1 linked lists, and then put this list of another linked list
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;
}
Close the database
public void CloseConnection () throws SQLException {
if (rs! = null)
Rs.close ();
if (stat! = null)
Stat.close ();
IF (conn! = null)
Conn.close ();
}
}
2.empvo.java the value object of the EMP table
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 call Connectdb, put list into request object, jump to 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" >
<body>
<table>
<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><c:foreach items= "${voinlist}" var= "EMPVO" >
<tr>
<td>${empvo.ename}</td><td>${empvo.hiredate}</td>
</tr>
</c:forEach>
</table>
</body>
Extract data from the database is displayed on the JSP page by Jstl