20151210--MVC
package com.hanqi; import java.io.IOException;
import java.sql.*;
import java.text.SimpleDateFormat; import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import org.omg.PortableInterceptor.RequestInfoOperations; import com.hanqi.*;
import com.hanqi.dao.User;
import com.hanqi.dao.UserDal; import java.util.*;
import java.util.Date;
/**
* Servlet implementation class SaveUser
*/
public class SaveUser extends HttpServlet {
private static final long serialVersionUID = 1L;
private User user; /**
* @see HttpServlet#HttpServlet()
*/
public SaveUser() {
super();
// TODO Auto-generated constructor stub
} /**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //转码,固定写法
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
response.setCharacterEncoding("UTF-8"); Date dt = new Date();//获取当前时间
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//格式化时间
String sj = sdf.format(dt);//将时间转换为字符串格式 //获取内容
String userID = request.getParameter("userid");
String userName = request.getParameter("username");
String pw = request.getParameter("password");
//判断输入的内容
if(userID == null || userID.trim().length() == 0)
{
//输出内容
response.getWriter().append("用户代码不能为空");
}
else if(userName == null || userName.trim().length() == 0)
{
response.getWriter().append("用户姓名不能为空");
}
else
{ User user = new User(); user.setUserid(userID);
user.setUsername(userName);
user.setPassword(pw);
user.setuserTime(sj); //调用模型层 UserDal ud = new UserDal(); try { ud.insert(user); response.sendRedirect("FindUserList"); } catch (Exception e) { response.getWriter().append("保存数据失败"); e.printStackTrace();
}
}
} /**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
} }
增加
package com.hanqi; import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import com.hanqi.common.DBHelper; import java.sql.*;
import java.util.Date;
import com.hanqi.dao.*; /**
* Servlet implementation class DeleteUser
*/
public class DeleteUser extends HttpServlet {
private static final long serialVersionUID = 1L; /**
* @see HttpServlet#HttpServlet()
*/
public DeleteUser() {
super();
// TODO Auto-generated constructor stub
} /**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //转码
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
response.setCharacterEncoding("UTF-8"); String userid = request.getParameter("userid");
if(userid != null && userid.trim().length() > 0)
{
UserDal ud = new UserDal(); try { ud.delete(userid); response.sendRedirect("FindUserList"); } catch (Exception e) { response.getWriter().append("删除数据失败"); e.printStackTrace();
}
}
} /**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
} }
删除
package com.hanqi; import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement; import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import com.hanqi.common.DBHelper;
import com.hanqi.dao.User;
import com.hanqi.dao.UserDal; /**
* Servlet implementation class EditUser
*/
public class EditUser extends HttpServlet {
private static final long serialVersionUID = 1L; /**
* @see HttpServlet#HttpServlet()
*/
public EditUser() {
super();
// TODO Auto-generated constructor stub
} /**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
response.setCharacterEncoding("UTF-8"); //接受相关参数
String userID = request.getParameter("userid");
String userName = request.getParameter("username");
String pw = request.getParameter("password");
//判断输入的内容
if(userID == null || userID.trim().length() == 0)
{
//输出内容
response.getWriter().append("用户代码不能为空");
}
else if(userName == null || userName.trim().length() == 0)
{
response.getWriter().append("用户姓名不能为空");
}
else
{
User user = new User(); user.setUserid(userID);
user.setUsername(userName);
user.setPassword(pw); UserDal ud = new UserDal(); try
{ ud.update(user); response.sendRedirect("FindUserList"); }
catch(Exception e)
{ response.getWriter().append("修改数据失败"); e.printStackTrace();
}
} } /**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
} }
修改
package com.hanqi; import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.*; import javax.servlet.*;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import com.hanqi.dao.User;
import com.hanqi.dao.UserDal; /**
* Servlet implementation class FindUserList
*/
public class FindUserList extends HttpServlet {
private static final long serialVersionUID = 1L; /**
* @see HttpServlet#HttpServlet()
*/
public FindUserList() {
super();
// TODO Auto-generated constructor stub
} /**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
response.setCharacterEncoding("UTF-8");
UserDal ud = new UserDal();
try
{
ArrayList<User> al = ud.getListAll();
request.setAttribute("userlist", al);
}
catch(Exception e)
{
response.getWriter().append("查找数据失败");
e.printStackTrace();
} //页面跳转
request.getRequestDispatcher("ShowUserList.jsp").forward(request,response); } /**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
} }
查找
package com.hanqi.dao; import java.sql.*;
import java.util.ArrayList; import com.hanqi.common.*; //关于User表的数据库操作类
public class UserDal
{ //增
public int insert(User user) throws Exception
{
int rtn = -1; Connection conn = DBHelper.getConnection(); PreparedStatement pst = null; if (conn != null)
{
try{
String sql = "insert into T_User (user_id, user_name, password,user_time) values (?,?,?,?) "; pst = conn.prepareStatement(sql); pst.setString(1, user.getUserid());
pst.setString(2, user.getUsername());
pst.setString(3, user.getPassword());
pst.setString(4, user.getuserTime());
rtn = pst.executeUpdate();
}
catch(Exception ex)
{
throw ex;
}
finally
{
try
{
pst.close();
}
catch(Exception e)
{
conn.close();
}
}
} return rtn;
} //删 public int delete(String user_id) throws Exception
{
int rtn = -1; Connection conn = DBHelper.getConnection(); PreparedStatement pst = null; if (conn != null)
{
try{
String sql = "delete T_User where user_id = ?"; pst = conn.prepareStatement(sql); pst.setString(1, user_id); rtn = pst.executeUpdate();
}
catch(Exception ex)
{
throw ex;
}
finally
{
try
{
pst.close();
}
catch(Exception e)
{
conn.close();
}
}
} return rtn;
} //改
public int update(User user) throws Exception
{
int rtn = -1; Connection conn = DBHelper.getConnection(); PreparedStatement pst = null; if (conn != null)
{
try
{
String sql = "update T_User set user_name = ?,password = ? "
+ "where user_id = ? "; pst = conn.prepareStatement(sql); pst.setString(3, user.getUserid());
pst.setString(1, user.getUsername());
pst.setString(2, user.getPassword()); rtn = pst.executeUpdate();
}
catch(Exception ex)
{
throw ex;
}
finally
{
try
{
pst.close();
}
catch(Exception e)
{
conn.close();
}
}
} return rtn;
} //查
public ArrayList<User> getListAll() throws Exception
{
ArrayList<User> rtn = new ArrayList<User>(); Connection conn = DBHelper.getConnection(); PreparedStatement pst = null; if (conn != null)
{
try
{ String sql = "select * from t_user"; pst = conn.prepareStatement(sql); ResultSet rs = pst.executeQuery();
if(rs != null)
{
while(rs.next())
{
User u = new User(); u.setUserid(rs.getString("user_id"));
u.setUsername(rs.getString("user_name"));
u.setPassword(rs.getString("password"));
u.setuserTime(rs.getString("user_time"));
rtn.add(u); //取一个数就放一个数
}
}
}
catch(Exception ex)
{
throw ex;
}
finally
{
try
{
pst.close();
}
catch(Exception e)
{
conn.close();
}
}
}
return rtn;
}
}
M
20151210--MVC的更多相关文章
- Asp.Net Mvc 使用WebUploader 多图片上传
来博客园有一个月了,哈哈.在这里学到了很多东西.今天也来试着分享一下学到的东西.希望能和大家做朋友共同进步. 最近由于项目需要上传多张图片,对于我这只菜鸟来说,以前上传图片都是直接拖得控件啊,而且还是 ...
- .Net Core MVC 网站开发(Ninesky) 2.4、添加栏目与异步方法
在2.3中完成依赖注入后,这次主要实现栏目的添加功能.按照前面思路栏目有三种类型,常规栏目即可以添加子栏目也可以选择是否添加内容,内容又可以分文章或其他类型,所以还要添加一个模块功能.这次主要实现栏目 ...
- ASP.NET MVC with Entity Framework and CSS一书翻译系列文章之第二章:利用模型类创建视图、控制器和数据库
在这一章中,我们将直接进入项目,并且为产品和分类添加一些基本的模型类.我们将在Entity Framework的代码优先模式下,利用这些模型类创建一个数据库.我们还将学习如何在代码中创建数据库上下文类 ...
- ASP.NET Core MVC/WebAPi 模型绑定探索
前言 相信一直关注我的园友都知道,我写的博文都没有特别枯燥理论性的东西,主要是当每开启一门新的技术之旅时,刚开始就直接去看底层实现原理,第一会感觉索然无味,第二也不明白到底为何要这样做,所以只有当你用 ...
- ASP.NET Core 中文文档 第四章 MVC(3.8)视图中的依赖注入
原文:Dependency injection into views 作者:Steve Smith 翻译:姚阿勇(Dr.Yao) 校对:孟帅洋(书缘) ASP.NET Core 支持在视图中使用 依赖 ...
- 开源:Taurus.MVC 框架
为什么要创造Taurus.MVC: 记得被上一家公司忽悠去负责公司电商平台的时候,情况是这样的: 项目原版是外包给第三方的,使用:WebForm+NHibernate,代码不堪入目,Bug无限,经常点 ...
- Taurus.MVC 2.2 开源发布:WebAPI 功能增强(请求跨域及Json转换)
背景: 1:有用户反馈了关于跨域请求的问题. 2:有用户反馈了参数获取的问题. 3:JsonHelper的增强. 在综合上面的条件下,有了2.2版本的更新,也因此写了此文. 开源地址: https:/ ...
- Taurus.MVC 2.0 开源发布:WebAPI开发教程
背景: 有用户反映,Tausus.MVC 能写WebAPI么? 能! 教程呢? 嗯,木有! 好吧,刚好2.0出来,就带上WEBAPI教程了! 开源地址: https://github.com/cyq1 ...
- 使用Visual Studio 2015 开发ASP.NET MVC 5 项目部署到Mono/Jexus
最新的Mono 4.4已经支持运行asp.net mvc5项目,有的同学听了这句话就兴高采烈的拿起Visual Studio 2015创建了一个mvc 5的项目,然后部署到Mono上,浏览下发现一堆错 ...
- .NetCore MVC中的路由(2)在路由中使用约束
p { margin-bottom: 0.25cm; direction: ltr; color: #000000; line-height: 120%; orphans: 2; widows: 2 ...
随机推荐
- 关于各种排列(dfs)
代码一:数字有重复: #include <cstdio> ],arr[]={,,,}; void dfs(int v){ if(v >= n){ ;i<n;i++) print ...
- 2014 ACM省赛总结
今年ACM省赛已经过去一个星期左右了,2年的ACM训练是该做个总结了,因为前几日去參加蓝桥杯总决赛,所以没来的及写总结,如今在这小小总结一下吧-- 依晰记得去年省赛时候的样子,如今感觉那时像是个无知的 ...
- SqlHelper数据库访问类
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.D ...
- PHP定义数组常量
最先想到的方法是这样: define('SIGN_CODE', array('9df512','59gf1g','5eg7h1','g1agf5','f5e151','g51gfr','a5481s' ...
- js 刷新网页
1. Javascript 返回上一页history.go(-1), 返回两个页面: history.go(-2); 2. history.back(). 3. window.history.forw ...
- SQL Server数据恢复——日志备份
太坑了,我把数据给删了 “大坑啊,数据被我误删了.”从事数据库相关工作的过程中,我想应该很多人会有过和我一样的遭遇吧?尤其是在进行update或者delete操作的时候,忘记了where条件.这些毁灭 ...
- Win32汇编开始 Hello Asm
今天开始学习Win32汇编 因为自己很多都是Windows方面 所以 接触一下Win32汇编 . ;.386指令集 .model flat,stdcall ;工作模式 option casemap:n ...
- n多边形面积
“ 改革春风吹满地,不会AC没关系;实在不行回老家,还有一亩三分地.谢谢!(乐队奏乐)” 话说部分学生心态极好,每天就知道游戏,这次考试如此简单的题目,也是云里雾里,而且,还竟然来这么几句打油诗.好呀 ...
- Web应用的部署
本文将介绍一些Web应用的部署: 部署规则 要成功地部署一个Web应用,必须遵循以下目录结构. 1.WEB-INF一定要直接放到应用上下文(Webapp)之下. 2.classes目录必须直接放在WE ...
- 动手编写插件-javascript分页插件
原来公司用的报表分页插件是C#编写的服务器插件,需要前后台交互,而且不支持ajax. 经过一段时间折腾,我编写了一个轻便的jquery分页插件,支持ajax.下面是插件代码 /* 插件名称:报表分页 ...