日期:2014-05-17  浏览次数:20675 次

问一个dao工厂外加jsp转发的问题,耐心的好心人帮我看一下吧
第一个文件package util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

public class DBUtil {
public static Connection getConnection() throws Exception {
Connection conn = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager
.getConnection("jdbc:mysql://localhost:3306/jsd1206",
"root", "1031512niu");
} catch (Exception e) {
e.printStackTrace();
}
return conn;
}

public static void close(Connection conn) {
if(conn!=null){
try{
conn.close();
}catch(Exception e1){
e1.printStackTrace();
}
}
}
}
第二个文件
package util;

import java.io.InputStream;
import java.util.Properties;

public class ConfigUtil {
private static Properties pros=new Properties();
static{
ClassLoader loader=ConfigUtil.class.getClassLoader();
InputStream ips=loader.getResourceAsStream("util/daoconfig.properties");
try {
pros.load(ips);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
public static String getValue(String key){
return pros.getProperty(key);
}
}
第三个文件
package util;

import javax.servlet.jsp.jstl.core.Config;

import dao.impl.EmployeeDAOJdbcImpl;

public class DAOFactory {
public static Object getInstance(String type){
Object obj=null;
// 根据接口名找到对应的类名
String className=ConfigUtil.getValue(type);
//使用反射创建实例
try {
obj=Class.forName(className).newInstance();
} catch (Exception e) {
// TODO: handle exception
}
return obj;
}
}
该第四个了,剩下的这两个是关于转发的,和前面的有联系
package web;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import util.DAOFactory;
import dao.EmployeeDao;
import entity.Employee;

public class ListEmpServlet extends HttpServlet {
public void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");


try {
EmployeeDao dao=(EmployeeDao)DAOFactory.getInstance("EmployeeDao");
// EmployeeDAOJdbcImpl dao=new EmployeeDAOJdbcImpl();
List<Employee> employees=dao.findAll();
//调用jsp!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
// 绑定
request.setAttribute("employees",employees);
// 获得转发器
RequestDispatcher rd=request.getRequestDispatcher("employee_list.jsp");
// 转发
rd.forward(request, response);

}

catch (Exception e) {
// TODO: handle exception
e.printStackTrace();

}

}
}
第五个
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@page import="entity.Employee