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 ...
随机推荐
- javascript第十七课:this使用
例如,我们要一个元素的值 function f1(){ alert(this.id); } document.getElementByid('#id').onclick=f1; //将函数赋值给事件
- 卓尼斯ZT-180评測
卓尼斯ZT-180评測 ——正在出差途中,用10”上网本发帖,没有拍照,且写得冲忙,不妥之处见谅. 一.採购 1.因外出旅游,不想带那台14"笔记本,所以想买一台平板电脑.当时,选择的 ...
- Devexpress之barManager控件属性
隐藏菜单栏左边的竖线和右边的箭头? 1.隐藏菜单栏上右边的箭头属性设置:OptionsBar=>>AllowQuickCustomization=False 2.隐藏菜单栏左边的竖线属性设 ...
- html加载js那些事
浏览器通过内置的JavaScript引擎,读取网页中的代码,对其处理后运行. JavaScript代码嵌入网页的方法 在网页中嵌入JavaScript代码有多种方法. 直接添加代码块 通过script ...
- android开发MD5加密工具类(一)
MD5加密工具类整理: package com.gzcivil.utils; import java.io.UnsupportedEncodingException; import java.secu ...
- How to recover a skipped tablespace after an incomplete recovery with resetlogs? [ID 1561645.1]
n this Document Goal Solution This document is being delivered to you via Oracle Support's Rapid ...
- Ubuntu 12.04 下安装git
---恢复内容开始--- 1.安装build-essential. 列出Git相关包(git-core 和 git-doc)所以来的各个安装包并安装: sudo apt-get build-dep g ...
- PS切图保存后的背景图为透明
1.若想PS切图保存后的背景图为透明,那么则需要在如下图中所示的修改即可,切图后[文件]——>[存储为web格式]——>[PNG-24]: 2.要想在css中的背景图片为相通,则先剪切一个 ...
- Smarty3配置
下载Smarty压缩包并解压,复制其中的libs文件夹到我们的PHP工程目录下(可将其改名为smarty).同时,在工程目录下新建三个文件夹,分别取名为templates.templates_c和sm ...
- div模拟textarea以实现高度自适应实例页面
作为多行文本域功能来讲,textarea满足了我们大部分的需求.然而,textarea有一个不足就是不能像普通div标签一样高度可以跟随内容自适应.textarea总是很自信地显摆它的滚动条,高度固执 ...