jsp 增删改查
CREATE TABLE `david2018_db`.`User` (
`UserId` INT NOT NULL AUTO_INCREMENT,
`UserName` NVARCHAR(30) NULL,
`PassWord` NVARCHAR(30) NULL,
`Age` INT NULL,
`Sex` NVARCHAR(30) NULL,
PRIMARY KEY (`UserId`));
insert into david2018_db.User (UserName,PassWord,Age,Sex) values ('武大郎','123456',35,'男');
insert into david2018_db.User (UserName,PassWord,Age,Sex) values ('潘金莲','123456',18,'女');
insert into david2018_db.User (UserName,PassWord,Age,Sex) values ('西门庆','123456',24,'男');
insert into david2018_db.User (UserName,PassWord,Age,Sex) values ('武松','123456',22,'男');
insert into david2018_db.User (UserName,PassWord,Age,Sex) values ('鲁智深','123456',30,'男');
insert into david2018_db.User (UserName,PassWord,Age,Sex) values ('林冲','123456',32,'男');
public class User {
private int UserId;
private String UserName;
private String PassWord;
private int Age;
private String Sex; public int getUserId() {
return UserId;
}
public void setUserId(int UserId) {
this.UserId = UserId;
}
public String getUserName() {
return UserName;
}
public void setUserName(String UserName) {
this.UserName = UserName;
}
public String getPassWord() {
return PassWord;
}
public void setPassWord(String PassWord) {
this.PassWord = PassWord;
}
public int getAge() {
return Age;
}
public void setAge(int Age) {
this.Age = Age;
}
public String getSex() {
return Sex;
}
public void setSex(String Sex) {
this.Sex = Sex;
}
}
编写工具类 获得数据库连接 utils包下创建DBUtil类
public class DBUtil {
public static BasicDataSource dataSource = new BasicDataSource(); static {
try {
Properties pp = new Properties();
InputStream is = DBUtil.class.getClassLoader().getResourceAsStream("database.properties");
pp.load(is);
String driver = pp.getProperty("driverName");
String url = pp.getProperty("url");
String username = pp.getProperty("user");
String password = pp.getProperty("password"); dataSource.setDriverClassName(driver);
dataSource.setUrl(url);
dataSource.setUsername(username);
dataSource.setPassword(password);
dataSource.setInitialSize(10);//初始化连接数
dataSource.setMaxIdle(5); //最大空闲
dataSource.setMinIdle(1); //最小空闲 } catch (Exception e) {
e.printStackTrace();
}
} public static BasicDataSource GetDataSource(){
return dataSource;
} }
//获取全部列表
public List<User> GetAllUser() throws SQLException {
String sql = "select * from User";
QueryRunner qr = new QueryRunner(DBUtil.GetDataSource());
List<User> users = qr.query(sql,new BeanListHandler<User>(User.class));
return users;
}
public List<User> GetAllService() throws SQLException {
UserDao dao = new UserDao();
return dao.GetAllUser();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
UserService service = new UserService();
List<User> userList = null;
try {
userList = service.GetAllService(); } catch (SQLException e) {
e.printStackTrace();
}
request.setAttribute("userList", userList);
request.getRequestDispatcher("userList.jsp").forward(request,response);
}
<servlet>
<servlet-name>UserList</servlet-name>
<servlet-class>com.david.web.UserList</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>UserList</servlet-name>
<url-pattern>/userList</url-pattern>
</servlet-mapping>
<%@ page import="com.david.domain.User" %>
<%@ page import="java.util.List" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>用户列表</title>
</head>
<body>
<a href="AddUser.jsp">添加用户</a>
<table>
<tr>
<th>用户ID</th>
<th>用户名</th>
<th>用户密码</th>
<th>用户年龄</th>
<th>用户性别</th>
<th>操作</th>
</tr>
<%
if (request.getAttribute("userList") != null) {
%>
<%for (User u : (List<User>) request.getAttribute("userList")) {%>
<tr>
<td><%=u.getUserId()%>
</td>
<td><%=u.getUserName()%>
</td>
<td><%=u.getPassWord()%>
</td>
<td><%=u.getAge()%>
</td>
<td><%=u.getSex()%>
</td>
<td>
<a href="GetUserById?userId=<%=u.getUserId()%>">修改</a>
<a href="DeleteUser?userId=<%=u.getUserId()%>">删除</a>
</td>
</tr>
<%}%>
<%}%>
</table>
</body>
</html>
//添加用户
public boolean AddUser(User user) throws SQLException {
String sql = "insert into User (UserName,PassWord,Age,Sex) values (?,?,?,?)";
QueryRunner qr = new QueryRunner(DBUtil.GetDataSource());
Object[] params = {user.getUserName(),user.getPassWord(),user.getAge(),user.getSex()};
int result = qr.update(sql,params);
return result > 0;
}
public boolean AddUser(User user) throws SQLException {
UserDao dao = new UserDao();
return dao.AddUser(user);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
String userName = request.getParameter("userName");
String passWord = request.getParameter("passWord");
String age = request.getParameter("age");
String sex = request.getParameter("sex"); User u = new User();
u.setUserName(userName);
u.setPassWord(passWord);
u.setAge(Integer.parseInt(age));
u.setSex(sex); UserService users = new UserService();
try {
boolean result = users.AddUser(u);
if(result){
response.sendRedirect("userList");
}else{ }
} catch (SQLException e) {
e.printStackTrace();
}
}
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>添加用户</title>
</head>
<body>
<form method="post" action="AddUser">
<p>用户名:<input name="userName"></p>
<p>密码:<input name="passWord"></p>
<p>性别:
<input type="radio" checked value="男" name="sex">男
<input type="radio" value="女" name="sex">女
</p>
<p>年龄:<select name="age">
<%for (int i = 18; i < 50; i++) {%>
<option value="<%=i%>"><%=i%></option>
<%}%>
</select></p>
<p>
<button type="submit">添加</button>
</p>
<a href="userList">用户列表</a>
</form> </body>
</html>
<servlet>
<servlet-name>AddUser</servlet-name>
<servlet-class>com.david.web.AddUser</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>AddUser</servlet-name>
<url-pattern>/AddUser</url-pattern>
</servlet-mapping>
//删除用户
public boolean DeleteUser(int userId) throws SQLException {
String sql = "delete from User where UserId = ?";
QueryRunner qr = new QueryRunner(DBUtil.GetDataSource());
return qr.update(sql,userId) > 0;
}
public boolean DeleteUser(int userId) throws SQLException {
UserDao dao = new UserDao();
return dao.DeleteUser(userId);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String userId = request.getParameter("userId");
UserService service = new UserService();
int id = Integer.parseInt(userId);
try {
boolean result = service.DeleteUser(id);
if(result){
response.sendRedirect("userList");
}
} catch (SQLException e) {
e.printStackTrace();
}
}
<servlet>
<servlet-name>DeleteUser</servlet-name>
<servlet-class>com.david.web.DeleteUser</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>DeleteUser</servlet-name>
<url-pattern>/DeleteUser</url-pattern>
</servlet-mapping>
<a href="DeleteUser?userId=<%=u.getUserId()%>">删除</a>
//查找一条User通过UserId
public User GetUserByUserId(int userId) throws SQLException {
String sql = "select * from User where UserId = ?";
QueryRunner qr = new QueryRunner(DBUtil.GetDataSource());
User user = qr.query(sql, userId, new BeanHandler<User>(User.class));
return user;
}
//修改user
public boolean UpdateUser(User user) throws SQLException {
String sql = "update User set UserName = ?,PassWord = ?,Age = ?, Sex = ? where UserId = ?";
Object [] params = {user.getUserName(),user.getPassWord(),user.getAge(),user.getSex(),user.getUserId()};
QueryRunner qr = new QueryRunner(DBUtil.GetDataSource());
int result = qr.update(sql,params);
return result > 0;
}
public User GetUserByUserId(int userId) throws SQLException {
UserDao dao = new UserDao();
return dao.GetUserByUserId(userId);
}
//修改user
public boolean UpdateUser(User user) throws SQLException {
UserDao dao = new UserDao();
return dao.UpdateUser(user);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String userId = request.getParameter("userId");
UserService service = new UserService();
try {
User u =service.GetUserByUserId(Integer.parseInt(userId));
request.setAttribute("user",u);
request.getRequestDispatcher("Update.jsp").forward(request,response);
} catch (SQLException e) {
e.printStackTrace();
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
String userId = request.getParameter("userId");
String userName = request.getParameter("userName");
String passWord = request.getParameter("passWord");
String age = request.getParameter("age");
String sex = request.getParameter("sex"); User u = new User();
u.setUserName(userName);
u.setPassWord(passWord);
u.setAge(Integer.parseInt(age));
u.setSex(sex);
u.setUserId(Integer.parseInt(userId)); UserService users = new UserService();
try {
boolean result = users.UpdateUser(u);
if (result) {
response.sendRedirect("userList");
} else { }
} catch (SQLException e) {
e.printStackTrace();
}
}
<a href="GetUserById?userId=<%=u.getUserId()%>">修改</a>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>修改用户</title>
</head>
<body>
<%
User u = (User)request.getAttribute("user");
%>
<form method="post" action="UpdateUser">
<p>用户Id:<input name="userId" value="<%=u.getUserId()%>" readonly></p>
<p>用户名:<input name="userName" value="<%=u.getUserName()%>"></p>
<p>密码:<input name="passWord" value="<%=u.getPassWord()%>"></p>
<p>性别:
<input type="radio" <%if(u.getSex().equals("男")){%> checked <%}%> value="男" name="sex">男
<input type="radio" <%if(u.getSex().equals("女")){%> checked <%}%> value="女" name="sex">女
</p>
<p>年龄:<select name="age">
<%for (int i = 18; i < 50; i++) {%>
<option value="<%=i%>"><%=i%></option>
<%}%>
</select>
<script>
for(var i = 0;i<document.getElementsByTagName("option").length;i++){
var option = document.getElementsByTagName("option")[i];
if(option.value == "<%=u.getAge()%>"){
option.selected = true;
}
}
</script>
</p>
<p>
<button type="submit">确认修改</button>
</p>
<a href="userList">用户列表</a>
</form> </body>
</html>
<servlet>
<servlet-name>UpdateUser</servlet-name>
<servlet-class>com.david.web.UpdateUser</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>UpdateUser</servlet-name>
<url-pattern>/UpdateUser</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>GetUserById</servlet-name>
<servlet-class>com.david.web.GetUserById</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>GetUserById</servlet-name>
<url-pattern>/GetUserById</url-pattern>
</servlet-mapping>
jsp 增删改查的更多相关文章
- jqGrid(struts2+jdbc+jsp)增删改查的例子
前几日一直在找关于Java操作jqgrid返回json的例子,在网上也看了不少东西,结果都没几个合理的,于是本人结合网上的零散数据进行整理,完成了 一个比较完整的jqgrid小例子,考虑到还有很多 ...
- easyui datagrid 增删改查示例
查询JSP页面 <!doctype html> <%@include file="/internet/common.jsp"%> <!-- 新样式右侧 ...
- 使用Servlet编写增删改查
第一步创建一个表 1 create database liyongzhendb default character set utf8 collate utf8_bin; 2 3 CREATE TABL ...
- 最简单的jsp+servlet的增删改查代码
package ceet.ac.cn.dao; import java.sql.Connection; import java.sql.PreparedStatement; import java.s ...
- web项目总结——通过jsp+servlet实现对oracle的增删改查功能
1.DAO模式 分包:依次建立 entity:实体包,放的是跟oracle数据库中表结构相对应的对象的属性,也就是这个对象有什么 dao:增删改查接口,实现增删改查的具体方法 service:同dao ...
- 数据库中的记录通过servlet回显到jsp页面中(连接数据库或者查询參照:对数据进行增删改查)
我们常常会用到通过图书的名称来查询图书那么这种话我们也就会使用到从数据库中搜索出数据而且载入到自己的Jsp页面中 这种话我们须要将从数据库中获取到的数据放进响应中然后通过%=request.getAt ...
- mvc模式jsp+servel+dbutils oracle基本增删改查demo
mvc模式jsp+servel+dbutils oracle基本增删改查demo 下载地址
- mvc模式jsp+servel+jdbc oracle基本增删改查demo
mvc模式jsp+servel+jdbc oracle基本增删改查demo 下载地址
- JDBC+Servlet+JSP的学生案例增删改查
数据库信息传输到页面实现. 先进行学生信息页面展示: 接口IStudentDao public interface IStudentDao { /** * 保存操作 * @param stu 学生对象 ...
随机推荐
- 【翻译】EXTJS 编码风格指南与实例
原文:EXTJS Code Style Guide with examples Ext JS风格指南: 熟知的且易于学习 快速开发,易于调试,轻松部署 组织良好.可扩展和可维护 Ext JS应用程序的 ...
- Camera 涉及的文件70
点击打开链接 Camera 涉及的文件 1.上电时序 Mediate/custom/$PROJECT/Camera/Camera/kd_camera_hw.c Mediate/custom/$PROJ ...
- 不用局部变量实现C语言两数交换算法
关于交换算法,我想非常简单,所以,这次不做分析,直接上代码: #include <stdio.h> #include <stdlib.h> //用异或方式实现 void swa ...
- LeetCode之“散列表”:Two Sum && 3Sum && 3Sum Closest && 4Sum
1. Two Sum 题目链接 题目要求: Given an array of integers, find two numbers such that they add up to a specif ...
- 第一个Polymer应用 - (4)收尾工作
原文链接: Step 4: Finishing touches翻译日期: 2014年7月8日翻译人员: 铁锚在本节中,会在卡片上添加收藏按钮,并可以通过切换选项卡(tabs)连接到不同的 <po ...
- Shell Scripts - 条件语句,case语句,function功能
修改之前的代码 1.判断 $1 是否为 hello,如果是的话,就显示 "Hello, how are you ?": 2.如果没有加任何参数,就提示使用者必须要使用的参数 ...
- 粒子滤波(PF:Particle Filter)
先介绍概念:来自百科 粒子滤波指:通过寻找一组在状态空间中传播的随机样本来近似的表示概率密度函数,再用样本均值代替积分运算,进而获得系统状态的最小方差估计的过程,波动最小,这些样本被形象的称为&quo ...
- Nginx安装Nginx-echo模块
Nginx-echo可以在Nginx中用来输出一些信息,是在测试排错过程中一个比较好的工具.它也可以做到把来自不同链接地址的信息进行一个汇总输出.总之能用起来可以给开发人员带来挺大帮助的.下面看看我们 ...
- python MultiProcessing标准库使用Queue通信的注意要点
今天原本想研究下MultiProcessing标准库下的进程间通信,根据 MultiProcessing官网 给的提示,有两种方法能够来实现进程间的通信,分别是pipe和queue.因为看queue顺 ...
- 《深入理解java虚拟机》读书笔记1--java内存区域
Java内存管理 本文主要介绍Java虚拟机运行时的内存区域是如何划分的.Java对象的创建过程.Java对象的内存布局.Java对象的访问定位 一:运行时区域划分 主要可以分为以下 几个: 程序计数 ...