您的位置:首页 > 数据库

如何使用小脚本和表达式链接数据库输出emp表的数据

2017-09-13 15:18 197 查看
1、 在项目中配置oracle的驱动jar包

把jar包拷贝到项目中的lib文件夹下

然后把jar包部署在项目上边

2、 创建数据库工具类(提供数据库的链接、关闭链接、查询、更新的方法)DBOperation

3、 对emp表进行封装

新建Emp类  代码如下:public class Emp {
private int empno;
private String ename;
private String job;
private int mgr;
private Date hiredate;
private int sal;
private int comm;
private int deptno;

public int getEmpno() {
return empno;
}
public void setEmpno(int empno) {
this.empno = empno;
}
public String getEname() {
return ename;
}
public void setEname(String ename) {
this.ename = ename;
}
public String getJob() {
return job;
}
public void setJob(String job) {
this.job = job;
}
public int getMgr() {
return mgr;
}
public void setMgr(int mgr) {
this.mgr = mgr;
}
public Date getHiredate() {
return hiredate;
}
public void setHiredate(Date hiredate) {
this.hiredate = hiredate;
}
public int getSal() {
return sal;
}
public void setSal(int sal) {
this.sal = sal;
}
public int getComm() {
return comm;
}
public void setComm(int comm) {
this.comm = comm;
}
public int getDeptno() {
return deptno;
}
public void setDeptno(int deptno) {
this.deptno = deptno;
}

}
4、 新建EmpDao对emp表进行数据库操作

public class EmpDao extends DbOperation{
//查询emp表数据
public ArrayList select(){
//集合对象用于保存查询到的数据
ArrayList al=new ArrayList();
//链接数据库
getConn();
//sql语句
String sql="select * from emp";
//执行查询
extQuery(sql);
//处理结果集
try {
while(rs.next()){
Emp e=new Emp();
e.setEmpno(rs.getInt("empno"));
e.setEname(rs.getString("ename"));
e.setJob(rs.getString("job"));
e.setMgr(rs.getInt("mgr"));
e.setComm(rs.getInt("comm"));
e.setSal(rs.getInt("sal"));
e.setDeptno(rs.getInt("deptno"));
e.setHiredate(rs.getDate("hiredate"));
al.add(e);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//关闭链接
closeAll();
return al;
}
}
5、  新建jsp来显示查询出的数据

<%@page import="myjsp.vo.Emp"%>
<%@page import="myjsp.dao.EmpDao"%>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
//获得emp表数据
EmpDao ed=new EmpDao();
ArrayList al=ed.select();
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'emp.jsp' starting page</title>
</head>
<body>
<table>
<tr>
<td>员工编号</td><td>员工姓名</td>
<td>职位</td><td>上级编号</td>
<td>入职日期</td><td>薪资</td>
<td>奖金</td><td>部门编号</td>
</tr>
<%for(int i=0;i<al.size();i++){
Emp e=(Emp)al.get(i);
out.print("<tr>");
out.print("<td>"+e.getE
4000
mpno()+"</td>");
out.print("<td>"+e.getEname()+"</td>");
out.print("<td>"+e.getJob()+"</td>");
out.print("<td>"+e.getMgr()+"</td>");
out.print("<td>"+e.getHiredate()+"</td>");
out.print("<td>"+e.getSal()+"</td>");
out.print("<td>"+e.getComm()+"</td>");
out.print("<td>"+e.getDeptno()+"</td>");
out.print("</tr>");
}%>
</table>
</body>
</html>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: