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包 实体类,数据库字段, ...
随机推荐
- Itunes制作手机铃声,图文版
一.下载歌曲,选择歌曲用itunes打开,打开出现下面界面 二.设置歌曲 右键点击歌曲,找到显示简介,点击选项,截取音乐,出现下图: 截取你喜欢的部分,点击确定 点击确定后,选中该歌曲,找到左上方 文 ...
- <canvas合成海报>所遇问题及解决方案总结
最近做了一个用canvas合成海报图片的移动端项目,由于一点canvas基础都没有,所以去网上搜了一位前辈的demo,但是开发过程中遇到了很多问题,现将所遇问题及解决方法总结如下: 1.移动端canv ...
- OpenCV学习3-----利用鼠标、键盘回调函数实现标定人体关节点
最近做实验,需要一些人体关节点的ground truth,需要自己手动标定,于是尝试使用OpenCV的鼠标键盘回调函数实现. 期间遇到不少问题,记录一下. 首先就是鼠标回调函数注册, namedWin ...
- 【Android Developers Training】 97. 序言:访问通讯录数据
注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好. 原文链接:http://developer ...
- 【Android Developers Training】 64. 绘制形状
注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好. 原文链接:http://developer ...
- MySQL实例搭建
Q:如何判断一个Linux系统具备安装MySQL的条件? A: 1.Linux网络已经配置完成 ip地址/子网掩码.默认网关.主机名字 /etc/hosts:访问这个数据库的应用的IP地址和主机名字也 ...
- 深入理解javascript异步编程障眼法&&h5 web worker实现多线程
0.从一道题说起 var t = true; setTimeout(function(){ t = false; }, 1000); while(t){ } alert('end'); 1 2 3 4 ...
- python爬虫从入门到放弃(一)之初识爬虫
整理这个文档的初衷是自己开始学习的时候没有找到好的教程和文本资料,自己整理一份这样的资料希望能对小伙伴有帮助 什么是爬虫? 网络爬虫(又被称为网页蜘蛛,网络机器人,在FOAF社区中间,更经常的称为网页 ...
- (转)log4j(六)——log4j.properties简单配置样例说明
一:测试环境与log4j(一)——为什么要使用log4j?一样,这里不再重述 1 老规矩,先来个栗子,然后再聊聊感受 (1)使用配文件的方式,是不是感觉非常的清爽,如果不在程序中读取配置文件就更加的清 ...
- Spring boot——logback 基础使用篇(一)
1 简单日志配置 spring boot内部使用Commons Logging来记录日志,但也保留外部接口可以让一些日志框架来进行实现,例如Java Util Logging,Log4J2还有Logb ...