Servlet做简单的ajax增删改查(分页)
jdbc.java
package servlet; import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement; public class Jdbc {
Connection conn;
Statement stem;
ResultSet re;
/*
* jdbc五步走:
* 1:加载驱动
* 2:创建连接
* 2.1:地址
* 2.2:用户名 root
* 2.3:密码 123
* 3:创建发送执行sql语句对象
* 4:发送执行sql语句
* 5:操作结果集
*/ private void lianjie() {
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/guoyihua", "root", "123");
stem = conn.createStatement();
} catch (Exception e) {
e.printStackTrace();
}
} private void guanbi() {
try {
if (re!=null) {
re.close();
} if (stem!=null) {
stem.close();
}
if (conn!=null) {
conn.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} /*
* 创建一个应用于任何查询的show方法。
* 但凡查询功能一定返回一个结果集
*/
public ResultSet show ( String sql ){
this.lianjie();
try {
re=stem.executeQuery(sql);
} catch (SQLException e) {
}
return re;
} public int update ( String sql ){
this.lianjie();
int i;
try {
i = stem.executeUpdate(sql);
this.guanbi();
return i;
} catch (SQLException e) {
}
return 0;
} }
UserService.java
package servlet; import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random; public class UserService {
Jdbc jdbc= new Jdbc(); int page=2; public List<Map<String, Object>> show(String ye) {
int yee = Integer.parseInt(ye);
yee=(yee-1)*page;
List<Map<String, Object>> list =new ArrayList<Map<String,Object>>();
ResultSet show = jdbc.show("select * from user limit "+yee+" , "+page+" ");
try {
while (show.next()) {
Map<String,Object> map = new HashMap<String, Object>();
map.put("id", show.getInt("id"));
map.put("name", show.getString("name"));
map.put("password", show.getLong("password"));
list.add(map);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} return list;
} public void deletee(String id) {
jdbc.update("delete from user where id='"+id+"' ");
} public Map<String, Object> toupdate(String id) {
Map<String, Object> map = new HashMap<String, Object>();
ResultSet re = jdbc.show("select * from user where id='"+id+"'");
try {
while (re.next()) {
map.put("id", re.getInt("id"));
map.put("name", re.getString("name"));
map.put("password", re.getLong("password"));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return map;
} public void update(String id, String name, String password) {
jdbc.update("update user set name='"+name+"',password='"+password+"'where id='"+id+"' ");
} public void add(String name, String password) {
Random random = new Random();
int id = random.nextInt(1000);
jdbc.update("insert into user (id,name,password) values('"+id+"','"+name+"','"+password+"')");
} public int tablecount() {
int tablecount=0;
int count=0;
ResultSet re = jdbc.show("select count(*) from user ");
try {
while (re.next()) {
tablecount = re.getInt("count(*)");
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(tablecount%page==0){
count = tablecount/page;
}
if (tablecount%page!=0) { count = tablecount/page+1;
}
return count;
} }
UserServlet.java
package servlet; import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map; import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; public class UserServlet extends HttpServlet{
List<Map<String, Object>> list= new ArrayList<Map<String,Object>>();
UserService us = new UserService();
HttpServletRequest request;
HttpServletResponse response;
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.request= request;
this.response= response;
String me = request.getParameter("method");
if (me.equals("show")) {
this.show();
}
if (me.equals("deletee")) {
this.deletee();
}
if (me.equals("toupdate")) {
this.toupdate();
}
if (me.equals("update")) {
this.update();
}if (me.equals("add")) {
this.add();
}
}
private void add() throws IOException {
String name = request.getParameter("name");
name=new String(name.getBytes("ISO8859-1"), "UTF-8");
String password = request.getParameter("password");
us.add(name,password);
response.getWriter().print("<script type=\"text/javascript\">parent.show(1)</script>");
}
private void update() throws IOException {
String id = request.getParameter("id");
String name = request.getParameter("name");
String password = request.getParameter("password");
name= new String(name.getBytes("ISO8859-1"), "UTF-8");
us.update(id,name,password);
response.getWriter().print("<script type=\"text/javascript\">parent.show(1)</script>");
}
private void toupdate() throws ServletException, IOException {
String id = request.getParameter("id");
Map<String, Object> map = us.toupdate(id);
request.setAttribute("map", map);
request.getRequestDispatcher("update.jsp").forward(request, response);
}
private void deletee() throws ServletException, IOException {
String id = request.getParameter("id");
us.deletee(id);
this.show();
}
private void show() throws ServletException, IOException {
String ye = request.getParameter("ye");
if (ye==null) {
ye="1";
}
List<Map<String, Object>> list=us.show(ye);
request.setAttribute("li", list);
int count=us.tablecount();
request.setAttribute("count", count);
request.getRequestDispatcher("show.jsp").forward(request, response); } }
index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
--> </head>
<script type="text/javascript" src="jquery-1.6.js"></script>
<script type="text/javascript"> function show(ye){
var d= new Date().getTime();
$.get("aa?method=show&ye="+ye+"&d="+d,function(date){
$("#div1").html(date)
})
} function deletee(id){
var d=new Date().getTime();
$.get("aa?method=deletee&id="+id+"&d="+d,function(date){
$("#div1").html(date);
})
} function toupdate(id){
$.get("aa?method=toupdate&id="+id,function(date){
$("#div2").html(date);
})
}
function update(a){
a.submit();
$(a).hide();
}
function toadd(){
$.get("add.jsp",function(date){
$("#div3").html(date);
})
}
function add(a) {
a.submit();
$(a).hide();
}
</script> <body>
<a href="javascript:show(1)">查询user表</a>
<div id="div1"></div>
<div id="div2"></div>
<div id="div3"></div>
</body>
</html>
show.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>"> <title>My JSP 'show.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
--> </head> <body>
<table bgcolor="c0c0c0" bordercolor="green" cellspacing="1">
<tr bordercolor="green">
<td>编号</td>
<td>姓名</td>
<td>密码</td>
<td>修改</td>
<td>删除</td>
</tr>
<c:forEach items="${requestScope.li}" var="list">
<tr>
<td>${list.id}</td>
<td>${list.name}</td>
<td>${list.password}</td>
<td><a href="javascript:toupdate(${list.id})">修改</a></td>
<td><a href="javascript:deletee(${list.id})">删除</a></td>
</tr>
</c:forEach>
</table> <c:if test="${param.ye>1}">
<a href="javascript:show(1)">首页</a>
</c:if>
<c:if test="${param.ye>1}">
<a href="javascript:show(${param.ye-1})">上一页</a>
</c:if>
<c:forEach begin="1" end="${requestScope.count}" varStatus="c">
<a href="javascript:show(${c.index})">${c.index}</a>
</c:forEach>
<c:if test="${param.ye<requestScope.count}">
<a href="javascript:show(${param.ye+1})">下一页</a>
</c:if>
<c:if test="${param.ye<requestScope.count}">
<a href="javascript:show(${requestScope.count})">尾页</a>
</c:if> <a href="javascript:toadd()"> 添加 </a>
</body>
</html>
add.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>"> <title>My JSP 'add.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
--> </head> <body>
<form action="aa" target="abc">
<h3>请输入以下内容</h3>
<input type="hidden" name="id">
<input type="hidden" name="method" value="add" >
请输入姓名<input type="text" name="name" ><br/><br/>
请输入密码<input type="text" name="password" ><br/><br/>
<input type="button" value="确认添加" onclick="javascript:add(this.form)">
<iframe name="abc" style="display: none;" frameborder="1"></iframe>
</form>
</body>
</html>
update.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>"> <title>My JSP 'update.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
--> </head> <body>
<form action="aa" target="abc">
<h3>请修改以下内容</h3>
<input type="hidden" name="method" value="update">
<input type="hidden" name="id" value="${map.id}"><br/><br/>
<input type="text" name="name" value="${map.name}"><br/><br/>
<input type="text" name="password" value="${map.password}"><br/><br/>
<input type="button" value="确认修改" onclick="javascript:update(this.form)">
<iframe name="abc" style="display: none;"></iframe>
</form>
</body>
</html>
此文章仅为个人学习记录文件,为个人所记笔记。
可供大家参考
但是注释甚少
如有疑问可以留言
希望可以帮助到初学者
2017-08-1119:53:30
Servlet做简单的ajax增删改查(分页)的更多相关文章
- salesforce 零基础开发入门学习(六)简单的数据增删改查页面的构建
VisualForce封装了很多的标签用来进行页面设计,本篇主要讲述简单的页面增删改查.使用的内容和设计到前台页面使用的标签相对简单,如果需要深入了解VF相关知识以及标签, 可以通过以下链接查看或下载 ...
- 【转载】salesforce 零基础开发入门学习(六)简单的数据增删改查页面的构建
salesforce 零基础开发入门学习(六)简单的数据增删改查页面的构建 VisualForce封装了很多的标签用来进行页面设计,本篇主要讲述简单的页面增删改查.使用的内容和设计到前台页面使用的 ...
- 【Mybatis】简单的mybatis增删改查模板
简单的mybatis增删改查模板: <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE map ...
- 最简单的mybatis增删改查样例
最简单的mybatis增删改查样例 Book.java package com.bookstore.app; import java.io.Serializable; public class Boo ...
- SpringMVC4+MyBatis3+SQLServer 2014 整合(包括增删改查分页)
前言 说起整合自然离开ssm,我本身并不太喜欢ORM,尤其是MyBatis,把SQL语句写在xml里,尤其是大SQL,可读性不高,出错也不容易排查. 开发环境 idea2016.SpringMVC4. ...
- node-express项目的搭建并通过mongoose操作MongoDB实现增删改查分页排序(四)
最近写了一个用node来操作MongoDB完成增.删.改.查.排序.分页功能的示例,并且已经放在了服务器上地址:http://39.105.32.180:3333. Mongoose是在node.js ...
- OracleHelper(对增删改查分页查询操作进行了面向对象的封装,对批量增删改操作的事务封装)
公司的一个新项目使用ASP.NET MVC开发,经理让我写个OracleHelper,我从网上找了一个比较全的OracleHelper类,缺点是查询的时候返回DataSet,数据增删改要写很多代码(当 ...
- web项目总结——通过jsp+servlet实现对oracle的增删改查功能
1.DAO模式 分包:依次建立 entity:实体包,放的是跟oracle数据库中表结构相对应的对象的属性,也就是这个对象有什么 dao:增删改查接口,实现增删改查的具体方法 service:同dao ...
- jsp+Servlet+JavaBean+JDBC+MySQL项目增删改查
1简单的Mvc,分层建包. java resources src/mian/java (1)dao 包 JDBC连接类,连接数据库.增删改查方法,其他的方法. (2)model包 实体类,数据库字段, ...
随机推荐
- JavaScript 创建一个 form 表单并提交
<!DOCTYPE HTML> <html lang="en-US"> <head> <meta charset="UTF-8& ...
- 3.VBScript基础
1.VBS只有一种数据类型 ->Variant类似于泛类型,其中具体类型会在调用的时候具体化 2.声明变量可以用Dim语句,Public语句,Private语句 声明多个变量用逗号分隔 也可以隐 ...
- 正确、安全地停止SpringBoot应用服务
引言 Spring Boot,作为Spring框架对"约定优先于配置(Convention Over Configuration)"理念的最佳实践的产物,它能帮助我们很快捷的创建出 ...
- Shiro固定身份验证
Shiro基础身份验证 如果要进行shiro的日志信息读取,那么需要使用一个org.apache.shiro.util.Factory接口,在这个接口里面定义有一 取得SecuruityManager ...
- Watson Conversation Service Implementation Methodology
Watson Conversation Service Implementation Methodology In order to implement the WCS successfully. Y ...
- gulp使用流程
1.全局安装gulp $ npm install --global gulp 2.作为项目的开发依赖(devDependencies)安装 $ npm install --save-dev gulp ...
- Socket异步通信及心跳包同时响应逻辑分析。
有段时间没有更博了,刚好最近在做Socket通信的项目,原理大致内容:[二维码-(加logo)]-->提供主机地址和端口号信息(直接使用[ThoughtWorks.QRCode.dll]比较简单 ...
- HDU 1814 Peaceful Commission / HIT 1917 Peaceful Commission /CJOJ 1288 和平委员会(2-sat模板题)
HDU 1814 Peaceful Commission / HIT 1917 Peaceful Commission /CJOJ 1288 和平委员会(2-sat模板题) Description T ...
- JS封闭函数、闭包、内置对象
一.变量作用域 变量作用域指的是变量的作用范围,javascript中的变量分为全局变量和局部变量 1.全局变量:在函数之外定义的变量,为整个页面公用,函数的内部外部都可以访问. 2.局部变量:在函数 ...
- CSS 样式书写规范+特殊符号
虽然我只是刚踏入web前端开发圈子.在一次次任务里头,我发觉每一次的css命名都有所不同和不知所措.脑海就诞生了一个想法--模仿大神的css命名样式. 毕竟日后工作上,是需要多个成员共同协作的.如果没 ...