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包 实体类,数据库字段, ...
随机推荐
- ThreadLocal经典分页
package com.netease.live.admin.util; import com.netease.live.common.util.Constant; /** * * @author b ...
- ListView 介绍
1. 通过继承Activity实现ListView 1.1 在XML布局文件中实现一个ListView <ListView android:layout_width="wrap_con ...
- 【案例分享】SequoiaDB+Spark搭建医院临床知识库系统
1.背景介绍 从20世纪90年代数字化医院概念提出到至今的20多年时间,数字化医院(Digital Hospital)在国内各大医院飞速的普及推广发展,并取得骄人成绩.不但有数字化医院管理信息系统(H ...
- accp8.0转换教材第1章多线程理解与练习
一.单词部分: ①process进程 ②current当前的③thread线程④runnable可获取的 ⑤interrupt中断⑥join加入⑦yield产生⑧synchronize同时发生 二.预 ...
- Linux 内核综述
一.什么是Linux内核: 内核->操作系统中最重要的部分,内核将在系统引导时被装载进RAM,其中包含了很多关键的例程,以操作系统.内核是OS最为关键的部分,人们常将OS(操作系统)与内核等同. ...
- Java 中基本类型和字符串之间的转换
Java 中基本类型和字符串之间的转换 在程序开发中,我们经常需要在基本数据类型和字符串之间进行转换. 其中,基本类型转换为字符串有三种方法: 1. 使用包装类的 toString() 方法 2. 使 ...
- 将HTML导出生成word文档
前言: 项目开发中遇到了需要将HTML页面的内容导出为一个word文档,所以有了这边随笔. 当然,项目开发又时间有点紧迫,第一时间想到的是用插件,所以百度了下.下面就介绍两个导出word文档的方法. ...
- ReadAndWriteBinaryFile
package JBJADV003;import java.io.FileInputStream;import java.io.DataInputStream;import java.io.EOFEx ...
- Unity-Shader-动态阴影(上) 投影的矩阵变换过程
[旧博客转移 - 2017年1月20日 01:20 ] 前面的话 最近很长时间没写博文了,一是太忙 ( lan ) 了,二是这段时间又领悟了一些东西,脑子里很混乱,不知道从何写起.但感觉不能再拖延下去 ...
- java 邮件发送工具类
首先需要下载mail.jar文件,我个人通常是使用maven中心库的那个: <dependency> <groupId>javax.mail</groupId> & ...