开发者社区> 问答> 正文

菜鸟蛋痛几天还是没解决[MVC模式留言板],无法读取显示数据:报错

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>

展开
收起
kun坤 2020-06-06 23:49:56 456 0
1 条回答
写回答
取消 提交回答
  • EmpDao.java中这段注释了conn没有初始化。

    18 /** public EmpDao() throws SQLException{

    19             conn = ConnectionUtil.getConnection();
    20          
    21     }**/
    ######debug就知道了######

    没获取数据怎么显示

    EmpDao  e = new EmpDao();

    list = e.selectEmp();

    ######我看不到创新
    2020-06-06 23:50:03
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
Java Spring Boot开发实战系列课程【第7讲】:Spring Boot 2.0安全机制与MVC身份验证实战(Java面试题) 立即下载
低代码开发师(初级)实战教程 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载