简单MVC实现增删改查
反射工具类RelfectionUtils
package Utils; import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type; /**
* 反射的 Utils 函数集合
* 提供访问私有变量, 获取泛型类型 Class, 提取集合中元素属性等 Utils 函数
* @author Administrator
*
*/
public class ReflectionUtils { /**
* 通过反射, 获得定义 Class 时声明的父类的泛型参数的类型
* 如: public EmployeeDao extends BaseDao<Employee, String>
* @param clazz
* @param index
* @return
*/
@SuppressWarnings("unchecked")
public static Class getSuperClassGenricType(Class clazz, int index){
Type genType = clazz.getGenericSuperclass(); if(!(genType instanceof ParameterizedType)){
return Object.class;
} Type [] params = ((ParameterizedType)genType).getActualTypeArguments(); if(index >= params.length || index < 0){
return Object.class;
} if(!(params[index] instanceof Class)){
return Object.class;
} return (Class) params[index];
} /**
* 通过反射, 获得 Class 定义中声明的父类的泛型参数类型
* 如: public EmployeeDao extends BaseDao<Employee, String>
* @param <T>
* @param clazz
* @return
*/
@SuppressWarnings("unchecked")
public static<T> Class<T> getSuperGenericType(Class clazz){
return getSuperClassGenricType(clazz, 0);
} /**
* 循环向上转型, 获取对象的 DeclaredMethod
* @param object
* @param methodName
* @param parameterTypes
* @return
*/
public static Method getDeclaredMethod(Object object, String methodName, Class<?>[] parameterTypes){ for(Class<?> superClass = object.getClass(); superClass != Object.class; superClass = superClass.getSuperclass()){
try {
//superClass.getMethod(methodName, parameterTypes);
return superClass.getDeclaredMethod(methodName, parameterTypes);
} catch (NoSuchMethodException e) {
//Method 不在当前类定义, 继续向上转型
}
//..
} return null;
} /**
* 使 filed 变为可访问
* @param field
*/
public static void makeAccessible(Field field){
if(!Modifier.isPublic(field.getModifiers())){
field.setAccessible(true);
}
} /**
* 循环向上转型, 获取对象的 DeclaredField
* @param object
* @param filedName
* @return
*/
public static Field getDeclaredField(Object object, String filedName){ for(Class<?> superClass = object.getClass(); superClass != Object.class; superClass = superClass.getSuperclass()){
try {
return superClass.getDeclaredField(filedName);
} catch (NoSuchFieldException e) {
//Field 不在当前类定义, 继续向上转型
}
}
return null;
} /**
* 直接调用对象方法, 而忽略修饰符(private, protected)
* @param object
* @param methodName
* @param parameterTypes
* @param parameters
* @return
* @throws InvocationTargetException
* @throws IllegalArgumentException
*/
public static Object invokeMethod(Object object, String methodName, Class<?> [] parameterTypes,
Object [] parameters) throws InvocationTargetException{ Method method = getDeclaredMethod(object, methodName, parameterTypes); if(method == null){
throw new IllegalArgumentException("Could not find method [" + methodName + "] on target [" + object + "]");
} method.setAccessible(true); try {
return method.invoke(object, parameters);
} catch(IllegalAccessException e) {
System.out.println("不可能抛出的异常");
} return null;
} /**
* 直接设置对象属性值, 忽略 private/protected 修饰符, 也不经过 setter
* @param object
* @param fieldName
* @param value
*/
public static void setFieldValue(Object object, String fieldName, Object value){
Field field = getDeclaredField(object, fieldName); if (field == null)
throw new IllegalArgumentException("Could not find field [" + fieldName + "] on target [" + object + "]"); makeAccessible(field); try {
field.set(object, value);
} catch (IllegalAccessException e) {
System.out.println("不可能抛出的异常");
}
} /**
* 直接读取对象的属性值, 忽略 private/protected 修饰符, 也不经过 getter
* @param object
* @param fieldName
* @return
*/
public static Object getFieldValue(Object object, String fieldName){
Field field = getDeclaredField(object, fieldName); if (field == null)
throw new IllegalArgumentException("Could not find field [" + fieldName + "] on target [" + object + "]"); makeAccessible(field); Object result = null; try {
result = field.get(object);
} catch (IllegalAccessException e) {
System.out.println("不可能抛出的异常");
} return result;
}
}
jdbcutils
package Utils; import java.sql.Connection;
import java.sql.SQLException; import javax.sql.DataSource; import com.mchange.v2.c3p0.ComboPooledDataSource; public class JdbcUtil { public static void reaseConnection(Connection connection) {
try {
if(connection!=null){
connection.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private static DataSource dataSource = null;
static{
dataSource = new ComboPooledDataSource("mvc");
} public static DataSource getDataSource(){
return dataSource;
} public static Connection getConnection(){
try {
return dataSource.getConnection();
} catch (SQLException e) {
// TODO Auto-generated catch block
//e.printStackTrace();
System.out.println("连接失败");
}
return null; }
}
DAO操作类 依靠c3p0,dbutil
package com.mvc.dao; import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List; import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;
import org.apache.el.util.ReflectionUtil; import com.sun.org.apache.bcel.internal.generic.NEW; import Utils.JdbcUtil;
import Utils.ReflectionUtils; public class Dao<T> {
private Class<T> clazz;
private QueryRunner queryRunner= new QueryRunner(JdbcUtil.getDataSource());
public Dao() {
clazz = ReflectionUtils.getSuperGenericType(this.getClass());
}
public <E> E getForValue(String sql,Object ...arg){
try {
return (E) queryRunner.query(sql,new ScalarHandler(),arg);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("getForValue error");
}
return null; }
public List<T> getForList(String sql,Object ...args){
try {
return queryRunner.query(sql, new BeanListHandler<>(clazz),args);
} catch (SQLException e) {
System.out.println("getForList error");
}
return null; }
public T get(String sql,Object ...args){
try {
return queryRunner.query(sql,new BeanHandler<>(clazz),args);
} catch (SQLException e) {
// TODO Auto-generated catch block
//e.printStackTrace();
System.out.println("get error");
}
return null;
} public void updata(String sql,Object ...args){
try {
queryRunner.update(sql,args);
} catch (SQLException e) {
// TODO Auto-generated catch block
//e.printStackTrace();
System.out.println("updata error");
}
}
}
Customer类
package com.mvc.domain; public class Customer {
private String id;
private String name;
private String address;
private String phone;
private int count; public Customer() {
super();
}
public Customer(String id, String name, String address, String phone) {
super();
this.id = id;
this.name = name;
this.address = address;
this.phone = phone;
this.count = 0;
} public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public String getId() {
return id;
}
public void setId(String 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 [id=" + id + ", name=" + name + ", address=" + address + ", phone=" + phone + "]";
} }
CustomerDao接口
package com.mvc.dao; import java.util.List; import com.mvc.domain.CriteriaCustomer;
import com.mvc.domain.Customer; public interface CustomerDao {
public List<Customer> getAll();
public void save(Customer customer);
public void upCount(Customer customer);
public void upAll(Customer customer);
public Customer getForIdName(String id,String name);
public Customer getForId(String id);
public void delete(String id);
public long getCountWithName(String name);
public long getCountWithId(String id);
public List<Customer> getCriteriaCustomers(CriteriaCustomer cc);
}
操作类
package com.mvc.jdbc; import java.sql.SQLException;
import java.util.List; import com.mvc.dao.CustomerDao;
import com.mvc.dao.Dao;
import com.mvc.domain.CriteriaCustomer;
import com.mvc.domain.Customer; public class CustomerDaoJdbc extends Dao<Customer> implements CustomerDao{ @Override
public List<Customer> getAll(){
// TODO Auto-generated method stub
String sql = "SELECT * FROM customer";
return getForList(sql);
} @Override
public void save(Customer customer) {
// TODO Auto-generated method stub
String sql = "insert into customer(id,name,address,phone,count) value(?,?,?,?,?)";
updata(sql,customer.getId(),customer.getName(),customer.getAddress(),customer.getPhone(),customer.getCount()); } @Override
public void upAll(Customer customer) {
// TODO Auto-generated method stub
String sql = "UPDATE customer SET NAME=?,ADDRESS=?,PHONE=? WHERE ID=?";
updata(sql,customer.getName(),customer.getAddress(),customer.getPhone(),customer.getId()); } @Override
public void upCount(Customer customer) {
// TODO Auto-generated method stub
String sql = "UPDATE customer SET COUNT=? WHERE ID=?";
updata(sql,customer.getCount(),customer.getId());
} @Override
public Customer getForIdName(String id,String name) {
// TODO Auto-generated method stub
String sql = "SELECT id,name,address,phone,count FROM customer where id = ? and name = ?";
return get(sql,id,name);
}
@Override
public Customer getForId(String id) {
// TODO Auto-generated method stub
String sql = "SELECT id,name,address,phone,count FROM customer where id = ?";
return get(sql,id);
}
@Override
public void delete(String id) {
// TODO Auto-generated method stub
String sql = "delete from customer where ID = ? ";
updata(sql,id); } @Override
public long getCountWithName(String name) {
// TODO Auto-generated method stub
String sql = "select count(id) from customer where name = ?";
return (long)getForValue(sql, name);
} @Override
public List<Customer> getCriteriaCustomers(CriteriaCustomer cc) {
// TODO Auto-generated method stub
String sql = "SELECT * FROM customer WHERE NAME LIKE ? AND address LIKE ? AND phone LIKE ?";
System.out.println(cc.getName()+cc.getAddress()+cc.getPhone());
return getForList(sql,cc.getName(),cc.getAddress(),cc.getPhone());
} @Override
public long getCountWithId(String id) {
// TODO Auto-generated method stub
String sql = "select count(id) from customer where id= ?";
return (long)getForValue(sql, id);
} }
servlet
package com.mvc.servlet; import java.io.IOException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List; 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 com.mvc.dao.CustomerDao;
import com.mvc.domain.CriteriaCustomer;
import com.mvc.domain.Customer;
import com.mvc.jdbc.CustomerDaoJdbc;
import com.sun.corba.se.impl.protocol.giopmsgheaders.Message; import jdk.nashorn.internal.ir.RuntimeNode.Request; /**
* Servlet implementation class SqlServlet
*/
@WebServlet("/SqlServlet")
public class SqlServlet extends HttpServlet {
private static final long serialVersionUID = 1L; /**
* @see HttpServlet#HttpServlet()
*/
CustomerDao customerDao = null; public SqlServlet() {
super();
// TODO Auto-generated constructor stub
customerDao = new CustomerDaoJdbc();
} /**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doPost(request, response);
} /**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
request.setCharacterEncoding("utf-8");
String servletPath = request.getServletPath();
// System.out.println(servletPath);
servletPath = servletPath.substring(1, servletPath.length() - 3);
try {
Method method = getClass().getDeclaredMethod(servletPath,
HttpServletRequest.class, HttpServletResponse.class);
method.invoke(this, request, response);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} } @SuppressWarnings("unused")
private void edit(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("edit");
String id = request.getParameter("id");
System.out.println(id);
Customer customer = customerDao.getForId(id);
System.out.println(customer);
request.setAttribute("customer", customer);
request.getRequestDispatcher("/updata.jsp").forward(request, response);
} @SuppressWarnings("unused")
private void updata(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("updata");
String id = request.getParameter("id");
String name = request.getParameter("name");
String address = request.getParameter("address");
String phone = request.getParameter("phone");
Customer customer = new Customer(id, name, address, phone);
String massage;
massage = "修改成功";
customerDao.upAll(customer);
request.setAttribute("massage", massage);
request.setAttribute("customer", customer);
request.getRequestDispatcher("/updata.jsp").forward(request, response); } @SuppressWarnings("unused")
private void add(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("add");
String id = request.getParameter("id");
String name = request.getParameter("name");
String address = request.getParameter("address");
String phone = request.getParameter("phone");
Customer customer = new Customer(id, name, address, phone);
System.out.println(customer);
long count = customerDao.getCountWithName(name);
long count1 = customerDao.getCountWithId(id);
System.out.println("count:" + count);
request.setAttribute("id", id);
request.setAttribute("name", name);
request.setAttribute("address", address);
request.setAttribute("phone", phone);
if (count1 > 0) {
request.setAttribute("massage", "学号已存在,报名失败");
request.getRequestDispatcher("/newcustomer.jsp").forward(request,
response);
} else {
request.setAttribute("massage", "报名成功");
customerDao.save(customer);
request.getRequestDispatcher("/newcustomer.jsp").forward(request,
response);
}
} @SuppressWarnings("unused")
private void delete(HttpServletRequest request, HttpServletResponse response)
throws IOException {
System.out.println("delete");
String id = request.getParameter("id");
System.out.println(id);
//customerDao.delete(id); //response.sendRedirect("query.do");
} @SuppressWarnings("unused")
private void query(HttpServletRequest request, HttpServletResponse response) {
String name = request.getParameter("name");
String address = request.getParameter("address");
String phone = request.getParameter("phone");
System.out.println(name + address + phone);
System.out.println("query");
CriteriaCustomer cc = new CriteriaCustomer(name, address, phone);
List<Customer> customers = customerDao.getCriteriaCustomers(cc);
request.setAttribute("customers", customers);
try {
request.getRequestDispatcher("/1.jsp").forward(request,
response);
} catch (ServletException | IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} @SuppressWarnings("unused")
private void come(HttpServletRequest request, HttpServletResponse response) {
String name = request.getParameter("name");
String id = request.getParameter("id");
Customer customer = customerDao.getForIdName(id, name);
System.out.println(customer);
if (customer != null) {
request.setAttribute("massage", "");
customer.setCount(customer.getCount() + 1);
customerDao.upCount(customer);
//response.sendRedirect("NewFile_1.jsp");
try {
request.getRequestDispatcher("/NewFile_1.jsp").forward(request,
response);
} catch (ServletException | IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} } else {
try {
String massage = "名字或学号错误";
request.setAttribute("massage", massage);
request.setAttribute("id", id);
request.setAttribute("name", name);
request.getRequestDispatcher("/come.jsp").forward(request,
response);
} catch (ServletException | IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
以下jsp代码 css样式等为网上找的素材
1.增加用户的jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!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">
<!--[if IE]><meta http-equiv="x-ua-compatible" content="IE=9" /><![endif]-->
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="Your Description Here">
<meta name="keywords"
content="bootstrap themes, portfolio, responsive theme">
<meta name="author" content="ThemeForces.Com"> <!-- Favicons
================================================== -->
<!-- <link rel="shortcut-icon" href="img/favicon.ico" type="image/x-icon"> -->
<!-- <link rel="apple-touch-icon" href="img/apple-touch-icon.png"> -->
<link rel="apple-touch-icon" sizes="72x72"
href="img/apple-touch-icon-72x72.png">
<link rel="apple-touch-icon" sizes="114x114"
href="img/apple-touch-icon-114x114.png"> <!-- Bootstrap -->
<link rel="stylesheet" type="text/css" href="css/bootstrap.css">
<!-- <link rel="stylesheet" type="text/css"
href="fonts/font-awesome/css/font-awesome.css"> --> <!-- Stylesheet
================================================== -->
<link rel="stylesheet" type="text/css" href="css/style.css">
<!-- <link rel="stylesheet" type="text/css" href="css/responsive.css"> --> <!-- <script type="text/javascript" src="js/modernizr.custom.js"></script> --> <title></title>
</head>
<body>
<h3 style="text-align: center;"><font color="red"><c:out value="${massage}"></c:out></font></h3>
<div id="tf-contact">
<div class="container">
<div class="section-title">
<hr>
</div> <div class="space"></div> <div class="row">
<div class="col-md-6 col-md-offset-3">
<form id="contact" action="add.do" method="post">
<div class="form-group">
<input class="form-control" id="exampleInputEmail1" type="text"
name="id" value="${id}" placeholder="长学号" />
</div> <div class="form-group">
<input class="form-control" type="text" name="name"
value="${name}" placeholder="姓名" />
</div> <div class="form-group">
<input class="form-control" type="text" name="address"
value="${address}" placeholder="班级" />
</div> <td><div class="form-group">
<input class="form-control" type="text" name="phone"
value="${phone}" placeholder="手机" />
</div>
<input class="btn btn-primary my-btn dark" type="submit"
value="--报名--" />
</form>
</div>
</div>
</div>
</div>
<!-- <script type="text/javascript" src="js/jquery.1.11.1.js"></script>-->
<!-- Include all compiled plugins (below), or include individual files as needed -->
<!-- <script type="text/javascript" src="js/bootstrap.js"></script> --> <!-- Javascripts
================================================== -->
<!-- <script type="text/javascript" src="js/main.js"></script>-->
</body>
</html>
2.查询
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <!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="scripts/jquery-1.7.2.js"></script>
<script type="text/javascript">
</script>
</head>
<body> <form action="query.do" method="post">
<table>
<tr>
<td>CustomerName:</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</a></td>
</tr>
</table>
</form> <br><br> <c:if test="${!empty requestScope.customers }"> <hr>
<br><br> <table border="1" cellpadding="10" cellspacing="0">
<tr>
<th>^ID</th>
<th>CustomerName</th>
<th>Address</th>
<th>Phone</th>
<th>UPDATE\DELETE</th>
</tr> <c:forEach items="${requestScope.customers }" var="cust"> <tr>
<td>${cust.id }</td>
<td>${cust.name }</td>
<td>${cust.address }</td>
<td>${cust.phone }</td>
<td>
<c:url value="/edit.do" var="editurl">
<c:param name="id" value="${cust.id }"></c:param>
</c:url>
<a href="${editurl }">UPDATE</a>
<c:url value="/delete.do" var="deleteurl">
<c:param name="id" value="${cust.id }"></c:param>
</c:url>
<a href="${deleteurl }" class="delete">DELETE</a>
</td>
</tr> </c:forEach> </table>
</c:if> </body>
</html>
3.删除
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <!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="scripts/jquery-1.7.2.js"></script> </head>
<body> <form action="query.do" method="post">
<table>
<tr>
<td>CustomerName:</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</a></td>
</tr>
</table>
</form> <br><br> <c:if test="${!empty requestScope.customers }"> <hr>
<br><br> <table border="1" cellpadding="10" cellspacing="0">
<tr>
<th>^ID</th>
<th>CustomerName</th>
<th>Address</th>
<th>Phone</th>
<th>UPDATE\DELETE</th>
</tr> <c:forEach items="${requestScope.customers }" var="cust"> <tr>
<td>${cust.id }</td>
<td>${cust.name }</td>
<td>${cust.address }</td>
<td>${cust.phone }</td>
<td>
<c:url value="/edit.do" var="editurl">
<c:param name="id" value="${cust.id }"></c:param>
</c:url>
<a href="${editurl }">UPDATE</a>
<c:url value="/delete.do" var="deleteurl">
<c:param name="id" value="${cust.id }"></c:param>
</c:url>
<a href="${deleteurl }" class="delete">DELETE</a>
</td>
</tr> </c:forEach> </table>
</c:if> </body>
</html>
4.修改
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!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">
<!--[if IE]><meta http-equiv="x-ua-compatible" content="IE=9" /><![endif]-->
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="Your Description Here">
<meta name="keywords"
content="bootstrap themes, portfolio, responsive theme">
<meta name="author" content="ThemeForces.Com"> <!-- Favicons
================================================== -->
<link rel="shortcut icon" href="img/favicon.ico" type="image/x-icon">
<link rel="apple-touch-icon" href="img/apple-touch-icon.png">
<link rel="apple-touch-icon" sizes="72x72"
href="img/apple-touch-icon-72x72.png">
<link rel="apple-touch-icon" sizes="114x114"
href="img/apple-touch-icon-114x114.png"> <!-- Bootstrap -->
<link rel="stylesheet" type="text/css" href="css/bootstrap.css">
<link rel="stylesheet" type="text/css"
href="fonts/font-awesome/css/font-awesome.css"> <!-- Stylesheet
================================================== -->
<link rel="stylesheet" type="text/css" href="css/style.css">
<link rel="stylesheet" type="text/css" href="css/responsive.css"> <script type="text/javascript" src="js/modernizr.custom.js"></script> <title></title>
</head>
<body> <c:if test="${requestScope.massage != null }">
<br>
<h3 style="text-align: center;"><font color="red"><c:out value="${massage}"></c:out></font></h3>
<br>
<br>
</c:if> <c:set var="id" value="${customer != null ? customer.id : param.id }"></c:set> <c:set var="name"
value="${customer != null ? customer.name : param.name }"></c:set>
<c:set var="address"
value="${customer != null ? customer.address : param.address }"></c:set>
<c:set var="phone"
value="${customer != null ? customer.phone : param.phone }"></c:set>
<div id="tf-contact">
<div class="container">
<div class="section-title"> <hr>
</div> <div class="space"></div> <div class="row">
<div class="col-md-6 col-md-offset-3">
<form id="contact" action="updata.do" method="post">
<input type="hidden" name="id" value="${id }"></input>
<div class="form-group">
<h4>姓名:</h4>
<input class="form-control" type="text" name="name"
value="${name}" placeholder="姓名" />
</div> <div class="form-group">
<h4>班级:</h4>
<input class="form-control" type="text" name="address"
value="${address}" placeholder="班级" />
</div> <div class="form-group">
<h4>手机:</h4>
<input class="form-control" type="text" name="phone"
value="${phone}" placeholder="手机" />
</div> <input class="btn btn-primary my-btn dark" type="submit"
value="修改" />
</form>
</div>
</div>
</div>
</div>
<script type="text/javascript" src="js/jquery.1.11.1.js"></script>
<!-- Include all compiled plugins (below), or include individual files as needed -->
<script type="text/javascript" src="js/bootstrap.js"></script> <!-- Javascripts
================================================== -->
<script type="text/javascript" src="js/main.js"></script>
</body>
</html>
web.xml
<?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_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>MVC</display-name> <servlet>
<description></description>
<display-name>SqlServlet</display-name>
<servlet-name>SqlServlet</servlet-name>
<servlet-class>com.mvc.servlet.SqlServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>SqlServlet</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping> </web-app>
运行效果 增加信息
数据库内数据
简单MVC实现增删改查的更多相关文章
- salesforce 零基础开发入门学习(六)简单的数据增删改查页面的构建
VisualForce封装了很多的标签用来进行页面设计,本篇主要讲述简单的页面增删改查.使用的内容和设计到前台页面使用的标签相对简单,如果需要深入了解VF相关知识以及标签, 可以通过以下链接查看或下载 ...
- 分享一个自己写的MVC+EF “增删改查” 无刷新分页程序
分享一个自己写的MVC+EF “增删改查” 无刷新分页程序 一.项目之前得添加几个组件artDialog.MVCPager.kindeditor-4.0.先上几个效果图. 1.首先建立一个数 ...
- 【ASP.NET MVC系列】浅谈jqGrid 在ASP.NET MVC中增删改查
ASP.NET MVC系列文章 [01]浅谈Google Chrome浏览器(理论篇) [02]浅谈Google Chrome浏览器(操作篇)(上) [03]浅谈Google Chrome浏览器(操作 ...
- 【Mybatis】简单的mybatis增删改查模板
简单的mybatis增删改查模板: <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE map ...
- 【转载】salesforce 零基础开发入门学习(六)简单的数据增删改查页面的构建
salesforce 零基础开发入门学习(六)简单的数据增删改查页面的构建 VisualForce封装了很多的标签用来进行页面设计,本篇主要讲述简单的页面增删改查.使用的内容和设计到前台页面使用的 ...
- 最简单的mybatis增删改查样例
最简单的mybatis增删改查样例 Book.java package com.bookstore.app; import java.io.Serializable; public class Boo ...
- ASP.NET Identity系列02,在ASP.NET MVC中增删改查用户
本篇体验在ASP.NET MVC中使用ASP.NET Identity增删改查用户. 源码在这里:https://github.com/darrenji/UseIdentityCRUDUserInMV ...
- MVC——数据库增删改查(aspx)
MVC: V(View) :视图→就是页面的模板 C(Control): 控制器→客户主要面对的就是控制器, M(Model):模板→在模板里面主要就是写关于数据库的各种增删改查的方法 它们之间的关系 ...
- SQL简单语句(增删改查)
简单的SQL语句增删改查操作 说明: 在mysql里面亲测结果正确 用到的表(学生表:studnets) 1.创建一个学生表,(学号,姓名,性别,家庭住址) mysql> create t ...
随机推荐
- eclipse下创建maven项目并部署到tomcat服务器(转)
maven项目部署到服务器有很多方法,可以利用jetty-maven-plugin或者tomcat-maven-plugin,这属于maven的知识点. 通常开发中,需要将项目放在服务器上借助开发工具 ...
- golang语言中sync/atomic包的学习与使用
package main; import ( "sync/atomic" "fmt" "sync" ) //atomic包提供了底层的原子级 ...
- golang语言中os/exec包的学习与使用
package main; import ( "os/exec" "fmt" "io/ioutil" "bytes" ) ...
- Linux 编译时内存不足
1.编译内核出现问题:No space left on device AS .tmp_kallsyms1.o .tmp_kallsyms1.S:2: fatal error: when wr ...
- dangerouslySetInnerHTMl
dangerouslySetInnerHTMl 是React标签的一个属性,类似于angular的ng-bind: 听说这个单词这么长,是故意的,应为有可能不合时宜的使用innerHTML会导致XSS ...
- JAVA程序 从命令行接受多个数字,求和之后输出结果
源程序代码: public class sum{ public static void main(String[] args){ double[] a=new double[4]; a[0]=Doub ...
- eigen Matrix详解
Eigen Matrix 详解 在Eigen中,所有的matrices 和vectors 都是模板类Matrix 的对象,Vectors 只是一种特殊的矩阵,行或者列为1. Matrix的前三个模板参 ...
- 计算器类(C++&JAVA——表达式转换、运算、模板公式)
运行: (a+b)*c 后缀表达式:ab+c* 赋值: Enter the a : 10 Enter the b : 3 Enter the c : 5 结果为:65 代码是我从的逻辑判断系统改过来的 ...
- javascript 高级程序设计 五
1.变量: ECMAScript中的基本类型都是值类型Boolean,Number,Null,Undefined和String,在这里JS和其他的语言有所不同,就是JS中的String是值类型 而不像 ...
- vs2015 npm list 更新问题
在更新npm list时候,经常会非常的慢,今天试了一个诡异的方法,就是在文件夹下面直接把所有缓存全部删除,全部重新下,结果感觉反而速度快很多. 原来的更新包80M竟然1个小时没有下载完. C:\Us ...