蛋痛菜鸟,做了(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());
}
}
}
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;
}
}
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;
}
}
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
}
}
[code=text]empList.jsp[/code][/code] mvc list
[code=html]<%@ 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>
package util;
import java.sql.*;
public class DBConnection {//修改了该类
private static Connection conn = null;
public static Connection getConn(){
String url = "jdbc:oracle:thin:@localhost:1521:orcl";
String user = "scott";
String password = "tiger";
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
conn = DriverManager.getConnection(url, user, password);
System.out.println("connect-ok!@!!");
} catch (Exception e) {
// TODO: handle exception
System.out.println(e.getMessage());
}
return conn;
}
}
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 util.DBConnection;
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();
conn = DBConnection.getConn();//增加了此行;
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;
}
}
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 dao.EmpDao;
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>();
list = new EmpDao().selectEmp();//增加了此行
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
}
}