MVC设计模式((javaWEB)在数据库连接池下,实现对数据库中的数据增删改查操作)

设计功能的实现:

----没有业务层,直接由Servlet调用DAO,所以也没有事务操作,所以从DAO中直接获取connection对象

----采用MVC设计模式

----采用到的技术

    。MVC设计模式,JSP,Servlet,POJO

  。数据库使用mysql

  。数据库连接池需要使用C3P0数据库连接池

  。页面上的提示需要使用jQuery

----技术难点

  。多个请求如何使用一个Servlet

  。如何模糊查询

  。如何在创建和修改的情况下,验证用户信息是否已被使用,并给出提示

---------------------------------------------------------------------------------------------------

1.首先数据库连接池C3P0的一些实现需要导五个开源的架包:

c3p0-0.9.1.2.jar

commons-dbcp-1.4.jar

commons-dbutils-1.3.jar

commons-pool-1.5.5.jar

mysql-connector-java-5.1.6-bin.jar

---------------------------------------------------------------------------

2.连接数据库连接池的文件为:在src目录下建立XML文件c3p0-config.xml;

<?xml version="1.0" encoding="UTF-8"?>

<c3p0-config>

    <named-config name="mvcapp">

<!-- 指定连接数据源的基本属性 -->
    <property name="user">root</property>
    <property name="password">lxn123</property>
    <property name="driverClass">com.mysql.jdbc.Driver</property>
    <property name="jdbcUrl">jdbc:mysql:///test</property>

<!-- 若数据库中连接数不足时, 一次向数据库服务器申请多少个连接 -->
    <property name="acquireIncrement">5</property>
    <!-- 初始化数据库连接池时连接的数量 -->
    <property name="initialPoolSize">10</property>
    <!-- 数据库连接池中的最小的数据库连接数 -->
    <property name="minPoolSize">10</property>
    <!-- 数据库连接池中的最大的数据库连接数 -->
    <property name="maxPoolSize">50</property>

    <!-- C3P0 数据库连接池可以维护的 Statement 的个数 -->
    <property name="maxStatements">20</property>
    <!-- 每个连接同时可以使用的 Statement 对象的个数 -->
    <property name="maxStatementsPerConnection">5</property>

  </named-config>

</c3p0-config>

---------------------------------------------------------------------------

3.连接数据库连接池的类:JdbcUtils

public class JdbcUtils {

  //释放connection连接
  public static void releaseConnection(Connection connection){
    try {
      if (connection!=null) {
      connection.close();
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  //该静态代码块只执行一次
  private static ComboPooledDataSource dataSource=null;
    static{
    //通过配置文件实现数据库的连接,只能被创建一次
      dataSource=new ComboPooledDataSource("mvcapp");
      }

    //获取数据库连接,返回数据源的Connection对象
  public static Connection getConnection() throws SQLException{
    return dataSource.getConnection();
  }
}

---------------------------------------------------------------------------

4.建立一个测试类(建立方法类似于class的建立,在src,所在包下建立,建立时找java下的JUnit,建立就行了):JdbcUtilsTest类,对数据库连接池进行测试,看是否连接成功!!!

public class JdbcUtilsTest {
//测试类包,测试数据库是否连接成功
@Test
public void testGetConnection() throws SQLException{
Connection connection=JdbcUtils.getConnection();
System.out.println(connection);
}
}

---------------------------------------------------------------------------

5.java是以面向对象的思想编程的,所以建立customer类,CriteriaCustomer类,对数据库里面的属性进行封装处理,便于后边功能调用的实现

customer类:对数据库里面的属性进行封装处理

public class Customer {
//建立了一个Customer类,对id,name,address,phone这些属性进行封装
private Integer id;
private String name;
private String address;
private String phone;

public Customer() {
super();
}

public Customer( String name, String address, String phone) {

this.name = name;
this.address = address;
this.phone = phone;
}

public Customer(Integer id, String name, String address, String phone) {
super();
this.id = id;
this.name = name;
this.address = address;
this.phone = phone;
}

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getAddress() {
return address;
}

public void setAddress(String address) {
this.address = address;
}

public String getPhone() {
return phone;
}

public void setPhone(String phone) {
this.phone = phone;
}

@Override
public String toString() {
return "Customer [;
}
}

-------------------

CriteriaCustomer类:模糊查询的类

//模糊查询的类
public class CriteriaCustomer {
private String name;
private String address;
private String phone;

public CriteriaCustomer() {
super();
}

public CriteriaCustomer(String name, String address, String phone) {
super();
this.name = name;
this.address = address;
this.phone = phone;
}

public String getName() {
if (name==null) {
name="%%";
}
else{
name="%"+name+"%";
}
return name;
}

public void setName(String name) {
this.name = name;
}

public String getAddress() {
if (address==null) {
address="%%";
}
else{
address="%"+address+"%";
}
return address;
}

public void setAddress(String address) {
this.address = address;
}

public String getPhone() {
if (phone==null) {
phone="%%";
}
else{
phone="%"+phone+"%";
}
return phone;
}

public void setPhone(String phone) {
this.phone = phone;
}

}

---------------------------------------------------------------------------

6.在该包下建立一个接口类(创建时找interface,创建):CustomerDAO,里边封装了一些方法:

(1)模糊查询方法

(2)查询的方法

(3)插入数据

(4)通过jsp超链接里面?后面的id,获取id值的方法

(5)通过jsp超链接里面?后面的id,并且实现删除的方法

(6)查村和该name值相等的个数的方法

(7)修改方法

//创建以CustomerDAO接口,可以实现其他类的调用
public interface CustomerDAO {

//模糊查询方法,通过输入的字符,查看数据表里的数据里,是否包含这些字符,
public List<Customer> getForListWithCriteriaCustomer(CriteriaCustomer cc);

//查询的方法,将数据库的所有数据放在list集合中,并实现输出的查询
public List<Customer> getAll();

//插入数据,在页面上输入数据name,address,phone,就直接插入
public void save(Customer customer);

//通过jsp超链接里面?后面的id,获取该id下的各属性的值
public Customer get(Integer id);

//通过jsp超链接里面?后面的id,并且实现删除的功能
public void delete(Integer id);

//返回和该name相等的个数
public long getCountWithName(String name);

//修改的方法

void update(Customer customer);
}

---------------------------------------------------------------------------

7.建立一个DAO类,封装了基本的增删改查方法,以供子类继承使用; 当前DAO没有事务,直接在方法中获取数据库的链接

(1)类的构造方法,用到了反射,便有后面Servlet类实现*.do的操作

(2)返回数据库里面一条记录的值:比如返回某一条记录的customerName,或返回有多少条记录

(3)返回所对应的list集合,获得的是数据库的所有记录值

(4)返回对应的T的一个实体类的对象,该类泛型类的方法

(5)该方法封装了增删改操作,可实现对数据库中的数据进行增加,删除,修改操作

/*
* 封装了基本的增删改查方法,以供子类继承使用;
* 当前dao没有事务,直接在方法中获取数据库的链接
* */
public class DAO <T>{

//这个是线程安全的
private QueryRunner queryRunner=new QueryRunner();

private Class<T> clazz;

//类的构造方法
public DAO() {
  //得到父类带泛型的类型
  //type类型导包为import java.lang.reflect.Type;反射类型里面的
  //反射。。。。。。Type所有超级类接口,ParameterizedType表示参数化类型,参数化类型在反射方法首次需要时创建(在此包中指定)。
  //当创建参数化类型 p 时,p 实例化的一般类型声明会被解析,并且按递归方式创建 p 的所有类型参数。

  //superClass instanceof ParameterizedType,判断superClass是否为首次出现的该类型,instanceof是用法,参数,例子的意思;
  Type superClass=getClass().getGenericSuperclass();
  if (superClass instanceof ParameterizedType) {
    ParameterizedType parameterizedType=(ParameterizedType) superClass;

    // getActualTypeArguments():返回表示此类型实际类型参数的 Type 对象的数组。
    Type[] typeArgs=parameterizedType.getActualTypeArguments();
    if (typeArgs!=null && typeArgs.length>0) {
      if (typeArgs[0] instanceof Class) {
        clazz=(Class<T>) typeArgs[0];
      }
    }
  }
}

//返回数据库里面一条记录的值:比如返回某一条记录的customerName,或返回有多少条记录
public <E> E getForValue(String sql,Object...args){
Connection connection=null;
try {
connection=JdbcUtils.getConnection();
return (E) queryRunner.query(connection,sql,new ScalarHandler(),args);
} catch (Exception e) {
e.printStackTrace();
}finally {
JdbcUtils.releaseConnection(connection);
}
return null;
}

//返回所对应的list集合,获得的是数据库中所有数据的集合
public List<T> getForList(String sql,Object...args){
Connection connection=null;
try {
connection=JdbcUtils.getConnection();
return queryRunner.query(connection,sql,new BeanListHandler<>(clazz),args);
} catch (Exception e) {
e.printStackTrace();
}finally {
JdbcUtils.releaseConnection(connection);
}
return null;
}

//返回对应的T的一个实体类的对象
public T get(String sql,Object...args){

Connection connection=null;
try {
connection=JdbcUtils.getConnection();
return queryRunner.query(connection,sql,new BeanHandler<>(clazz),args);
} catch (Exception e) {
e.printStackTrace();
}finally {
JdbcUtils.releaseConnection(connection);
}
return null;
}


//该方法封装了增删改操作
public void update(String sql,Object...args){
Connection connection=null;

try {
connection=JdbcUtils.getConnection();
queryRunner.update(connection, sql, args);

} catch (Exception e) {
e.printStackTrace();
}finally {
JdbcUtils.releaseConnection(connection);
}
}
}

---------------------------------------------------------------------------

8.CustomerDAOJdbcImpl类,继承了父类DAO和继承了接口CustomerDAO,及可获得父类DAO的非构造方法,和接口CustomerDAO封装的方法,并在此类中将接口封装的方法加以实现

CustomerDAOJdbcImpl extends DAO<Customer> implements CustomerDAO

//实现各个功能的类
public class CustomerDAOJdbcImpl extends DAO<Customer> implements CustomerDAO{

@Override
//模糊查询的方法
public List<Customer> getForListWithCriteriaCustomer(CriteriaCustomer cc) {
String sql="select id,name,address,phone from customer "
+ "where name like ? and address like ? and phone like ?";
//修改了CriteriaCustomer的get方法,使其返回的字符串中含有%%
return getForList(sql,cc.getName(),cc.getAddress(),cc.getPhone());
}

@Override
//获取整个数据库中的所有数据
public List<Customer> getAll() {
String sql1="select id,name,address,phone from customer";
return getForList(sql1);
}

@Override
//插入数据到数据库的方法
public void save(Customer customer) {
String sql2="insert into customer(name,address,phone) values(?,?,?)";
update(sql2, customer.getName(),customer.getAddress(),customer.getPhone());
}

@Override
//获取某个数据
public Customer get(Integer id) {
String sql3="select id,name,address,phone from customer where id=?";
return get(sql3,id);
}

@Override
//删除id=?的数据
public void delete(Integer id) {
String sql4="delete from customer where id=?";
update(sql4, id);
}

@Override
//用名字获取count(name),即获取用户的数量
public long getCountWithName(String name) {
String sql5="select count(name) from customer where name=?";
return getForValue(sql5, name);
}

@Override

//对数据进行修改
public void update(Customer customer) {
String sql = "UPDATE customer SET name = ?, address = ?, phone = ? " +
"WHERE id = ?";
update(sql, customer.getName(), customer.getAddress(),
customer.getPhone(), customer.getId());
}

}

---------------------------------------------------------------------------

9.建立一个测试类(建立方法类似于class的建立,在src,所在包下建立,建立时找java下的JUnit,建立就行了):CustomerDAOJdbcImplTest类,对CustomerDAOJdbcImpl类里面的方法进行测试,看在CustomerDAOJdbcImpl类里面的方法是否都可以实现,为后边在Servlet类里面实现这些方法进行测试,避免出错

public class CustomerDAOJdbcImplTest1 {

//测试类:
private CustomerDAO customerDAO=new CustomerDAOJdbcImpl();

@Test
public void testGetForListWithCriteriaCustomer(){
CriteriaCustomer cc=new CriteriaCustomer("l",null,null);
List<Customer> list=customerDAO.getForListWithCriteriaCustomer(cc);
System.out.println(list);

}

public void testGetAll() {
List<Customer> list=customerDAO.getAll();
System.out.println(list);
}


public void testSave() {
Customer customer=new Customer();
customer.setName("jiafds");
customer.setAddress("fldskf");
customer.setPhone("5646566");

customerDAO.save(customer);
}


public void testGetInteger(){
Customer customer=customerDAO.get(3);
System.out.println(customer);
}


public void testDelete() {
customerDAO.delete(3);
}


public void testGetCountWithName() {
long count=customerDAO.getCountWithName("lxn");
System.out.println(count);
}

}

---------------------------------------------------------------------------

10.建立一个Servlet类:CustomerServlet(建立时选择了doGet和doPost方法,间doPost方法放到doGet里边可实现doPost的方法),doPost里边的方法都实现,利用反射接受多个以.do结尾的请求,其他方法实现了模糊查询,增加,删除,修改的操作

public class CustomerServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

private CustomerDAO customerDAO=new CustomerDAOJdbcImpl();

protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}

protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//利用反射,接受多个以.do结尾的请求

//1.变量 servletPath获取所有后边带.do的方法名和.do ,例如这个/pass.do
String servletPath=request.getServletPath();


//2.在字符串后面除去.do这个字符
String methodName=servletPath.substring(1);
methodName=methodName.substring(0,methodName.length()-3);
//System.out.println(methodName);
try {
Method method=getClass().getDeclaredMethod
(methodName, HttpServletRequest.class,HttpServletResponse.class);
//这里面的this指通过这个方法的到方法名字,并且输出,及this指methodName的到的方法名字
method.invoke(this,request,response);


} catch (Exception e) {
//如果出现错误,去error页面
response.sendRedirect("error.jsp");
}

}

//查询方法
private void query(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
//获取模糊查询的请求参数
String name=request.getParameter("name");
String address=request.getParameter("address");
String phone=request.getParameter("phone");

//把请求参数封装为一个CriteriaCustomer对象
CriteriaCustomer cc=new CriteriaCustomer(name,address,phone);

//1.调用CustomerDAO的getForListWithCriteriaCustomer(cc)方法得到lists的集合
List<Customer> lists=customerDAO.getForListWithCriteriaCustomer(cc);

//2.把list集合放到request中
request.setAttribute("list", lists);

//3.转发页面到index.jsp(不能使用重定向) /代表的是根目录下的jsp文件;
request.getRequestDispatcher("/index.jsp").forward(request, response);
}

//插入数据的方法:
private void addCustomer(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException{

//1.获取表单参数,name,address,phone
String name=request.getParameter("name");
String address=request.getParameter("address");
String phone=request.getParameter("phone");

//检验名字是否已经被占用了:调用CustomerDAO的getCountWithName方法,获取name参数是否大于0,如果大于....
//并且消息可以回显:value="<%=request.getParameter("name")==null ? "" :%>"
long count=customerDAO.getCountWithName(name);

if (count>0) {
request.setAttribute("message","用户名"+name+"已经被占用了,请重新选择!!!");

//名字重复了,请求的转发到/newcustomer.jsp
request.getRequestDispatcher("/newcustomer.jsp").forward(request, response);
//结束方法
return ;
}

//2.若验证通过的话,把表单参数封装为一个customer的对象
Customer customer=new Customer(name,address,phone);

//3.调用CustomerDAO的save方法执行保存
customerDAO.save(customer);

//数据插入成功后,请求的转发到newcustomer.jsp
//request.getRequestDispatcher("/newcustomer.jsp").forward(request, response);

//4.数据插入成功后,重定向到success.jsp页面:使用重定向可以避免出现表单的重复提交问题.
response.sendRedirect("success.jsp");

}

private void delete(HttpServletRequest request, HttpServletResponse response) throws IOException {
//1.获取请求的id
String idStr=request.getParameter("id");
int id=0;
try {
id=Integer.parseInt(idStr);
//2.调用CustomerDAO的getId()方法执行删除
customerDAO.delete(id);
} catch (Exception e) {
response.sendRedirect("query.do");
}
//重定向的页面jsp,其前面不用加"/"
response.sendRedirect("success.jsp");
}

//修改数据表里的数据
private void edit(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//出现了所查询的数据不存在的错误显示到error.jsp页面
String forwardPath="/error.jsp";

//1.获取请求的id
String idStr=request.getParameter("id");

//2. 调用 CustomerDAO 的 customerDAO.get(id) 获取和 id 对应的 Customer 对象 customer
try {

Customer customer=customerDAO.get(Integer.parseInt(idStr));
//如果数据存在的跳转到updatecustomer.jsp页面,进行修改数据
if(customer!=null){
forwardPath="/updatecustomer.jsp";
//将数据放到request请求的转发的里面
request.setAttribute("customer", customer);
}
} catch (Exception e) {
System.out.println("fsdlkf");
}

//4. 响应 updatecustomer.jsp 页面: 转发.
request.getRequestDispatcher(forwardPath).forward(request, response);
}

private void update(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//1. 获取表单参数: id, name, address, phone, oldName
String id=request.getParameter("id");
String name=request.getParameter("name");
String address=request.getParameter("address");
String phone=request.getParameter("phone");
String oldName=request.getParameter("oldName");

//2. 检验 name 是否已经被占用:

//2.1 比较 name 和 oldName 是否相同, 若相同说明 name 可用.
//2.1 若不相同, 则调用 CustomerDAO 的 getCountWithName(String name) 获取 name 在数据库中是否存在
if(!oldName.equalsIgnoreCase(name)){
long count=customerDAO.getCountWithName(name);

//2.2 若返回值大于 0, 则响应 updatecustomer.jsp 页面: 通过转发的方式来响应 newcustomer.jsp
if (count>0) {
//2.2.1 在 updatecustomer.jsp 页面显示一个错误消息: 用户名 name 已经被占用, 请重新选择!
//在 request 中放入一个属性 message: 用户名 name 已经被占用, 请重新选择!,
//在页面上通过 request.getAttribute("message") 的方式来显示
request.setAttribute("message", "用户名"+name+"已经被占用,请重现选择!!!");

//2.2.2 newcustomer.jsp 的表单值可以回显.
//address, phone 显示提交表单的新的值, 而 name 显示 oldName, 而不是新提交的 name

//2.2.3 结束方法: return
request.getRequestDispatcher("updatecustomer.jsp").forward(request, response);
return ;
}
}
//3. 若验证通过, 则把表单参数封装为一个 Customer 对象 customer
Customer customer=new Customer(name,address,phone);
customer.setId(Integer.parseInt(id));

//4. 调用 CustomerDAO 的 update(Customer customer) 执行更新操作
customerDAO.update(customer);

//5. 重定向到 query.do
response.sendRedirect("query.do");
}

}

---------------------------------------------------------------------------

11.error.jsp页面,如果出现错误或异常,可以跳转到这个一面

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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>
<h3>不好意思页面无法打开!!!</h3>
</body>
</html>

--------------------------------------------------------------------------

12.index.jsp页面,是主页面,在这个里面有JQuary方法(要进行删除操作时,闪现出一个小框,询问是否确定删除),可实现模糊查询,按Query,显示模糊查询的结构,按Add New Customer可以进行数据的插入,填入数据插入后,显示操作成功页面success.jsp,点击该页面的Return...又返回到index.jsp页面,同时点击每个数据后面的delete和update可以实现对数据的删除和修改,点击update,到updatecustomer.jsp页面,实现对数据的修改.

<%@page import="com.lanqiao.javatest2.Customer"%>
<%@page import="java.util.List"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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>
<script type="text/javascript" src="JavaScript/jquery-1.7.2.js"></script>
<script type="text/javascript">

$(function(){
$(".delete12").click(function(){
var content = $(this).parent().parent().find("td:eq(1)").text();
var flag = confirm("确定要是删除" + content + "的信息吗?");
return flag;
});
});

</script>
</head>
<body>
<form action="query.do" method="post">
<table>
<tr>
<td>name:</td>
<td><input type="text" name="name"/></td>
</tr>

<tr>
<td>address:</td>
<td><input type="text" name="address"/></td>
</tr>

<tr>
<td>phone:</td>
<td><input type="text" name="phone"/></td>
</tr>

<tr>
<td><input type="submit" value="Query"/></td>
<td><a href="newcustomer.jsp">Add New Customer(insert new Customer)</a></td>
</tr>
</table>
</form>
<%
List<Customer> list=(List<Customer>)request.getAttribute("list");
if(list!=null && list.size()>0){
%>
<hr>
<table border="1" cellpadding="10" cellspacing="0">
<tr>
<th>id</th>
<th>name</th>
<th>address</th>
<th>phone</th>
<th>Delete</th>
<th>Update</th>
</tr>
<%for(Customer list12:list){%>
<tr>
<td><%=list12.getId() %></td>
<td><%=list12.getName() %></td>
<td><%=list12.getAddress() %></td>
<td><%=list12.getPhone() %></td>
<td><a href="delete.do?id=<%=list12.getId() %>" class="delete12">Delete</a></td>
<td><a href="edit.do?id=<%=list12.getId() %>">Update</a></td>
</tr>
<%}%>
</table>

<%}%>
</body>
</html>

---------------------------------------------------------------------------

13.newcustomer.jsp页面,是index.jsp页面点击Add New Customer(insert new Customer),跳转到的,在该可以进行对数据的插入操作,其中,必须判断是否数据表中已经含有该name.

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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>
<%
Object obj=request.getAttribute("message");
if(obj!=null){
out.println("<br>");
%>
<font color="red"><%=obj %></font>
<br><br>
<%}%>
<form action="addCustomer.do" method="post">
<table border="1" >
<tr>
<td>name:</td>
<td><input type="text" name="name"
value="<%=request.getParameter("name")==null ? "" : request.getParameter("name")%>" /></td>
</tr>

<tr>
<td>address:</td>
<td><input type="text" name="address"
value="<%=request.getParameter("address")==null ? "" : request.getParameter("address")%>"/></td>
</tr>

<tr>
<td>phone:</td>
<td><input type="text" name="phone"
value="<%=request.getParameter("phone")==null ? "" : request.getParameter("phone")%>"/></td>
</tr>

<tr>
<td colspan="2"><input type="submit" value="Submit"/></td>

</tr>
</table>
</form>
</body>
</html>

---------------------------------------------------------------------------

14.success.jsp页面,插入,修改的页面提交之后,如果成功的话,就出现在该页面,作用是返回操作成功的信息给用户,再点击Return...,返回到index.jsp页面

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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>
<h2>操作成功!!!</h2>
<a href="index.jsp">Return....</a>
</body>
</html>

---------------------------------------------------------------------------

15.updatecustomer.jsp页面,实现对数据的修改,其中使用了<input type="hidden" name="id" value="<%= id %>"/>这个隐藏类型,在页面上用户看不到这个类型而已。

<%@page import="com.lanqiao.javatest2.Customer"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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>
<%
Object str=request.getAttribute("message");
if(str!=null){
%>
<font color="red"><%=str %></font>
<%
}
%>
<%
String id=null;
String name=null;
String address=null;
String phone=null;
String oldName=null;

Customer customer=(Customer)request.getAttribute("customer");
if(customer!=null){
id=customer.getId()+"";
name=customer.getName();
oldName=customer.getName();
address=customer.getAddress();
phone=customer.getPhone();
}
else{
id=request.getParameter("id");
name=request.getParameter("name");
oldName=request.getParameter("oldName");
address=request.getParameter("address");
phone=request.getParameter("phone");
}
%>

<form action="update.do" method="post">
<input type="hidden" name="id" value="<%= id %>"/>
<input type="hidden" name="oldName" value="<%= oldName %>"/>
<table border="1" >
<tr> <td>name:</td>
<td><input type="text" name="name" value="<%=name %>" /></td>
</tr>

<tr> <td>address:</td>
<td><input type="text" name="address" value="<%=address%>"/></td>
</tr>

<tr> <td>phone:</td>
<td><input type="text" name="phone" value="<%=phone%>"/></td>
</tr>

<tr>
<td colspan="2"><input type="submit" value="Submit"/></td>

</tr>
</table>
</form>
</body>
</html>

---------------------------------------------------------------------------

页面及数据库截图:

操作前数据库表:

MVC设计模式((javaWEB)在数据库连接池下,实现对数据库中的数据增删改查操作)

index.jsp页面显示:在表格里填入数据,点击Query可以进行模糊查询。

MVC设计模式((javaWEB)在数据库连接池下,实现对数据库中的数据增删改查操作)

MVC设计模式((javaWEB)在数据库连接池下,实现对数据库中的数据增删改查操作)

 点击Add New。。。可以插入数据;

MVC设计模式((javaWEB)在数据库连接池下,实现对数据库中的数据增删改查操作)

 插入数据后显示:点击后返回到index.jsp页面

MVC设计模式((javaWEB)在数据库连接池下,实现对数据库中的数据增删改查操作)

点击表格里面的delete出现:点击确定和取消,完成此方法

 MVC设计模式((javaWEB)在数据库连接池下,实现对数据库中的数据增删改查操作)

 点击表格里面的update出现这个页面,表格里面还有原来的值,并进行修改

MVC设计模式((javaWEB)在数据库连接池下,实现对数据库中的数据增删改查操作)

---------------------------------------------------------------------------

16.在lib下边的web.xml文件,实现对Servlet和jsp文件之间的配置和反射;*.do可以对后边带.do的Servlet类里面的方法进行反射和获取

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" >


<servlet>
<description></description>
<display-name>CustomerServlet1</display-name>
<servlet-name>CustomerServlet1</servlet-name>
<servlet-class>com.lanqiao.javatest6.CustomerServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>CustomerServlet1</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>

</web-app>