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增删改查(分页)的更多相关文章

  1. salesforce 零基础开发入门学习(六)简单的数据增删改查页面的构建

    VisualForce封装了很多的标签用来进行页面设计,本篇主要讲述简单的页面增删改查.使用的内容和设计到前台页面使用的标签相对简单,如果需要深入了解VF相关知识以及标签, 可以通过以下链接查看或下载 ...

  2. 【转载】salesforce 零基础开发入门学习(六)简单的数据增删改查页面的构建

    salesforce 零基础开发入门学习(六)简单的数据增删改查页面的构建   VisualForce封装了很多的标签用来进行页面设计,本篇主要讲述简单的页面增删改查.使用的内容和设计到前台页面使用的 ...

  3. 【Mybatis】简单的mybatis增删改查模板

    简单的mybatis增删改查模板: <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE map ...

  4. 最简单的mybatis增删改查样例

    最简单的mybatis增删改查样例 Book.java package com.bookstore.app; import java.io.Serializable; public class Boo ...

  5. SpringMVC4+MyBatis3+SQLServer 2014 整合(包括增删改查分页)

    前言 说起整合自然离开ssm,我本身并不太喜欢ORM,尤其是MyBatis,把SQL语句写在xml里,尤其是大SQL,可读性不高,出错也不容易排查. 开发环境 idea2016.SpringMVC4. ...

  6. node-express项目的搭建并通过mongoose操作MongoDB实现增删改查分页排序(四)

    最近写了一个用node来操作MongoDB完成增.删.改.查.排序.分页功能的示例,并且已经放在了服务器上地址:http://39.105.32.180:3333. Mongoose是在node.js ...

  7. OracleHelper(对增删改查分页查询操作进行了面向对象的封装,对批量增删改操作的事务封装)

    公司的一个新项目使用ASP.NET MVC开发,经理让我写个OracleHelper,我从网上找了一个比较全的OracleHelper类,缺点是查询的时候返回DataSet,数据增删改要写很多代码(当 ...

  8. web项目总结——通过jsp+servlet实现对oracle的增删改查功能

    1.DAO模式 分包:依次建立 entity:实体包,放的是跟oracle数据库中表结构相对应的对象的属性,也就是这个对象有什么 dao:增删改查接口,实现增删改查的具体方法 service:同dao ...

  9. jsp+Servlet+JavaBean+JDBC+MySQL项目增删改查

    1简单的Mvc,分层建包. java resources src/mian/java (1)dao 包 JDBC连接类,连接数据库.增删改查方法,其他的方法. (2)model包 实体类,数据库字段, ...

随机推荐

  1. Notepad++中过滤掉的正则方式

    2 => 'ashadv'3 => 'aogro'4 => 'aogs'5 => 'ashamw'6 => 'arc'8 => 'gtsatq'9 => 'b ...

  2. js中的confirm的运用

    <a href="javascript:void(0);" onclick='tuichu_queren();'>安全退出</a> <script t ...

  3. Chapter 2. Video Formats and Quality

    本章节主要介绍一些视频格式相关的基础知识. 交织(Interlace) 即每一个采样帧采样时隔行采样,奇数行和偶数行交替. YCbCr 人眼视觉系统(Human Visual System, HVS) ...

  4. JAVA基础——异常详解

    JAVA异常与异常处理详解 一.异常简介 什么是异常? 异常就是有异于常态,和正常情况不一样,有错误出错.在java中,阻止当前方法或作用域的情况,称之为异常. java中异常的体系是怎么样的呢? 1 ...

  5. RabbitMQ系列教程之五:主题(Topic)

    (本实例都是使用的Net的客户端,使用C#编写),说明,中文方括号[]表示名词.   在上一个教程中,我们改进了我们的日志记录系统. 没有使用只能够进行虚拟广播的[Fanout]交换机,而是使用了[D ...

  6. 致命错误:mysql/cli 目录 #include "mysql/client_plugin.h"

    居然说没有mysql.h这个文件,可是我确实安装了mysql了啊.......  原来是缺少libmysqlclient-dev,OK安装就是了 ubuntu下  :  audo apt-get in ...

  7. Spring 加载静态资源

    <mvc:default-servlet-handler/> JSP 中通过标签加载js文件或者link标签加载css文件等静态资源时要在springmvc的xml文件中配置以上设置请求就 ...

  8. input复选框操作的部分高频率使用代码

    1. 获取单个checkbox选中项(三种写法): $("input:checkbox:checked").val() 或者 $("input:[type='checkb ...

  9. 构建高并发&高可用&安全的IT系统-高并发部分

    什么是高并发? 狭义来讲就是你的网站/软件同一时间能承受的用户数量有多少 相关指标有 并发数:对网站/软件同时发起的请求数,一般也可代表实际的用户 每秒响应时间:常指一次请求到系统正确响的时间(以秒为 ...

  10. Haproxy的配置

    1,下载Haproxy 下载Haproxy 1.6  2,安装haproxy uname -r cd /usr/local/src/haproxy-1.6.9/ make TARGET=linux31 ...