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包 实体类,数据库字段, ...
随机推荐
- php中json对象数据的输出转化
php中json对象数据的输出转化 public function get_my_now_citys(){ $datas=$this->_post('datas'); //前台js脚本传递给后端 ...
- Spring Boot 构建 WAR和JAR 文件
原文:https://github.com/x113773/testall/issues/3 ## JAR文件方式一:1. 修改[pom.xml](https://github.com/x113773 ...
- [leetcode-516-Longest Palindromic Subsequence]
Given a string s, find the longest palindromic subsequence's length in s. You may assume that the ma ...
- 如何解决java文件上面有错,但是文件夹上面不显示的错误
查了网上很多办法,都是解决不了的,不是我说什么,有些人真的人云亦云,没解决的瞎**乱转发 解决方法是:换个工作空间(workplace)即可,不行你打我
- Discuz论坛提速优化技巧
Discuz是国内最受站长们欢迎的建站源码之一,除了开源以外还有着很强大的后台,即便是没有建站基础和不懂代码的站长也能很快的架设出一个论坛,甚至是门户. 一个网站的加载速度除了影响你在搜索引擎里的排名 ...
- 【MUI】百度地图定位功能
博主最近进行一款APP开发,需要用到定位功能,经过一番折腾,终于搞定,不废话,代码如下 mui.plusReady(function() { var longitude, latitude; //va ...
- Python文件系统功能:os模块
Python文件系统功能:os模块 1.os模块方法分类 (1)目录: chdir() 改变工作目录 chroot() 设定当前进程的根目录 listdir() 列出指定目录下的所有文件名 mkdir ...
- VB6之摄像头控制
参考文献:http://www.cnblogs.com/xidongs/archive////.html 直接上代码: 'code by lichmama from cnblogs.com '@vb6 ...
- Java 实现FTP上传和下载
1. 目前网上开源的FTP Client主要有JFTP.FTP4.edtFtpjJ和Apache.FTPClient. 2. jftp地址:http://www.jmethods.com/ 3. ed ...
- ubuntu16.04安装不上有道词典的解决办法
转自:http://www.linuxdiyf.com/linux/21143.html ubuntu16.04安装不上有道词典,提示: le@hu-pc:~/下载$ sudo dpkg -i you ...