jsp从mysql提取数据_从数据库提取数据通过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 getListWithVOFromRS() {

String sql = "select * from emp";

List rowList = new LinkedList();

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> getListWithListFromRS() {

String sql = "select * from emp";

List> rowList = new LinkedList>();

List colList = null;

try {

getConnectFromOracle();

rs = stat.executeQuery(sql);

int columnCount = rs.getMetaData().getColumnCount();

while (rs.next()) {

colList = new LinkedList();

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

listinlist

${collist}

voinlist

${empvo.ename}${empvo.hiredate}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值