DBConnection.java
package util; import java.sql.*; public class DBConnection { @SuppressWarnings("unused") public static void main(String[] args) { String url = "jdbc:oracle:thin:@localhost:1521:orcl"; String user = "scott"; String password = "tiger"; Connection con; Statement stmt; try { Class.forName("oracle.jdbc.driver.OracleDriver"); con = DriverManager.getConnection(url, user, password); stmt = con.createStatement(); con.close(); System.out.println("connect-ok!@!!"); } catch (Exception e) { // TODO: handle exception System.out.println(e.getMessage()); } } }
EmpDao.java
package dao; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import javax.servlet.jsp.jstl.sql.Result; //import util.ConnectionUtil; import vo.EmpVO; public class EmpDao { Connection conn =null; PreparedStatement pstmt = null; Result rs = null; /** public EmpDao() throws SQLException{ conn = ConnectionUtil.getConnection(); }**/ public ArrayList<EmpVO> selectEmp() throws SQLException{ ArrayList<EmpVO> list = new ArrayList<EmpVO>(); StringBuffer sql = new StringBuffer(); sql.append("select empo,ename,job,mgr,hirdate,sal,comm,deptno from emp"); PreparedStatement pstmt = conn.prepareStatement(sql.toString()); ResultSet rs = pstmt.executeQuery(); while(rs.next()){ EmpVO vo = new EmpVO(); vo.setEmpno(rs.getInt("empno")); vo.setEname(rs.getString("ename") ); vo.setJob(rs.getString("job")); vo.setMgr(rs.getInt("mgr")); vo.setHiredate(rs.getString("hiredate")); vo.setSal(rs.getFloat("sal")); vo.setComm(rs.getFloat("comm")); vo.setDeptno(rs.getInt("deptno")); list.add(vo); } if(rs!=null) rs.close(); if(pstmt !=null) pstmt.close(); if(conn !=null) conn.close(); return list; } }
EmpVO.java
package vo; public class EmpVO { private int empno,mgr,deptno; private String ename,job,hiredate; private float sal,comm; public int getEmpno() { return empno; } public void setEmpno(int empno) { this.empno = empno; } public int getMgr() { return mgr; } public void setMgr(int mgr) { this.mgr = mgr; } public int getDeptno() { return deptno; } public void setDeptno(int deptno) { this.deptno = deptno; } 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 String getHiredate() { return hiredate; } public void setHiredate(String hiredate) { this.hiredate = hiredate; } public float getSal() { return sal; } public void setSal(float sal) { this.sal = sal; } public float getComm() { return comm; } public void setComm(float comm) { this.comm = comm; } }
listServlet.java
package servletUtil; import java.io.IOException; import java.util.ArrayList; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import vo.EmpVO; /** * Servlet implementation class listServlet */ @WebServlet("/listServlet") public class listServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public listServlet() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub ArrayList<EmpVO> list = new ArrayList<EmpVO>(); try { request.setAttribute("list", list); request.getRequestDispatcher("/WEB-INF/empList.jsp").forward(request, response); System.out.println("link!!!!"); } catch (Exception e) { throw new ServletException("Cannot obtain products from DB",e); // TODO: handle exception } } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub } }
empList.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="EUC-KR"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <table border = "1"> <tr > <c:forEach items="${list}" var="list"> <td>사원번호${list.empno}</td> <td>이름${list.ename}</td> <td>입사일자${list.hiredate}</td> <td>급여${list.sal}</td> </c:forEach> </tr> </table> </body> </html>
EmpDao.java中这段注释了conn没有初始化。
18 | /** public EmpDao() throws SQLException{ |
19 | conn = ConnectionUtil.getConnection(); |
20 |
21 | }**/ |
没获取数据怎么显示
EmpDao e = new EmpDao();
list = e.selectEmp();
######我看不到创新版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。